倒排索引-hadoop_第1页
倒排索引-hadoop_第2页
倒排索引-hadoop_第3页
倒排索引-hadoop_第4页
倒排索引-hadoop_第5页
免费预览已结束,剩余1页可下载查看

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、倒排索弓I-hadoopI-hadoop倒排索引是文档检索系统中常用的数据结构,被广泛地应用于全文搜索引擎。主要用来存储某个单词在一个文档或一组文档中的存储位置的映射,即提供了一种内容来查找文档的方式。 由于不是根据文档来确定文档所包含的内容, 而是进行相反的操作,因而称为倒排索引(InvertedIndex)简单倒排索引的数据结构的元组集合:词汇,文档ID,词汇位置第一、HadoopHadoop的例子-倒排索引1 .输入文件:3个文件巾le1.txt加e2.txt加e3.txt如下:2.那么最后的倒排索引结果是:Hellofile3-txt:1;MapReduce工1J.63.txtIile

2、2*txt;txt:1;byfile? txt:1;二自file2.zxt:2;filel.txt:1powerfi-LlfiLe2.txt11;aemplefil&2-txt;:1;file!,txt;1;第二、功能分析:首先设置map类。使用默认的TextInputFormat类对输入的文件处理,得到文本中的每行的偏移量及其内容。Map过程首先必须分析输入的key,value寸,倒排索引中需要的3个信息:单词、文档URL和词频。其次是Combine的过程。经过map方法处理后,Combine的过程将key值相同的value值累加,得到一个单词在文档中的词频。修改key值和value

3、值。这次将单词作为key值,URL和词频组成value值(如file1.txt:1”),利用MapReduce框架默认的HashPartitioner类完成Shuffle过程, 将相同的单词的所有的记录发送给同一个Reducer进行处理。然后是Reduce过程。Reduce过程只需要将相同的key值的value值组合成倒排索引文件所需的格式即可。川 92-仪 1-记争 H11SagiE塾珈HtfelloKapESeduQBbvJhpEedue第三、完整代码packageorg.apache.hadoop.mapreduce.app.invertedindex;importjava.io.IOE

4、xception;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.hadoop.mapreduce.Reducer;importorg.apache.hadoop.mapredu

5、ce.lib.input.FileInputFormat;importorg.apache.hadoop.mapreduce.lib.input.FileSplit;importorg.apache.hadoop.mapreduce.lib.output.FileOutputFormat;publicclassInvertedindex/mapper类publicstaticclassMapextendsMapperprivateTextkeyInfo=newText();/存储单词和URM合privateTextvalueInfo=newText();/存储词频privateFileSpli

6、tsplit;/存储Split对象/实现map函数Overrideprotectedvoidmap(Objectkey,Textvalue,Contextcontext)throwsIOException,InterruptedException/获取对所属的FileSplit对象split=(FileSplit)context.getInputSplit();StringlineValue=value.toString();StringTokenizeritr=newStringTokenizer(lineValue);while(itr.hasMoreTokens()/key值由单词和UR

7、M成,如MapRudecefile1.txt/获取文件的完整路径Stringitrm=itr.nextToken();keyInfo.set(itrm+:+split.getPath().toString();intsplitIndex=split.getPath().toString().indexOf(file);keyInfo.set(itrm+:+split.getPath().toString().substring(splitIndex);/*intsplitindex=split.getPath().toString().indexOf(file);keyInfo.set(itr

8、.nextToken()+:+split.getPath().toString().substring(splitindex);*/词频初始化为1valueInfo.set(1);context.write(keyInfo,valueInfo);/Combine类publicstaticclassCombineextendsReducerprivateTextinfo=newText();/实现reduce函数Overrideprotectedvoidreduce(Textkey,Iterablevalues,Contextcontext)throwsIOException,Interrupt

9、edException/统计词频intsum=0;for(Textvalue:values)sum+=Integer.parseInt(value.toString();intsplitIndex=key.toString().indexOf(:);/重新设置Value值由URL和词频组成info.set(key.toString().substring(splitIndex+1)+:+sum);/重新设置key值得单词key.set(key.toString().substring(0,splitIndex);context.write(key,info);/Reducer类publicst

10、aticclassReduceextendsReducerprivateTextresult=newText();/实现Reduce函数Overrideprotectedvoidreduce(Textkey,Iterablevalues,Contextcontext)throwsIOException,InterruptedException/生成文档列表StringfileList=newString();for(Textvalue:values)fileList+=value.toString()+;result.set(fileList);context.write(key,result

11、);publicstaticvoidmain(Stringargs)tryConfigurationconfiguration=newConfiguration();/这句话很关键/*Stringioargs=newStringindex_in,index_out;if(args.length=2)ioargs=args;StringotherArgs=newGenericOptionsParser(configuration,ioargs).getRemainingArgs();if(otherArgs.length!=2)System.err.println(Usage:inverted+

12、Invertedindex.class.getSimpleName();System.exit(2);*/args=newString/intputpath“hdfs:hadoop-master:9000/user/hadoop/index_in,/outputpath“hdfs:hadoop-master:9000/user/hadoop/index_out;/启动计算任务Jobjob=newJob(configuration,Invertedindex.class.getSimpleName();job.setJarByClass(Invertedindex.class);/映射job.setMapperClass(Map.class);/合成job.setCombinerClass(Combine.class);/规约job.setReducerClass(Reduce.class);/设置映射Map输出类型job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(Text.class);/设置reduce规约输出类型job.setOutputKeyClass(Text.job.setOutputValueClass(Text./

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论