您的位置:首页 > 运维架构

上传本地文件到hdfs

2016-12-19 17:33 288 查看
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Progressable;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;

/**
* Created by CaoSen on 2016-12-19.
*/
public class TestLocalHdfsjava {
public static void main(String[] args)throws Exception{
String localSrc = "C:\\Users\\CaoSen\\Desktop\\tt.scala";
String dst = "hdfs://192.168.42.139:9000/tt.scala";
InputStream in = new BufferedInputStream(new FileInputStream(localSrc));
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(dst), conf);
OutputStream out = fs.create(new Path(dst), new Progressable() {
public void progress() {
System.out.print(".");
}
});
IOUtils.copyBytes(in, out, 4096, true);
out.flush();
out.close();
fs.close();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hdfs hadoop