hadoop学习-倒排索引

来源:互联网 发布:文具淘宝店铺 编辑:程序博客网 时间:2024/06/01 19:21

倒排索引是文档搜索系统中常用的数据结构。它主要用来存储某个词组在一个或多个文档中的位置映射。通常情况下,倒排索引由词组以及相关的文档列表组成。如下表所示。

表1:

  单词      文档列表

单词1文档1文档2文档3单词2文档2文档4文档5单词3文档3文档5文档6倒排索引


从表1可以看出单词1出现在{文档1,文档2,文档3},单词2出现在{文档2,文档4,文档5},单词3出现在{文档3,文档5,文档6}

实际使用中还需要给文档添加一个权值,用来表示该词组与文档的相关性。如表2所示。

表2:

单词     文档列表

单词1文档1权文档2权文档3权单词2文档2权文档4权文档5权单词3文档3权文档5权文档6权添加权重的倒排索引

这里的权重,一般可以使用词频,即记录词组在文档中出现的次数。更复杂的权重可以使用TF-IDF算法等等。

本例子以词频为权重,使用MapReduce来实现倒排索引。

举个例子:

现有2个文件1.txt,2.txt,其内容分别是:

1.txt

hello world

2.txt

hello hadoop

则相应的倒排索引:

"hello":           1.txt,1;2.txt,1

"world":          1.txt,1

“hadoop":      2.txt,1

 下面介绍下mapreduce实现过程:

1、Map

在倒排索引中需要3个信息,词组、来源文档、词频

因此在,Map阶段我们需要得到:

1.txt

hello world                      -------->>    hello          1.txt          1

                                       -------->>    world         1.txt          1

2.txt

hello hadoop                  -------->>    hello          2.txt          1

                                       -------->>    hadoop     2.txt          1

这里的map结果有3个值,而<key,value>只有2个值,为了简便处理,不使用hadoop自定义数据类型。我们对其中的2个值合并成一个

以<"hello:1.txt,1">作为<key,value>输出到Combine过程。

2、Combine

Combine过程将key值相同的value值累加,计算同一个词组在一个文档中的词频。

除了对其累加,还需要对key值进行拆分。将文档来源和词频合并在一起。如下所示。

hello:1.txt,1             -------->>    hello         1.txt:1

world:1.txt,1            -------->>    world        1.txt:1

hello:2.txt,1             -------->>    hello         2.txt:1

hadoop:2.txt,1        -------->>    hadoop    2.txt:1

<hadoop    2.txt:1>作为<key,value>输出

3、Reduce

在Reduce 只需要对相同key值的value相加即可。

如 hello 1.txt:1;2.txt:1

代码:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. import java.io.IOException;  
  2. import java.util.Iterator;  
  3. import java.util.StringTokenizer;  
  4.   
  5. import org.apache.hadoop.conf.Configuration;  
  6. import org.apache.hadoop.conf.Configured;  
  7. import org.apache.hadoop.fs.Path;  
  8. import org.apache.hadoop.io.Text;  
  9. import org.apache.hadoop.mapred.FileInputFormat;  
  10. import org.apache.hadoop.mapred.FileOutputFormat;  
  11. import org.apache.hadoop.mapred.JobClient;  
  12. import org.apache.hadoop.mapred.JobConf;  
  13. import org.apache.hadoop.mapred.KeyValueTextInputFormat;  
  14. import org.apache.hadoop.mapred.MapReduceBase;  
  15. import org.apache.hadoop.mapred.Mapper;  
  16. import org.apache.hadoop.mapred.OutputCollector;  
  17. import org.apache.hadoop.mapred.Reducer;  
  18. import org.apache.hadoop.mapred.Reporter;  
  19. import org.apache.hadoop.mapred.*;  
  20. import org.apache.hadoop.mapred.TextOutputFormat;  
  21. import org.apache.hadoop.util.Tool;  
  22. import org.apache.hadoop.util.ToolRunner;  
  23. import org.apache.hadoop.io.Writable;  
  24. import org.apache.hadoop.io.IntWritable;  
  25. import org.apache.hadoop.io.*;  
  26.   
  27. public class InvertedIndex2 extends Configured implements Tool {  
  28.       
  29.     public static class MapClass extends MapReduceBase  
  30.         implements Mapper<LongWritable, Text, Text, Text> {  
  31.         private Text keyInfo = new Text();  
  32.         private Text valueInfo = new Text();  
  33.         private FileSplit split;  
  34.         public void map(LongWritable key, Text value,  
  35.                         OutputCollector<Text, Text> output,  
  36.                         Reporter reporter) throws IOException {  
  37.                         split = (FileSplit)reporter.getInputSplit();  
  38.                         String line = value.toString();  
  39.             StringTokenizer itr = new StringTokenizer(line);  
  40.               
  41.             while (itr.hasMoreTokens()){  
  42.                 keyInfo.set(itr.nextToken() + ":" + split.getPath().getName().toString());  
  43.                 valueInfo.set("1");  
  44.                 output.collect(keyInfo,valueInfo);  
  45.             }  
  46.         }  
  47.     }  
  48.       
  49.     public static class Combine extends MapReduceBase  
  50.         implements Reducer<Text, Text, Text, Text> {  
  51.         private Text info = new Text();  
  52.         public void reduce(Text key, Iterator<Text> values,  
  53.                            OutputCollector<Text, Text> output,  
  54.                            Reporter reporter) throws IOException {  
  55.                              
  56.             int sum = 0;  
  57.             while (values.hasNext()) {  
  58.                 sum = sum + Integer.parseInt(values.next().toString());  
  59.             }  
  60.             int splitIndex = key.toString().indexOf(":");  
  61.             info.set(key.toString().substring(splitIndex+1)+":"+sum);  
  62.             key.set(key.toString().substring(0,splitIndex));  
  63.             output.collect(key, info);  
  64.         }  
  65.     }  
  66.       
  67.     public static class Reduce extends MapReduceBase  
  68.         implements Reducer<Text, Text, Text, Text> {  
  69.         private Text result = new Text();  
  70.         public void reduce(Text key, Iterator<Text> values,  
  71.                            OutputCollector<Text, Text> output,  
  72.                            Reporter reporter) throws IOException {  
  73.             String strtemp = new String();  
  74.             //String csv = "";  
  75.             //int count = 0;  
  76.             while (values.hasNext()) {  
  77.                 strtemp += values.next().toString() + ";";  
  78.             }  
  79.             result.set(strtemp);  
  80.             output.collect(key, result);  
  81.         }  
  82.     }  
  83.       
  84.     public int run(String[] args) throws Exception {  
  85.         Configuration conf = getConf();  
  86.           
  87.         JobConf job = new JobConf(conf, InvertedIndex2.class);  
  88.           
  89.         //Path in = new Path(args[0]);  
  90.         //Path out = new Path(args[1]);  
  91.         FileInputFormat.addInputPath(job, new Path(args[0]));  
  92.         FileInputFormat.addInputPath(job, new Path(args[1]));  
  93.         FileOutputFormat.setOutputPath(job, new Path(args[2]));  
  94.         //FileInputFormat.setInputPaths(job, in);  
  95.         //FileOutputFormat.setOutputPath(job, out);  
  96.         job.setJobName("InveredIndex2");  
  97.         job.setMapperClass(MapClass.class);  
  98.         job.setCombinerClass(Combine.class);  
  99.         job.setReducerClass(Reduce.class);  
  100.           
  101.         job.setInputFormat(TextInputFormat.class);  
  102.         job.setOutputFormat(TextOutputFormat.class);  
  103.         job.setOutputKeyClass(Text.class);  
  104.         job.setOutputValueClass(Text.class);  
  105.         //job.set("key.value.separator.in.input.line", ",");  
  106.           
  107.         JobClient.runJob(job);  
  108.           
  109.         return 0;  
  110.     }  
  111.       
  112.     public static void main(String[] args) throws Exception {   
  113.         int res = ToolRunner.run(new Configuration(), new InvertedIndex2(), args);  
  114.           
  115.         System.exit(res);  
  116.     }  
  117. }  
运行结果:



参考资料:
《hadoop-开启通向云计算的捷径》(刘鹏)

关于 hadoop reduce 阶段遍历 Iterable 的 2 个“坑”

0 0
原创粉丝点击