Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1080147
  • 博文数量: 143
  • 博客积分: 969
  • 博客等级: 准尉
  • 技术积分: 1765
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-30 12:09
文章分类

全部博文(143)

文章存档

2023年(4)

2021年(2)

2020年(4)

2019年(4)

2018年(33)

2017年(6)

2016年(13)

2014年(7)

2013年(23)

2012年(33)

2011年(14)

我的朋友

分类: 大数据

2018-06-23 15:18:39

       倒排索引是文档检索系统中最常用的数据结构,被广泛用于全文搜索引擎。它主要是用来存储某个单词(或词组)在一个文档或一组文档的存储位置的映射,即提供了一种根据内容来查找文档的方式。由于不是根据文档来确定文档所包含的内容,而是进行了相反的操作(根据关键字来查找文档),因而称为倒排索引(Inverted Index)。通常情况下,倒排索引由一个单词(词组)以及相关的文档列表(标示文档的ID号,或者是指定文档所在位置的URI)组成。

    注意点:
   1、输入数据是个目录
   2、在map中需要获取key所属文档路径或者名称


点击(此处)折叠或打开

  1. import org.apache.hadoop.fs.Path;
  2. import org.apache.hadoop.io.IntWritable;
  3. import org.apache.hadoop.io.LongWritable;
  4. import org.apache.hadoop.io.NullWritable;
  5. import org.apache.hadoop.io.Text;
  6. import org.apache.hadoop.mapreduce.Job;
  7. import org.apache.hadoop.mapreduce.Mapper;
  8. import org.apache.hadoop.mapreduce.Reducer;
  9. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  10. import org.apache.hadoop.mapreduce.lib.input.FileSplit;
  11. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  12. import org.apache.hadoop.util.GenericOptionsParser;

  13. import java.io.IOException;
  14. import java.util.StringTokenizer;

  15. public class InvertedIndexJob {

  16.     public static class InvertedIndexMapper extends Mapper<LongWritable, Text, Text,IntWritable>{

  17.         @Override
  18.         public void map(LongWritable key, Text text, Context context) throws IOException, InterruptedException {

  19.             String data= text.toString();
  20.             FileSplit inputSplit = (FileSplit) context.getInputSplit();
  21.             String fileName = inputSplit.getPath().getName();
  22.             StringTokenizer strToken = new StringTokenizer(data);
  23.             while (strToken.hasMoreTokens()){
  24.                 context.write(new Text(strToken.nextToken()+":"+fileName), new IntWritable(1));
  25.             }
  26.         }
  27.     }

  28.     public static class InvertedIndexReduce extends Reducer<Text,IntWritable, NullWritable, Text>{
  29.         @Override
  30.         public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
  31.             int count=0;
  32.             for (IntWritable value: values){
  33.                 count++;
  34.             }
  35.             context.write(NullWritable.get(), new Text( key.toString() +"\t"+ String.valueOf(count)));
  36.         }
  37.     }

  38.     public static void main(String []args){

  39.         Job job = null;
  40.         try {
  41.             job = Job.getInstance();
  42.             job.setJobName("InvertedIndexJob");
  43.             job.setJarByClass(InvertedIndexJob.class);
  44.             job.setMapperClass(InvertedIndexMapper.class);
  45.             job.setMapOutputKeyClass(Text.class);
  46.             job.setMapOutputValueClass(IntWritable.class);
  47.             job.setReducerClass(InvertedIndexReduce.class);
  48.             job.setOutputKeyClass(NullWritable.class);
  49.             job.setOutputValueClass(Text.class);
  50.             job.setNumReduceTasks(1);

  51.             String []arrays = new GenericOptionsParser(args).getRemainingArgs();
  52.             FileInputFormat.setInputPaths(job, new Path(arrays[0]));
  53.             FileOutputFormat.setOutputPath(job,new Path(arrays[1]));

  54.             System.out.println(job.waitForCompletion(true));

  55.         } catch (IOException e) {
  56.             e.printStackTrace();
  57.         } catch (InterruptedException e) {
  58.             e.printStackTrace();
  59.         } catch (ClassNotFoundException e) {
  60.             e.printStackTrace();
  61.         }

  62.     }
  63. }

阅读(976) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~