欢迎投稿

今日深度:

hadoop 倒排索引,

hadoop 倒排索引,




原文:http://blog.csdn.net/ludengji/article/details/6872664
[java] view plaincopy
  1. 倒排索引是文档检索系统中最常用的数据结构,被广泛的应用于全文搜索引擎。它主要用来存储某个单词(或词组),在一个文档或一组文档中的存储位置的映射,即提供了一种根据内容来查找文档的方式,由于不是根据文档来确定文档所包含的内容,而是进行了相反的操作,因而被称为倒排索引。  
  2. 这里感觉有点问题。就是在一个mapper里,处理来自同一个文件的内容才可以。否则combiner处理不正确。网上查了资料,如是说:我们知道InputFormat决定着InputSplit,每个InputSplit会分配给一个单独的Mapper,因此InputFormat决定了具体的Map task数量。 如果是这样子,就正确。但是如何知道到底有多少个map task?不清楚...
[java] view plaincopy
  1. 假设在inversed.files中有file1,file2,file3文件,其内容分别如下:  
[java] view plaincopy
  1. file1:  
[java] view plaincopy
  1. dog cat  
  2. dog rabbit  
  3. tiger mice  
  4. goose chicken  
  5. rabbit fox  
[java] view plaincopy
  1. file2:  
[java] view plaincopy
  1. tiger donkey  
  2. lion fish  
  3. duck wolf  
  4. dog bird  
  5. cat bear  
[java] view plaincopy
  1. file3:  
[java] view plaincopy
  1. snake pig  
  2. lion  
  3. cat  
  4. elephant  
[java] view plaincopy
  1. 则进行倒排索引之后,其结果为:  
[java] view plaincopy
  1.   

bear file2:1 ,

bird file2:1 ,

cat file2:1 ,file1:1 ,file3:1 ,

chicken file1:1 ,

dogfile1:2 ,file2:1 ,duckfile2:1 ,dungkeyfile2:1 ,elephantfile3:1 ,fishfile2:1 ,foxfile1:1 ,goosefile1:1 ,lionfile2:1 ,file3:1 ,micefile1:1 ,pigfile3:1 ,rabbitfile1:2 ,snakefile3:1 ,tigerfile2:1 ,file1:1 ,wolffile2:1 , [java] view plaincopy
  1.   
[java] view plaincopy
  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.         FileInputFormat.addInputPath(job, new Path("inversed.files"));  
  115.         FileOutputFormat.setOutputPath(job, new Path("inversed.result"));  
  116.           
  117.         System.exit(job.waitForCompletion(true)?0:1);  
  118.           
  119.     }  
  120.   
  121. }  

www.htsjk.Com true http://www.htsjk.com/Hadoop/41794.html NewsArticle hadoop 倒排索引, 原文:http://blog.csdn.net/ludengji/article/details/6872664 [java]  view plaincopy 倒排索引是文档检索系统中最常用的数据结构,被广泛的应用于全文搜索引擎。它主要用来存储某个...
相关文章
    暂无相关文章
评论暂时关闭