hadoop入门之通过java代码实现将本地文件上传到hadoop的文件系统

package upload;
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;
public class UploadFile {
 public static void main(String[] args) {
try {
String localStr = args[0];
String dst = args[1];
//in对应的是本地文件系统的目录
InputStream in = new BufferedInputStream(new FileInputStream(localStr));
Configuration conf = new Configuration();
//获得hadoop系统的连接 
FileSystem fs = FileSystem.get(URI.create(dst),conf);

//out对应的是Hadoop文件系统中的目录
OutputStream out = fs.create(new Path(dst));
IOUtils.copyBytes(in, out, 4096,true) //4096是4k字节
System.out.println("success");
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
展开阅读全文

没有更多推荐了,返回首页