使用MapReduce实现简单的倒排索引

package InvertedIndexer;


import java.io.IOException;


/*
 * 简单的倒排索引
 * Map的输出是
 * (word,filename#偏移地址)
 * Reduce的过程就是
 * (word,fielename#偏移地址,filename#偏移地址)
 * */
public class SimpleInvertedIndex {
/** 对文本进行处理,得到<word,filename#offset>格式的键值对输出,从而得到一个单词在文档中出现的位置 **/
public static class InvertedIndexMapper extends
Mapper<Object, Text, Text, Text> {
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
FileSplit fileSplit = (FileSplit) context.getInputSplit();
String fileName = fileSplit.getPath().getName(); // 得到文件名
Text word = new Text();
Text fileName_lineOffset = new Text(fileName + "#" + key.toString());
StringTokenizer itr = new StringTokenizer(value.toString());
for (; itr.hasMoreTokens();) {
word.set(itr.nextToken());
context.write(word, fileName_lineOffset);
}
}
}


/** 从Mapper处得到的内容,根据相同key值,进行累加处理,输出该单词所有出现的文档位置 **/
public static class InvertedIndexReducer extends
Reducer<Text, Text, Text, Text> {
public void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
Iterator<Text> it = values.iterator();
StringBuilder all = new StringBuilder();
if (it.hasNext())
all.append(it.next().toString());
for (; it.hasNext();) {
all.append(";");
all.append(it.next().toString());
}
context.write(key, new Text(all.toString()));
} // 最终输出键值对示例:("fish", "doc1#0; doc1#8;doc2#0;doc2#8 ")
}


public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = new Job(conf, "invert index");
job.setJarByClass(SimpleInvertedIndex.class);
job.setInputFormatClass(TextInputFormat.class);
job.setMapperClass(InvertedIndexMapper.class);
job.setReducerClass(InvertedIndexReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值