MapReduce编程之倒排索引的实现

倒排索引:由于不是根据文档来确定文档所包含的内容,而是进行相反的操作,因而称为倒排索引

--------------------------------

map

输出:

key:单词+文档URI,

value:词频

-------------------------------

combiner

输入:

key:单词+文档URI,

value:词频

输出:

key:单词+文档URI,

value,单个文档的中出现的词频汇总就是map的value-list合并的结果

---------------------------------------

Reduce:

输入:
combiner的结果key  value-list

输出

key:单词

value:文档URI:词频

----------------------------------------------------------

源代码

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

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;


public class InvertedIndex {
    
    public static class InvertedIndexMap extends Mapper<Object,Text,Text,Text>{
        
        private Text valueInfo = new Text();
        private Text keyInfo = new Text();
        private FileSplit split;
        
        public void map(Object key, Text value,Context context)
                throws IOException, InterruptedException {
            //获取<key value>对所属的FileSplit对象
            split = (FileSplit) context.getInputSplit();
            StringTokenizer stk = new StringTokenizer(value.toString());
            while (stk.hasMoreElements()) {
                //key值由(单词:URI)组成
                keyInfo.set(stk.nextToken()+":"+split.getPath().toString());
                //词频
                valueInfo.set("1");
                context.write(keyInfo, valueInfo);
                
            }
            
            
        }
    }
    
    public static class InvertedIndexCombiner extends Reducer<Text,Text,Text,Text>{
        
        Text info = new Text();

        public void reduce(Text key, Iterable<Text> values,Context contex)
                throws IOException, InterruptedException {
            int sum = 0;
            for (Text value : values) {
                sum += Integer.parseInt(value.toString());
            }
            
            int splitIndex = key.toString().indexOf(":");
            //重新设置value值由(URI+:词频组成)
            info.set(key.toString().substring(splitIndex+1) +":"+ sum);
            //重新设置key值为单词
            key.set(key.toString().substring(0,splitIndex));
            contex.write(key, info);
        }
    }
    
    public static class InvertedIndexReduce extends Reducer<Text,Text,Text,Text>{
        
        private Text result = new Text();
        
        public void reduce(Text key, Iterable<Text> values,Context contex)
                throws IOException, InterruptedException {
            //生成文档列表
            String fileList = new String();
            for (Text value : values) {
                fileList += value.toString()+";";
            }
            result.set(fileList);
            contex.write(key, result);
        }
    }
    
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        
        Configuration conf = new Configuration();
        
        Job job = new Job(conf,"InvertedIndex");
        
        job.setJarByClass(InvertedIndex.class);
        
        job.setMapperClass(InvertedIndexMap.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);
        
        job.setCombinerClass(InvertedIndexCombiner.class);
        
        job.setReducerClass(InvertedIndexReduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        
        FileInputFormat.addInputPath(job, new Path("./in/invertedindex/"));
        FileOutputFormat.setOutputPath(job, new Path("./out/"));
        
        System.exit(job.waitForCompletion(true)?0:1);
        
        
    }
}

测试文件:

1.txt:

Hello MapReduce Hello Hadoop

2.txt

Hello MapReduce Hello Hadoop

Hello MapReduce Hello Hadoop

3.txt

Hello MapReduce Hello Hadoop

Hello MapReduce Hello Hadoop

Hello MapReduce Hello Hadoop

part-r-00000

Hadoop    file:/E:/KAI_FA/eclipse/workshop/InvertedIndex/in/invertedindex/1.txt:1;file:/E:/KAI_FA/eclipse/workshop/InvertedIndex/in/invertedindex/2.txt:2;file:/E:/KAI_FA/eclipse/workshop/InvertedIndex/in/invertedindex/3.txt:3;
Hello    file:/E:/KAI_FA/eclipse/workshop/InvertedIndex/in/invertedindex/3.txt:6;file:/E:/KAI_FA/eclipse/workshop/InvertedIndex/in/invertedindex/1.txt:2;file:/E:/KAI_FA/eclipse/workshop/InvertedIndex/in/invertedindex/2.txt:4;
MapReduce    file:/E:/KAI_FA/eclipse/workshop/InvertedIndex/in/invertedindex/2.txt:2;file:/E:/KAI_FA/eclipse/workshop/InvertedIndex/in/invertedindex/1.txt:1;file:/E:/KAI_FA/eclipse/workshop/InvertedIndex/in/invertedindex/3.txt:3;


  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MapReduce编程中,倒排索引是一种常见的应用案例。倒排索引(Inverted index)是一种索引方法,用于存储某个单词在一个文档或一组文档中的位置映射。它是文档检索系统中最常用的数据结构。 在实现倒排索引MapReduce程序中,首先需要设置MapReduce工作任务的相关参数,比如输入路径和输出路径。然后,需要编写自定义的Mapper类,将文本中的单词按照空格进行切割,并将“单词:文档名称”作为key,单词次数作为value输出。接着,在Map阶段的输出结果形式基础上,可以编写自定义的Combiner类,对每个文档的单词进行词频统计。 具体实现过程中,可以使用Eclipse等开发工具打开项目,并按照指定的路径和格式进行输入和输出。在Mapper类中,可以使用split函数对文本进行切割,并使用context.write函数将结果输出。在Combiner类中,可以对每个文档的单词进行统计,并输出结果。最后,通过在MapReduce程序中指定输入路径和输出路径,运行程序即可得到倒排索引的结果。 总结起来,实现倒排索引MapReduce程序包括设置任务参数、编写Mapper类、编写Combiner类,并按照指定的输入和输出路径运行程序。通过这样的实现,可以将文档中的单词按照索引形式进行存储,方便后续的全文搜索等操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值