栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

HdfsAPI

HdfsAPI

HDFS常用API
package com.ithhs;

import junit.framework.TestCase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.*;
import java.util.Arrays;

public class HDFSJavaApiDemoTest {
    FileSystem fs = null;

    
    @Before
    public void init() throws IOException {
        //设置当前用户为root用户
        System.setProperty("HADOOP_USER_NAME", "root");
        Configuration configuration = new Configuration();
        configuration.set("fs.defaultFS", "hdfs://mini1:9000");
        //创建客户端对象
        fs = FileSystem.get(configuration);
    }
	
    //创建文件夹
    @Test
    public void testMkDirs() throws IOException {
        //在hdfs上创建文件夹
        Path path = new Path("/test2");
        boolean isSuccess = fs.mkdirs(path);
        //断言
        Assert.assertEquals(true, isSuccess);
    }
	
    //删除文件或文件夹
    @Test
    public void testDeleteDir() throws IOException {
        
        boolean isDelete = fs.delete(new Path("/test2"), true);
        //断言
        Assert.assertEquals(true, isDelete);
    }

    //上传一个文件到hdfs上
    @Test
    public void testCopyFromLocal() throws IOException {
        
        fs.copyFromLocalFile(new Path("e:/user.txt"), new Path("/user.txt"));
    }

	//从hdfs上下载一个文件到本地
    @Test
    public void testCopyToLocal() throws IOException {
        
        fs.copyToLocalFile(new Path("/user.txt"), new Path("e:/u.txt"));
    }

    
    @Test
    public void testListFiles() throws IOException {
        
        RemoteIterator remoteIterator = fs.listFiles(new Path("/"), true);
        while (remoteIterator.hasNext()) {//判断是否有下一个元素
            //LocatedFileStatus 代表的是一个文件的描述信息
            LocatedFileStatus fileStatus = remoteIterator.next();//取出下一个元素,并将指针往后移动一位
            System.out.println("文件路径:" + fileStatus.getPath());
            System.out.println("文件名称:" + fileStatus.getPath().getName());
            System.out.println("文件的长度:" + fileStatus.getLen());
            System.out.println("文件块大小:" + fileStatus.getBlockSize());
            System.out.println("文件副本:" + fileStatus.getReplication());
            System.out.println("文件块信息:" + Arrays.toString(fileStatus.getBlockLocations()));
            System.out.println("=====================================");
        }
    }

    
    @Test
    public void testListStatus() throws IOException {
        
        FileStatus[] fileStatuses = fs.listStatus(new Path("/"));
        for (FileStatus fileStatus : fileStatuses) {
        //判断遍历到的对象是文件还是文件夹
            if (fileStatus.isDirectory()) {
                System.out.println("是一个目录");
            } else {
                System.out.println("是一个文件");
            }
            System.out.println("文件路径:" + fileStatus.getPath());
            System.out.println("文件名称:" + fileStatus.getPath().getName());
            System.out.println("文件的长度:" + fileStatus.getLen());
            System.out.println("文件块大小:" + fileStatus.getBlockSize());
            System.out.println("文件副本:" + fileStatus.getReplication());
            System.out.println("=====================================");
        }
    }

    
    @Test
    public void testCreate() throws IOException {
        
        //本质是一个字节输出流
        FSDataOutputStream fsDataOutputStream = fs.create(new
        Path("/out.txt"), true);
        for (int i = 0; i < 10; i++) {
        fsDataOutputStream.write("hello world".getBytes());
        fsDataOutputStream.write("rn".getBytes());
        }
        fsDataOutputStream.close();
    }

    
    @Test
    public void testRead() throws IOException {
        //FSDataInputStream 本质上是一个字节输入流
        FSDataInputStream fsDataInputStream = fs.open(new
        Path("/out2.txt"));
        int readNum = 0;//读取到的有效字节数
        byte[] bytes = new byte[1024];//每次最多读取1024个字节
        while ((readNum = fsDataInputStream.read(bytes)) != -1) {
        System.out.println(new String(bytes, 0, readNum));
        }
        fsDataInputStream.close();
    }

    
    @After
    public void destroy() throws IOException {
        //客户端对象用完后需要关闭
        fs.close();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/743169.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号