解析hadoop框架下的Map-Reduce job的输出格式的实现
?????Hadoop 其实并非一个单纯用于存储的分布式文件系统,而是一个被设计用来在由普通硬件设备组成的大型集群上执行分布式应用的框架。 Hadoop 包含两个部分:一个分布式文件系统 HDFS (Hadoop Distributed File System),和一个Map-Reduce实现。
?
??? 研究hadoop,从nutch入手是比较好的选择,分布式文件系统就不说了,下面说说MapReduce产生Job中设置的输入输出,一般new一个Job会这样设置 输入输出路径:
?
基类FileOutputFormat:org.apache.hadoop.mapred.FileOutputFormat
?这是个抽象类,实现了检查输入路径是否存在的方法,具体输出方式写成抽象方法预留给了子类。
?子类见下图:
?
子类MapFileOutputFormat :org.apache.hadoop.mapred.MapFileOutputFormat
public class ParseOutputFormat implements OutputFormat<Text, Parse> {//这里不是检查输出路径,是检查数据路径下的子路径,改变了接口中的定义public void checkOutputSpecs(FileSystem fs, JobConf job) throws IOException { Path out = FileOutputFormat.getOutputPath(job); if (fs.exists(new Path(out, CrawlDatum.PARSE_DIR_NAME))) throw new IOException("Segment already parsed!"); }//下面获取了三个输入句柄,分别向三个路径中输出键值对public RecordWriter<Text, Parse> getRecordWriter(FileSystem fs, JobConf job, String name, Progressable progress)?throws IOException { ...... Path text = new Path(new Path(out, ParseText.DIR_NAME), name); // 一个输出路径 ?Path data = new Path(new Path(out, ParseData.DIR_NAME), name); //两个输出路径?Path crawl = new Path(new Path(out, CrawlDatum.PARSE_DIR_NAME), name);//三个输出路径? //一个写入 final MapFile.Writer textOut = new MapFile.Writer(job, fs, text.toString(), Text.class, ParseText.class, CompressionType.RECORD, progress); //第二个写入 final MapFile.Writer dataOut = new MapFile.Writer(job, fs, data.toString(), Text.class, ParseData.class, compType, progress); //第三个写入 final SequenceFile.Writer crawlOut = SequenceFile.createWriter(fs, job, crawl, Text.class, CrawlDatum.class, compType, progress); return new RecordWriter<Text, Parse>() { public void write(Text key, Parse parse)throws IOException {...... crawlOut.append(key, d);....... crawlOut.append(new Text(newUrl), newDatum);...... crawlOut.append(key, adjust);...... dataOut.append(key, parseData);...... crawlOut.append(key, datum); } }//关闭三个句柄 public void close(Reporter reporter) throws IOException { textOut.close(); dataOut.close(); crawlOut.close(); } }; }}?? ParseOutputFormat实现了OutputFormat接口,改变了job中设置的输出路径,并且把不同的内容输出到不同的路径,从而达到了多个输出(并且根据逻辑划分)。这个我觉得值得借鉴。
?
?? 关于输入以及输入输出的各个实现都有什么用处,以后有机会再来写写。本人现在还是一知半解,见笑了。