Hadoop API 操作

导入依赖

     <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>3.1.3</version>
        </dependency>

获取FileSystem的四种方式

 /**
     * Configuration   该类的对象封装了客户端或者服务器的配置
     * FileSystem 该类的对象是一个文件系统对象,可以使用该类对象的一些方法来对文件进行操作,通过FileSyatem
     * 的静态方法get获得该对象
     **/
    //获取FileSystem的集中类型
    //第一种
    @Test
    public void FileSystemdemo01() throws IOException {
        Configuration configuration = new Configuration();
        //指定使用的文件系统类型
        //fs.defaultFS 固定不变的参数
        configuration.set("fs.defaultFS", "hdfs://node01:8020/");
        //获取指定的文件系统
        //导包注意 import org.apache.hadoop.fs
        FileSystem fileSystem = FileSystem.get(configuration);
        System.out.println("demo01-->" + fileSystem);
    }

    //第二种
    @Test
    public void FileSystemdemo02() throws Exception {
        FileSystem fileSystem = FileSystem.get(
                new URI("hdfs://node01:8020")
                , new Configuration()
        );
        System.out.println("demo02-->" + fileSystem);
    }

    //第三种
    @Test
    public void FileSystemdemo03() throws IOException {
        Configuration configuration = new Configuration();
        //指定文件系统
        configuration.set("fa.defaultFS", "hdfs://node01:8020");
        //创阿FileSystem的实列
        FileSystem fileSystem = FileSystem.newInstance(configuration);
        System.out.println("demo03-->" + fileSystem.toString());
    }

    //地四种
    @Test
    public void FileSystemdemo04() throws Exception {
        FileSystem fileSystem = FileSystem.newInstance(
                new URI("hdfs://node01:8020")
                , new Configuration());
        System.out.println("demo04-->" + fileSystem);
    }

使用url访问数据

//1.错误1

    /**
     * 是maven加载hadoop的依赖包之后,启动项目出现了以上异常
     * 这个异常是jar包的冲突,删除掉slf4j-log4j12-1.7.26.jar就可以了
     * 我们在maven依赖中对hadoop-hdfs和hadoop-client的依赖都去除slf4j-log4j12依赖即可
     */
    @Test  //使用url方式访问数据
    public void demo01() throws IOException {
        //1.注册hdfs的url
        URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
        //2.获取文件输入流
        InputStream inputStream = new URL("hdfs://node01:8020/dir1/a.txt").openStream();
        //3.获取文件输出流
        FileOutputStream fileOutputStream = new FileOutputStream(new File("D:/a.txt"));
        //4.实现文件的拷贝
        IOUtils.copy(inputStream, fileOutputStream);
        //5.关闭流
        System.out.println("完成");
        org.apache.commons.io.IOUtils.closeQuietly(inputStream);
        org.apache.commons.io.IOUtils.closeQuietly(fileOutputStream);

    }

HADOOP API 操作

    //遍历hdfs中的所有文件
    @Test
    public void mylistFile() throws Exception {
        //获取FileSystem
        FileSystem fileSystem = FileSystem.newInstance(
                new URI("hdfs://node01:8020"),
                new Configuration());
        //获取RemoteIterator(迭代器) 得到所有的文件夹 第一个参数指定要遍历的路径,第二参数指定是否递归遍历
        RemoteIterator<LocatedFileStatus> fileStatusRemoteIterator =
                fileSystem.listFiles(new Path("/"), true);
        while (fileStatusRemoteIterator.hasNext()) {
            //获取文件
            LocatedFileStatus next = fileStatusRemoteIterator.next();
            //获取block信息
            BlockLocation[] blockLocations = next.getBlockLocations();
            System.out.println("block数量-->" + blockLocations.length);
            System.out.println("获取文件名字-->" + next.getPath().getName());
            System.out.println("获取文件路径-->" + next.getPath().toString());
        }
        //关闭
        fileSystem.close();
    }


    //hdfs创建文件夹
    @Test
    public void mkdirdemo() throws Exception {
        FileSystem fileSystem = FileSystem.newInstance(
                new URI("hdfs://node01:8020"),
                new Configuration());
        boolean mkdirs = fileSystem.mkdirs(new Path("/aa/bb/cc"));
        FSDataOutputStream fsDataOutputStream = fileSystem.create(new Path("/aa/bb/cc/a.txt"));
        if (mkdirs) {
            System.out.println("创建成功");
        }
        fileSystem.close();
    }


    @Test//创建文件
    public void mkdirdemo01() throws Exception {
        FileSystem fileSystem = FileSystem.newInstance(
                new URI("hdfs://node01:8020"),
                new Configuration());
        //如果输入的路径不存在也会自动创建
        fileSystem.create(new Path("/aa/bb/cc/a.txt"));
        fileSystem.close();
    }


    @Test//删除文件或文件夹
    public void deletefileandwjj() throws Exception {
        FileSystem fileSystem = FileSystem.newInstance(
                new URI("hdfs://node01:8020"),
                new Configuration());
        //删除(删除路径最后面的文件或者文件夹)
        fileSystem.deleteOnExit(new Path("/aa/bb/cc"));
        fileSystem.close();
    }

    //文件的下载
    @Test
    public void FileToLocal() throws URISyntaxException, IOException {
        FileSystem fileSystem = FileSystem.newInstance(
                new URI("hdfs://node01:8020")
                , new Configuration());
        //路径是hadoop的路径 通过filesystem的open方法
        FSDataInputStream open = fileSystem.open(new Path("/aa/bb/a.txt"));
         //上传到本地
        FileOutputStream fileOutputStream =new FileOutputStream(new File("D:/aa.txt"));
         //一边读取一边上传
        IOUtils.copy(open,fileOutputStream);
        //关闭流
        org.apache.commons.io.IOUtils.closeQuietly(open);
        org.apache.commons.io.IOUtils.closeQuietly(fileOutputStream);
        //关闭filesystem
        fileSystem.close();
    }


    //文件下载方法二
    @Test
    public void  FileToLocal02() throws URISyntaxException, IOException {
        FileSystem fileSystem = FileSystem.newInstance(
                new URI("hdfs://node01:8020")
                , new Configuration());
        fileSystem.copyToLocalFile(new Path("/aa/bb/a.txt"),new Path("D:/bb.txt"));
        fileSystem.close();
    }

    //本地文件上传到hadoop
    @Test
    public  void  LocalToFile() throws URISyntaxException, IOException {
        FileSystem fileSystem = FileSystem.newInstance(
                new URI("hdfs://node01:8020")
                , new Configuration());
        fileSystem.copyFromLocalFile(new Path("file:///D:/abc.txt"),new Path("/aa"));
        fileSystem.close();
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

热心市民爱抽烟屁

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

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

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

打赏作者

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

抵扣说明:

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

余额充值