hadoop 上传文件
Posted Stay Hungry, Stay Foolish
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hadoop 上传文件相关的知识,希望对你有一定的参考价值。
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
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;
public class UploadFile {
public static void main(String[] args) {
try {
String localSrc = "/usr/local/hadoop/NOTICE.txt";
String dst = "hdfs://localhost:9000/user/NOTICE.txt";
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);
System.out.println("success");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
以上是关于hadoop 上传文件的主要内容,如果未能解决你的问题,请参考以下文章