HDFSAPI编程源码-文件创建操作

 首先获取文件配置

//获得文件系统对象,由于只有root用户才有写权限,因此用root用户去获取文件系统对象

创建一个txt文件,再使用IO流写入数据

最后再关闭

package hdfsapi;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.URI;

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

public class E6_CreateAPI {

	public static void main(String[] args) throws IOException, InterruptedException {
		// TODO Auto-generated method stub
		Configuration conf =new Configuration();
		//conf.set("fs.defaultFS", "hdfs://master:8020");
		//FileSystem fs = FileSystem.get(conf);
		FileSystem fs = FileSystem.get(URI.create("hdfs://master:8020"), conf, "root");
		FSDataOutputStream fin = fs.create(new Path("/user/root/hello1.txt"));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fin));
		bw.write("Hello World!");
		bw.newLine();
		bw.write("I am a student.\n");
		bw.close();
		fin.close();
		fs.close();
		System.out.println("done...");
	}

}

你可能感兴趣的:(hadoop,hdfs,java)