Hadoop之道--MapReduce简单应用倒排索引(InversedIndex)

Hadoop版本:1.1.2

集成开发平台:Eclipse SDK 3.5.1

原创作品:http://blog.csdn.net/yming0221/article/details/9024419


倒排索引(Inverted index),也常被称为反向索引、置入档案或反向档案,是一种索引方法,被用来存储在全文搜索下某个单词在一个文档或者一组文档中的存储位置的映射。它是文档检索系统中最常用的数据结构。通过倒排索引,可以根据单词快速获取包含这个单词的文档列表。

      倒排索引源于实际应用中需要根据属性的值来查找记录。这种索引表中的每一项都包括一个属性值和具有该属性值的各记录的地址。由于不是由记录来确定属性值,而是由属性值来确定记录的位置,因而称为倒排索引。

Hadoop代码:

[java] view plain copy
  1. import java.io.IOException;  
  2. import java.util.StringTokenizer;  
  3.   
  4. import org.apache.hadoop.conf.Configuration;  
  5. import org.apache.hadoop.fs.Path;  
  6. import org.apache.hadoop.io.Text;  
  7. import org.apache.hadoop.mapreduce.Job;  
  8. import org.apache.hadoop.mapreduce.Mapper;  
  9. import org.apache.hadoop.mapreduce.Reducer;  
  10. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  11. import org.apache.hadoop.mapreduce.lib.input.FileSplit;  
  12. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  13.   
  14. public class InversedIndex {  
  15.       
  16.     /** 
  17.      * 将输入文件拆分, 
  18.      * 将关键字和关键字所在的文件名作为map的key输出, 
  19.      * 该组合的频率作为value输出 
  20.      * */  
  21.       
  22.     public static class InversedIndexMapper extends Mapper<Object, Text, Text, Text> {  
  23.           
  24.         private Text outKey = new Text();  
  25.         private Text outVal = new Text();  
  26.           
  27.         @Override  
  28.         public void map (Object key,Text value,Context context) {  
  29.             StringTokenizer tokens = new StringTokenizer(value.toString());  
  30.             FileSplit split = (FileSplit) context.getInputSplit();  
  31.             while(tokens.hasMoreTokens()) {  
  32.                 String token = tokens.nextToken();  
  33.                 try {  
  34.                     outKey.set(token + ":" + split.getPath());  
  35.                     outVal.set("1");  
  36.                     context.write(outKey, outVal);  
  37.                 } catch (IOException e) {  
  38.                     e.printStackTrace();  
  39.                 } catch (InterruptedException e) {  
  40.                     e.printStackTrace();  
  41.                 }  
  42.             }  
  43.         }  
  44.     }  
  45.       
  46.     /** 
  47.      * map的输出进入到combiner阶段,此时来自同一个文件的相同关键字进行一次reduce处理, 
  48.      * 将输入的key拆分成关键字和文件名,然后关键字作为输出key, 
  49.      * 将文件名与词频拼接,作为输出value, 
  50.      * 这样就形成了一个关键字,在某一文件中出现的频率的 key--value 对 
  51.      * */  
  52.     public static class InversedIndexCombiner extends Reducer<Text, Text, Text, Text> {  
  53.           
  54.         private Text outKey = new Text();  
  55.         private Text outVal = new Text();  
  56.           
  57.         @Override  
  58.         public void reduce(Text key,Iterable<Text> values,Context context) {  
  59.             String[] keys = key.toString().split(":");  
  60.             int sum = 0;  
  61.             for(Text val : values) {  
  62.                 sum += Integer.parseInt(val.toString());  
  63.             }  
  64.             try {  
  65.                 outKey.set(keys[0]);  
  66.                 int index = keys[keys.length-1].lastIndexOf('/');  
  67.                 outVal.set(keys[keys.length-1].substring(index+1) + ":" + sum);  
  68.                 context.write(outKey, outVal);  
  69.             } catch (IOException e) {  
  70.                 e.printStackTrace();  
  71.             } catch (InterruptedException e) {  
  72.                 e.printStackTrace();  
  73.             }  
  74.         }  
  75.           
  76.     }  
  77.       
  78.     /** 
  79.      * 将combiner后的key value对进行reduce, 
  80.      * 由于combiner之后,一个关键字可能对应了多个value,故需要将这些value进行合并输出 
  81.      * */  
  82.       
  83.     public static class InversedIndexReducer extends Reducer<Text, Text, Text, Text> {  
  84.           
  85.         @Override  
  86.         public void reduce (Text key,Iterable<Text> values,Context context) {  
  87.             StringBuffer sb = new StringBuffer();  
  88.             for(Text text : values) {  
  89.                 sb.append(text.toString() + " ,");  
  90.             }  
  91.             try {  
  92.                 context.write(key, new Text(sb.toString()));  
  93.             } catch (IOException e) {  
  94.                 e.printStackTrace();  
  95.             } catch (InterruptedException e) {  
  96.                 e.printStackTrace();  
  97.             }  
  98.         }  
  99.     }  
  100.       
  101.     public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {  
  102.         Configuration conf = new Configuration();  
  103.         Job job = new Job(conf,"index inversed");  
  104.           
  105.         job.setJarByClass(InversedIndex.class);  
  106.         job.setMapperClass(InversedIndexMapper.class);  
  107.         job.setCombinerClass(InversedIndexCombiner.class);  
  108.         job.setReducerClass(InversedIndexReducer.class);  
  109.         job.setMapOutputKeyClass(Text.class);  
  110.         job.setMapOutputValueClass(Text.class);  
  111.         job.setOutputKeyClass(Text.class);  
  112.         job.setOutputValueClass(Text.class);  
  113.   
  114.         job.setNumReduceTasks(3);  
  115.           
  116.         FileInputFormat.addInputPath(job, new Path("input"));  
  117.         FileOutputFormat.setOutputPath(job, new Path("output"));  
  118.           
  119.         System.exit(job.waitForCompletion(true)?0:1);  
  120.           
  121.     }  
  122.   
  123. }  

原文本文件:

text1.txt

[plain] view plain copy
  1. MapReduce is sample  
text2.txt
[plain] view plain copy
  1. MapReduce is powerful is sample  
text3.txt
[plain] view plain copy
  1. Hello MapReduce hello world  

运行结果文件:
[plain] view plain copy
  1. Hello   text3.txt:1 ,  
  2. MapReduce   text3.txt:1 ,text1.txt:1 ,text2.txt:1 ,  
  3. hello   text3.txt:1 ,  
  4. is  text2.txt:2 ,text1.txt:1 ,  
  5. powerful    text2.txt:1 ,  
  6. sample  text2.txt:1 ,text1.txt:1 ,  
  7. world   text3.txt:1 ,  



过程分析:

1、Map过程:

首先是使用默认的TextInputFormat类对文本进行处理,得到每行的偏移量和每行的内容<key,value>,然后Map对每行进行单词分割并设置value为1,得到<key1,value1>,这里key1是单词以及该单词所在的这个文件的URI,value1就是1,表示这个单词出现过1次。


2、Combine过程,完成词频的统计。

该过程对Mapper输出的信息先对每个文档的单词排序<key2,list(value2)>,并对相同的单词计数进行累加,得到<key3,value3>,完成同文档的词频的统计。然后将key和value进行拆分,将单词作为key,将单词所在文档的URI和在本文档中出现的次数作为value,得到<key4,value4>。


3、Reduce过程,完成对不同文档中的相同的单词以及value值进行输出。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值