hadoop (五)操作HDFS

hadoop (五)操作HDFS

环境搭建参考 《hadoop (四)开发环境及Word Count》。

实例代码

package org.cnliu.myhadoop.ex;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class DFSTest {
    public static void main(String[] args) throws  Exception{
        Configuration configuration=new Configuration();
        configuration.set("fs.default.name","hdfs://192.168.56.104:9000");

        FileSystem fileSystem=FileSystem.get(configuration);

        //列出hdfs上 /input 目录下的所有文件
        FileStatus[] statuses = fileSystem.listStatus(new Path("/user/liuzd"));
        for (FileStatus status:statuses){
            System.out.println(status.getPath());
        }

        //写入数据到hdfs的path路径
        Path path= new Path("/user/liuzd/in/text3.txt");
        FSDataOutputStream fsDataOutputStream=fileSystem.create(path);

        byte[] buffer ="你好 Hello".getBytes("UTF-8");
        fsDataOutputStream.write(buffer,0,buffer.length);
        fsDataOutputStream.flush();
        fsDataOutputStream.close();

        System.out.println("Create OK");

    }
}

DFS操作列表

image

你可能感兴趣的:(hadoop (五)操作HDFS)