Configuration、FileSystem
小白写博客,在hadoop集群写入文件,看完configuration和configured等的源码之后的心德
hadoop集群写入文件
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
/**
* hadoop jar Hadoop_test-1.0-SNAPSHOT.jar com.jxlg.hdfs.ConfigHdfs
* -D input=/etc/passwd -D output=passwd/passwd.txt
*
*/
import java.io.FileInputStream;
import java.io.OutputStream;
public class ConfigHdfs extends Configured implements Tool {
@Override
public int run(String[] strings) throws Exception {
Configuration conf=getConf();
String input =conf.get("input");
String output=conf.get("output");
FileSystem fs = FileSystem.get(conf);
FileInputStream fis=new FileInputStream(input);
OutputStream os=fs.create(new Path(output));
byte []b=new byte[1024];
int len;
while((len=fis.read(b))!=-1){
os.write(b,0,len);
os.flush();
}
if(os!=null)os.close();
if(fis!=null)fis.close();
return 0;
}
public static void main(String[] args) throws Exception {
int n= new ToolRunner().run(new ConfigHdfs(),args);
System.exit(n);
}
}