Hadoop通过API访问HDFS

1、version_1

 /**
     * 通过Hadoop API访问HDFS
     * @throws IOException
     */
    @Test
    public void readFileByAPI() throws IOException {
        //获取hadoop配置信息
        Configuration conf = new Configuration();
        //添加名称节点的映射
        /**
         * <property>
         <name>fs.defaultFS</name>
         <value>hdfs://s150</value>
         </property>
         */
         conf.set("fs.defaultFS","hdfs://s150");
        //获取文件系统
        FileSystem fs = FileSystem.get(conf);
        //获取路径
        Path p = new Path("hdfs://s150:8020/usr/xiaoqiu/hadoop/index.html");
        //通过文件系统打开路径获取HDFS文件输入流
        FSDataInputStream fis =  fs.open(p);
        //创建缓冲区
        byte[] buf = new byte[1024];
        int len = -1;
        //当当读取的长度不等于-1的时候开始写入
        //写入需要字节输出流
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while ((len = fis.read(buf)) != -1){
            baos.write(buf,0,len);
        }
        //写入完毕,关闭输入流
        fis.close();
        //关闭输出流
        baos.close();
        //将输出流转换字节数组
        byte[] bytes= baos.toByteArray();
        //将字节数组转为字符串
        String str = new String(bytes);
        //打印字符串
        System.out.println(str);

    }

2、version_2借助Hadoop的IOutil对文件进行读取和写入缓冲区

/**
     * 通过Hadoop API访问HDFS
     *借助IOUtils进行文件的读取
     * @throws IOException
     */
    @Test
    public void readFileByAPI2() throws IOException {
        //获取hadoop配置信息
        Configuration conf = new Configuration();
        //添加名称节点的映射
        /**
         * <property>
         <name>fs.defaultFS</name>
         <value>hdfs://s150</value>
         </property>
         */
        conf.set("fs.defaultFS","hdfs://s150");
        //获取文件系统
        FileSystem fs = FileSystem.get(conf);
        //获取路径
        Path p = new Path("hdfs://s150:8020/usr/xiaoqiu/hadoop/index.html");
        //通过文件系统打开路径获取HDFS文件输入流
        FSDataInputStream fis =  fs.open(p);
        //写入需要字节输出流
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        //借助Hadoop的IOutil对文件进行读取和写入缓冲区
        IOUtils.copyBytes(fis,baos,1024);
        //写入完毕,关闭输入流
        fis.close();
        //关闭输出流
        baos.close();
        //将输出流转换字节数组
        byte[] bytes= baos.toByteArray();
        //将字节数组转为字符串
        String str = new String(bytes);
        //打印字符串
        System.out.println(str);

    }

3、创建文件/文件夹

 /**
     * 创建文件夹
     * @throws IOException
     */
    @Test
    public void mkdir() throws IOException {
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://s150:8020");
        FileSystem fs = FileSystem.get(conf);
        fs.mkdirs(new Path("/usr/xiaoqiu/hadoop/mydir1"));

    }

    /**
     * 创建文件
     */
    @Test
    public void mkFile() throws IOException {
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://s150:8020");
        FileSystem fs = FileSystem.get(conf);
        //创建文件,返回输出流
        FSDataOutputStream fos = fs.create(new Path("/usr/xiaoqiu/hadoop/mydir1/hello1.txt"));
        //写入文件
        fos.write("helloworld".getBytes());
        //关闭输出流
        fos.close();
    }

4、删除文件夹/文件

 /**
     * 删除文件夹
     * @throws IOException
     */
    @Test
    public void removeFile() throws IOException {
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://s150:8020");
        FileSystem fs = FileSystem.get(conf);
        fs.delete(new Path("/usr/xiaoqiu/hadoop/mydir1"),true);
    }





































































欢迎关注我的公众号:小秋的博客 CSDN博客:https://blog.csdn.net/xiaoqiu_cr github:https://github.com/crr121 联系邮箱:rongchen633@gmail.com 有什么问题可以给我留言噢~
原文地址:https://www.cnblogs.com/flyingcr/p/10327005.html