阅读背景:

Chpater03 Hadoop组件(二) 剖析MapReduce程序

来源:互联网 
采用maven来编写程序,功能为合并多个文件为一个文件
package hadoop;import java.io.IOException;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 main(String[] args) throws IOException {//保留键值对的配置类Configuration conf=new  Configuration();//Hadoop api的起点是FileSystem.使用工厂方法获取实例FileSystem hdfs=FileSystem.get(conf);//获取专用于本地文件系统的FileSystem对象FileSystem local=FileSystem.getLocal(conf);//使用Path独享编制文件和目录Path inputDir=new Path(args[0]);Path hdfsFile=new Path(args[1]);try{//FileStatus存储文件和目录的元数据,得到目录中的文件列表FileStatus[] inputFiles=local.listStatus(inputDir);//创建流对象FSDataOutputStream out=hdfs.create(hdfsFile);for (int i = 0; i < inputFiles.length; i++) {System.out.println(inputFiles[i].getPath().getName());FSDataInputStream in=local.open(inputFiles[i].getPath());byte[] buffer=new byte[256];int byteRead=0;while((byteRead=in.read(buffer))>0){out.write(buffer, 0, byteRead);}in.close();}out.close();}catch(IOException e){e.printStackTrace();}}}package hadoop;im



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: