將本地小文件合併上傳到HDFS文件系統中。 html
一種方法能夠如今本地寫一個腳本,先將一個文件合併爲一個大文件,而後將整個大文件上傳,這種方法佔用大量的本地磁盤空間; java
另外一種方法以下,在複製的過程當中上傳。 apache
package mergerfiles; import java.io.IOException; import org.apache.hadoop.io.IOUtils; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; public class PutMerge { public static void putMergeFunc(String localDir,String fsFile) throws IOException{ Configuration conf = new Configuration(); conf.set("fs.default.name", "hdfs://localhost:9000"); FileSystem fs = FileSystem.get(conf); //fs 是HDFS文件系統 FileSystem local = FileSystem.getLocal(conf); //本地文件系統 Path localdir = new Path(localDir); Path hdfsFile = new Path(fsFile); FileStatus[] status = local.listStatus(localdir);//獲得輸入路徑 FSDataOutputStream out = fs.create(hdfsFile); //在HDFS 上建立輸出文件 for(FileStatus st: status){ Path temp = st.getPath(); FSDataInputStream in = local.open(temp); IOUtils.copyBytes(in, out, 4096, false); //讀取in流中的內容放入out in.close(); //完成後,關閉當前文件輸入流 } out.close(); } /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub String sourceFile = ""; String targetFile = ""; if(args.length<2){ sourceFile = "/opt/201211 1330/20121101"; targetFile = "/import/1/20121101/20121101.txt"; }else{ sourceFile = args[0]; targetFile = args[1]; } putMergeFunc(sourceFile, targetFile); } }
引自 : http://www.cnblogs.com/dandingyy/archive/2013/03/08/2950720.html oop