Java操作HDFS文件系统

Java面试宝典、学习笔记

目录

获取文件系统

文件操作


获取文件系统

/**
 * 获取文件系统
 */
public class getFileSystemTest {

    /**
     * 第一种方式:获取文件系统
     * configuration:该类的对象封装了客户端或者服务器的配置
     * filesystem
     */
    @Test
    public void getSystem() throws IOException {
        // 获取configuration对象
        Configuration configuration = new Configuration();
        // 设置configuration对象,设置的目的是来指定我们要操作的文件系统
        // hdfs://node01:8020 操作分布式文件系统, 操作元数据节点
        configuration.set("fs.defaultFS" , "hdfs://node01:8020");
        // 获取指定的文件系统, 获取FileSystem就相当于获取了主节点中所有的元数据信息
        FileSystem fileSystem = FileSystem.get(configuration);
        System.out.println("fileSystem:"+fileSystem.toString());
    }

    /**
     * 第二种方式:获取文件系统
     * configuration:该类的对象封装了客户端或者服务器的配置
     * filesystem
     */
    @Test
    public void getSystem2() throws URISyntaxException, IOException {
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://node01:8020"), new Configuration());
        System.out.println("fileSystem2:"+fileSystem.toString());
    }

    /**
     * 第三种方式:获取文件系统
     * configuration:该类的对象封装了客户端或者服务器的配置
     * filesystem
     */
    @Test
    public void getSystem3() throws IOException {
        Configuration configuration = new Configuration();
        configuration.set("fs.defaultFS" , "hdfs://node01:8020");

        FileSystem fileSystem = FileSystem.newInstance(configuration);
        System.out.println("fileSystem3:"+fileSystem.toString());
    }

    /**
     * 第四种方式:获取文件系统
     * configuration:该类的对象封装了客户端或者服务器的配置
     * filesystem
     */
    @Test
    public void getSystem4() throws Exception {
        FileSystem fileSystem = FileSystem.newInstance(new URI("hdfs://node01:8020"), new Configuration());
        System.out.println("fileSystem4:"+fileSystem.toString());
    }

}

文件操作

public class getHDFSFileTest {

    /**
     * 文件上传
     */
    @Test
    public void uploadFile() throws URISyntaxException, IOException, InterruptedException {
        // 1.获取FileSystem对象
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://node01:8020"), new Configuration(), "root");
        // 本地拷贝到hdfs上
        fileSystem.copyFromLocalFile(
                new Path("C:\\Users\\baidu\\Desktop\\123\\1111.jpg"),
                new Path("/study/hadoop/1111.jpg"));
        fileSystem.close();
    }

    /**
     * 方式二:下载文件
     */
    @Test
    public void downloadFile2() throws IOException, URISyntaxException, InterruptedException {
        // 1.获取FileSystem对象  伪造用户root
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://node01:8020"), new Configuration(), "root");
        // 2.调用方法直接实现文件的下载
        fileSystem.copyToLocalFile(new Path("/newFile.txt"),
                new Path("C:\\Users\\baidu\\Desktop\\newFile.txt"));
        fileSystem.close();
    }

    /**
     * 方式一:下载文件
     */
    @Test
    public void downloadFile() throws URISyntaxException, IOException {
        // 1.获取FileSystem对象
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://node01:8020"), new Configuration());
        // 2.获取hdfs文件的输入流
        FSDataInputStream inputStream = fileSystem.open(new Path("/seats.sql"));
        // 3.获取本地文件的输出流
        FileOutputStream outputStream = new FileOutputStream(new File("C:\\Users\\baidu\\Desktop\\video\\hello.txt"));
        // 4.实现文件下载
        IOUtils.copy(inputStream, outputStream);
        // 关闭流
        IOUtils.closeQuietly(inputStream);
        IOUtils.closeQuietly(outputStream);
        fileSystem.close();
    }

    /**
     * 在hdfs上创建文件(夹)
     */
    @Test
    public void mkdirHdfs() throws Exception {
        // 1.获取FileSystem对象
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://node01:8020"), new Configuration());

        // 2.创建文件夹
        //fileSystem.mkdirs(new Path("/javaApi/test"));
        // 3.创建文件
        fileSystem.create(new Path("/javaApi/test/a.txt"));
    }

    /**
     * 获取目录下所有文件信息
     */
    @Test
    public void getFileList() throws Exception {
        // 1.获取FileSystem对象
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://node01:8020"), new Configuration());
        // 2.获取remoteIterator得到所有的文件或文件夹,第一个参数指定遍历的路径,第二个参数表示是否要递归遍历
        // 获取目录下所有文件信息
        RemoteIterator<LocatedFileStatus> files = fileSystem.listFiles(new Path("/"), true);
        while (files.hasNext()) {
            LocatedFileStatus next = files.next();
            // hdfs://node01:8020/study/hadoop/alibaba/阿里巴巴Java开发手册2.0.pdf
            System.out.println(next.getPath().toString());
            System.out.println(next.getPath().getName().toString());
            // 获取文件的block存储信息
            BlockLocation[] blockLocations = next.getBlockLocations();
            System.out.println("每个文件的block数量:"+blockLocations.length);
            for (BlockLocation blockLocation : blockLocations) {
                // 每个block副本的存储位置
                // 每个副本存储主机的位置
                String[] hosts = blockLocation.getHosts();
                for (String host : hosts) {
                    System.out.println(host+"===");
                }
            }
        }
        fileSystem.close();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

架构师成长进阶空间

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值