HDFS常见API操作

HDFS常见API操作

一个刚开始学习大数据的小白,愿意和大家分享所学所得。

1、put:文件上传 对应hadoop fs -put(等同于hadoop fs -copyFromLocal)

//上传文件
    @Test
    public void put() throws IOException, InterruptedException {
    
    
        //与hdfs服务器建立连接
        FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop102:9000"),new Configuration(),"zmk");

        //用fs进行操作
        fs.copyFromLocalFile(new Path("d:\\hdfsput.txt"),new Path("/"));

        //关闭连接
        fs.close();
    }

由上面所见,每次操作前都需要建立连接,为了简化每次都需要建立连接,这里我们采用@Before注解将建立连接写入,这样再每次执行@Test前都会先执行@Before注解下的内容,同理,我们将释放连接写在@After中。

	private FileSystem fs;
@Before
    public void before() throws IOException, InterruptedException {
    
    
        fs = FileSystem.get(URI.create("hdfs://hadoop102:9000"), new Configuration(), "zmk");
        System.out.println("Before执行了!!!!");
    }
@After
    public void after() throws IOException {
    
    
        fs.close();//关流
        System.out.println("After执行了!!!!");
    }

2、copyToLocalFile:文件下载 对应hadoop fs -get(等同于hadoop fs -copyToFile)

//下载文件到本地
    @Test
    public void get() throws IOException {
    
    
        fs.copyToLocalFile(new Path("/wcoutput"),new Path("d:\\"));
    }

3、mkdirs:创建文件夹 对应hadoop fs -mkdir

//创建文件夹
    @Test
    public void make() throws IOException {
    
    
        fs.mkdirs(new Path("/aaa"));
    }

4、delete:删除操作 对应hadoop fs -rm

//删除hdfs上的文件
    @Test
    public void delete() throws IOException {
    
    
        boolean flag = fs.delete(new Path("/hdfsput.txt"), true);
        if(flag)
            System.out.println("删除成功");
        else
            System.out.println("删除失败");
    }

5、rename操作 对应hadoop fs -mv

//更改文件名
    @Test
    public void rename() throws IOException {
    
    
        fs.rename(new Path("/hdfsput.txt"),new Path("/hdfsrename.txt"));
    }

6、listFile:查看文件详情

//HDFS文件详情查看
    //查看文件名称、权限、长度、块信息
    @Test
    public void listFile() throws IOException {
    
    
        RemoteIterator<LocatedFileStatus> fileStatus = fs.listFiles(new Path("/"), true);//查看hdfs根目录下所有文件详细信息  返回迭代器对象
        while(fileStatus.hasNext()){
    
    
            LocatedFileStatus file = fileStatus.next();
            System.out.println("文件路径信息:"+file.getPath());
            System.out.println("所有者:"+file.getOwner());
            BlockLocation[] blockLocations = file.getBlockLocations();//获取存储的块信息
            for (BlockLocation blockLocation : blockLocations) {
    
    
                System.out.println("块信息:"+blockLocation);
                String[] hosts = blockLocation.getHosts();
                for (String host : hosts) {
    
    
                    System.out.println("块在主机"+host+"上");
                }
            }
        }
    }

7、listStatus:判断是否是文件

//判断是文件还是文件夹
    @Test
    public void liststatus() throws IOException {
    
    
        FileStatus[] statuses = fs.listStatus(new Path("/"));
        for (FileStatus status : statuses) {
    
    
            if (status.isFile()){
    
    
                System.out.println("是文件,该文件路径为:"+status.getPath());
            }else{
    
    
                System.out.println("是文件夹,该文件夹路径为:"+status.getPath());
            }
        }
    }

谢谢观看,若有问题恳请指出

猜你喜欢

转载自blog.csdn.net/qq_40169189/article/details/105546278