• HDFS IO流操作


    一、IO流文件上传

        @Test
        public void testIOPut() throws URISyntaxException, IOException, InterruptedException {
            // 1. 获取对象
            Configuration conf = new Configuration();
            FileSystem fs = FileSystem.get(new URI("hdfs://wt1:9000"), conf, "tom");
            // 2. 获取输入流
            FileInputStream fIS = new FileInputStream(new File("D:\test.txt"));
            // 3. 获取输出流
            FSDataOutputStream fOS = fs.create(new Path("/lili.txt"));
            // 4. 流的对拷
            IOUtils.copyBytes(fIS, fOS, conf);
            // 5. 关闭资源
            IOUtils.closeStream(fOS);
            IOUtils.closeStream(fIS);
            fs.close();
        }

    二、IO流文件下载

        @Test
        public void testIODownload() throws URISyntaxException, IOException, InterruptedException {
            // 1. 获取对象
            Configuration conf = new Configuration();
            FileSystem fs = FileSystem.get(new URI("hdfs://wt1:9000"), conf, "tom");
            // 2. 获取输入流
            FSDataInputStream fIS = fs.open(new Path("/abc.txt"));
            // 3. 获取输出流
            FileOutputStream fOI = new FileOutputStream(new File("E:\abc\abc.txt"));
            // 4. 流的对拷
            IOUtils.copyBytes(fIS, fOI, conf);
            // 5. 关闭资源
            IOUtils.closeStream(fOI);
            IOUtils.closeStream(fIS);
            fs.close();
        }

     三、定位读取文件

        // 下载第一块
        @Test
        public void testLocation() throws URISyntaxException, IOException, InterruptedException {
            // 1.获取对象
            Configuration conf = new Configuration();
            FileSystem fs = FileSystem.get(new URI("hdfs://wt1:9000"), conf, "tom");
            // 2.获取输入流
            FSDataInputStream fIS = fs.open(new Path("/jdk-8u261-linux-x64.tar.gz"));
            // 3.获取输出流
            FileOutputStream fOS = new FileOutputStream(new File("E:\abc\jdk-8u261-linux-x64.tar.gz"));
            // 4.流的对拷(只copy128)
            byte[] buf = new byte[1024];
            for (int i = 0; i < 1024 * 128 ; i++) {
                fIS.read(buf);
                fOS.write(buf);
            }
            // 5. 关闭资源
            IOUtils.closeStream(fOS);
            IOUtils.closeStream(fIS);
            fs.close();
        }
    
        // 下载第二块
        @Test
        public void testDownloadTwo() throws URISyntaxException, IOException, InterruptedException {
            // 1. 获取对象
            Configuration conf = new Configuration();
            FileSystem fs = FileSystem.get(new URI("hdfs://wt1:9000"), conf, "tom");
            // 2. 获取输入流
            FSDataInputStream fIS = fs.open(new Path("/jdk-8u261-linux-x64.tar.gz"));
            // 3. 设置指定读取的起点
            fIS.seek(1024*1024*128);
            // 4. 获取输出流
            FileOutputStream fOS = new FileOutputStream(new File("E:\abc\jdk.tar.gz.part2"));
            // 5. 流的对拷
            IOUtils.copyBytes(fIS, fOS, conf);
            // 6. 关闭连接
            IOUtils.closeStream(fOS);
            IOUtils.closeStream(fIS);
            fs.close();
        }
  • 相关阅读:
    python中字符串 s[ : 4]是什么意思?字符串切片
    python找不到模块的问题解决方法(有空钻研一下具体的原因)
    解决CUDA error: no kernel image is available for execution on the device 测试cuda是否可用
    执行 shell 报错 source:not found,一行注释解决
    【项目】项目137
    【项目】项目134
    【项目】项目135
    【项目】项目139
    【项目】项目142
    【项目】项目141
  • 原文地址:https://www.cnblogs.com/wt7018/p/13591291.html
Copyright © 2020-2023  润新知