资源描述:
《Hadoop倒排索引例子.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、packagecn.yws;importjava.io.IOException;importjava.util.StringTokenizer;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.fs.Path;importorg.apache.hadoop.io.Text;importorg.apache.hadoop.mapreduce.Job;importorg.apache.hadoop.mapreduce.Mapper;importorg.apache.hado
2、op.mapreduce.Reducer;importorg.apache.hadoop.mapreduce.lib.input.FileInputFormat;importorg.apache.hadoop.mapreduce.lib.input.FileSplit;importorg.apache.hadoop.mapreduce.lib.output.FileOutputFormat;importorg.apache.hadoop.util.GenericOptionsParser;//倒排索引请在hadoopindex_in目录下放置file1
3、,file2测试文件publicclassMyInvertedIndex{publicstaticclassMapextendsMapper
4、{//super.map(key,value,context);//获取文件分词split=(FileSplit)context.getInputSplit();StringTokenizertokenizer=newStringTokenizer(value.toString());while(tokenizer.hasMoreTokens()){intsplitindex=split.getPath().toString().indexOf("file");keyinfo.set(tokenizer.nextToken()+":"+split.ge
5、tPath().toString().substring(splitindex));valueinfo.set("1");//file3:1;context.write(keyinfo,valueinfo);}}}publicstaticclassCombineextendsReducer{privateTextinfoText=newText();@Overrideprotectedvoidreduce(Textkey,Iterablevalues,Contextcontext)throwsIOE
6、xception,InterruptedException{//super.reduce(key,values,context);intsum=0;for(Textvalue:values){sum+=Integer.parseInt(value.toString());}intsplitindex=key.toString().indexOf(":");//file2:1;file3:2;file1:1infoText.set(key.toString().substring(splitindex+1)+":"+sum);key.set(key.to
7、String().substring(0,splitindex));context.write(key,infoText);}}publicstaticclassReduceextendsReducer{privateTextresult=newText();@Overrideprotectedvoidreduce(Textkey,Iterablevalues,Contextcontext)throwsIOException,InterruptedException{//super.reduce(k
8、ey,values,context);//生成文档列表Stringfilelist=newSt