每日学习

今天进行HDFSAPI操作文件

代码:

package com.zhao.hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.lang.reflect.Array;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;

/**
 * 客户端代码常用套路
 * 1.获取一个客户端对象
 * 2.执行相关操作命令
 * 3.关闭资源
 * */
public class HdfsClient {
    private FileSystem fs;

    @Before
    public void init() throws URISyntaxException, IOException, InterruptedException {
        //连接集群的nn地址
        URI url = new URI("hdfs://Hadoop102:8020");
        //创建一个配置文件
        Configuration configuration = new Configuration();
        //用户
        String user = "zhao";
        //1.获取到了客户端对象
        fs = FileSystem.get(url, configuration, user);
    }

    @After
    public void close() throws IOException {
        //关闭资源
        fs.close();
    }

    @Test
    public void textmkdir() throws URISyntaxException, IOException, InterruptedException {
        //2.创建一个文件夹
        fs.mkdirs(new Path("/xiyou/huaguoshan2"));
    }
    //上传

    /**
     * 参数优先级(由低向高)
     * hdfs-default.xml=>hdfs-site.xml=>在项目资源目录下的配置文件优先级=>代码里面的配置
     */
    @Test
    public void testput() throws IOException {
        //参数解读:参数一:表示删除原数据;参数二:是否允许覆盖;参数三:原数据路径;参数四:目的地路径
        fs.copyFromLocalFile(true, true, new Path("G:\\text02.txt"), new Path("/user/hadoop/test"));
    }

    //文件下载
    @Test
    public void testGet() throws IOException {
        //参数解读:参数一:原文件是否删除 参数二:原文件路径HDFS 参数三:目的路径win 参数四:
        fs.copyToLocalFile(false, new Path("/xiyou/huaguoshan"), new Path("d:\\"), false);
    }
    //删除
    @Test
    public void testRm() throws IOException {
        //参数解读:参数一:要删除的路径 参数二:是否递归删除
        //删除文件
        //fs.delete(new Path("/jdk-8u212-linux-x64.tar.gz"),false);
        //删除空目录
        //fs.delete(new Path("/xiyou"),false);
        //删除非空目录
        fs.delete(new Path("/jinguo"), true);
    }

    //文件的更名和移动
    @Test
    public void testmv() throws IOException {
        //参数解读:参数1:原文件路径;参数2:目标文件路径
        //对文件名称的修改
        //fs.rename(new Path("/wcinput/word.txt"),new Path("/wcinput/ss.txt"));
        //文件的移动和更名
        //fs.rename(new Path("/wcinput/ss.txt"),new Path("/cls.txt"));
        //目录更名
        fs.rename(new Path("/wcinput"), new Path("/output"));
    }

    //获取文件详细信息
    @Test
    public void fileDetail() throws IOException {
        RemoteIterator<LocatedFileStatus> iterator = fs.listFiles(new Path("/"), true);
        while (iterator.hasNext()) {
            LocatedFileStatus fileStatus=iterator.next();
            System.out.println("========" + fileStatus.getPath() + "=========");
            System.out.println(fileStatus.getPermission());
            System.out.println(fileStatus.getOwner());
            System.out.println(fileStatus.getGroup());
            System.out.println(fileStatus.getLen());
            System.out.println(fileStatus.getModificationTime());
            System.out.println(fileStatus.getReplication());
            System.out.println(fileStatus.getBlockSize());
            System.out.println(fileStatus.getPath().getName());

            //获取块信息
            BlockLocation[] blockLocations=fileStatus.getBlockLocations();
            System.out.println(Arrays.toString(blockLocations));
        }
    }
    @Test
    public void testListStatus() throws IOException{
        //  判断是文件还是文件夹
        FileStatus[] listStatus = fs.listStatus(new Path("/"));
        for (FileStatus fileStatus : listStatus) {
            // 如果是文件
            if (fileStatus.isFile()) {
                System.out.println("文件:" + fileStatus.getPath().getName());
            } else {
                System.out.println("目录:" + fileStatus.getPath().getName());
            }
        }
    }
}

 

posted @ 2021-09-22 11:11  哦心有  阅读(18)  评论(0)    收藏  举报