zoukankan      html  css  js  c++  java
  • 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();
        }
  • 相关阅读:
    支付宝生活号授权获取用户信息
    jQuery选项卡
    多图上传加验证加修改file样式
    微信小程序上传图片单张
    圣诞节的整理前两周的内容5
    圣诞节的整理前两周的内容4
    圣诞节的整理前两周的内容3
    圣诞节的整理前两周的内容2
    圣诞节的整理前两周的内容1
    2018.12.14——函数作用域
  • 原文地址:https://www.cnblogs.com/wt7018/p/13591291.html
Copyright © 2011-2022 走看看