1. 程式人生 > >hadoop入門之通過java程式碼實現將本地檔案上傳到hadoop的檔案系統

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());}}}