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();
        }
  • 相关阅读:
    js完美拖拽封装及其案例
    js运动框架封装以及两种案例
    js常见的11种兼容
    Lua 学习笔记(五)函数
    Lua 学习笔记(四)语句与控制结构
    Lua 学习笔记(三)表达式
    Lua 学习笔记(二)语法、类型、值
    Lua 学习笔记(一)环境搭建
    Cocos2d-x 3.2 学习笔记(十六)保卫萝卜 游戏主循环与定时器
    Cocos2d-x 3.2 学习笔记(十五)保卫萝卜 场景与数据
  • 原文地址:https://www.cnblogs.com/wt7018/p/13591291.html
Copyright © 2011-2022 走看看