HDFSAPI编程源码-文件写入

理解HDFS Java API编程原理;

掌握HDFS的命令;

掌握Elipse远程调试Hadoop程序的方法;

掌握HDFS基本的API调用方法

通过调用Java API实现对HDFS的文件系统的操作

将fout中的文本内容写入fin路径下的文件中

package hdfsapi;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URI;

import org.apache.commons.io.CopyUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;

public class E7_WriteDataAPI {

	public static void main(String[] args) throws IllegalArgumentException, IOException, InterruptedException {
		// TODO Auto-generated method stub
		Configuration conf =new Configuration();
		conf.set("fs.defaultFS", "hdfs://master:8020");
		conf.setBoolean("dfs.support.append", true);
		conf.set("dfs.client.block.write.replace-datanode-on-failure.policy", "NEVER"); 
		FileSystem fs = FileSystem.get(URI.create("hdfs://master:8020"),conf,"root");
		
		FileInputStream fin = new FileInputStream(new File("E:\\tmp\\workspace\\test2.txt"));
		FSDataOutputStream fout = fs.append(new Path("/user/root/hello1.txt"));
		IOUtils.copyBytes(fin, fout, 4096,true);
		fout.close();
		fin.close();
		fs.close();
		System.out.println("done!");
	}

}

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