public static void wrteFileHdfs(String localSrc,String hdfsPath){
try {
InputStream in=new BufferedInputStream(new FileInputStream(localSrc));
Configuration conf=new Configuration();
FileSystem fs=FileSystem.get(URI.create(hdfsPath), conf);
OutputStream out=fs.create(new Path(hdfsPath), new Progressable(){
@Override
public void progress() {
System.out.println("*");
}
});
IOUtils.copyBytes(in, out, 4096,true);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String localSrc="/home/zkpk/sogou100.txt";
String hdfsPath="hdfs://master:9000/search/";
WriteHdfsFile.wrteFileHdfs(localSrc, hdfsPath);
}
这段代码展示了如何利用Java的HDFS API将本地文件`/home/zkpk/sogou100.txt`上传到Hadoop的HDFS中,路径为`hdfs://master:9000/search/`。首先,通过`BufferedInputStream`读取本地文件,然后配置HDFS连接,创建`FileSystem`实例,并用`OutputStream`将数据写入HDFS。过程中还实现了进度反馈功能。

2962

被折叠的 条评论
为什么被折叠?



