你好,游客 登录 注册 搜索
背景:
阅读新闻

MapReduce在倒排索引中练习

[日期:2014-04-08] 来源:Linux社区  作者:momomi [字体:大 中 小]

倒排索引是文件检索系统中常用的数据结构,被广泛应用于全文章搜索引擎。
 
通常情况下,倒排索引由一个单词或词组以及相关的文档列表组成,文档列表中的文档或者是标识文档的ID号,或者是指定文档所在位置的URI;
 
在实际应用中,往往还需要给每个文档加一个权值,用来指出每个文档与搜索内容的相关度;
 
我的例子中,文档内容如下:
 
Hadoop11:/home/in/win1 # hadoop fs -cat /user/root/in1/words.txt
 mapreduce is simple
 hadoop11:/home/in/win1 # hadoop fs -cat /user/root/in1/words1.txt
 mapreduce is powerfull and is simple
 hadoop11:/home/in/win1 # hadoop fs -cat /user/root/in1/words2.txt
 cat: File does not exist: /user/root/in1/words2.txt
 
我的目标结果:
 
and words1.txt:1;
 bye words3.txt:1;
 hello words3.txt:1;
 is words.txt:1;words1.txt:2;
 mapreduce words1.txt:1;words3.txt:2;words.txt:1;
 powerfull words1.txt:1;
 simple words1.txt:1;words.txt:1;
 
代码清单,自己import吧:
 
 
 
public class indexSum {
  private static final Log log = LogFactory.getLog(LogCount.class);
 
 
 
 public static class TokenizerMapper1 extends
    Mapper<Object, Text, Text, Text> {
 
  private Text key1 = new Text();
  private Text value1 = new Text();
 
  public void map(Object key, Text value, Context context)
    throws IOException, InterruptedException {
 
  FileSplit split = (FileSplit) context.getInputSplit();
 
  StringTokenizer itr = new StringTokenizer(value.toString());
 
  while (itr.hasMoreTokens()) {
    key1.set(itr.nextToken() + ":"
      + split.getPath().getName().toString());
 
    value1.set("1");
 
    context.write(key1, value1);
    }
  }
  }
 
 public static class IntSumReducer extends Reducer<Text, Text, Text, Text> {
  private Text result = new Text();
 
  public void reduce(Text key, Iterable<Text> values, Context context)
    throws IOException, InterruptedException {
    int sum = 0;
 
  for (Text val : values) {
    sum += Integer.parseInt(val.toString());
    }
    int splitIndex = key.toString().indexOf(":");
    result.set(key.toString().substring(splitIndex + 1) + ":" + sum);
    key.set(key.toString().substring(0, splitIndex));
    context.write(key, result);
  }
 
 }
 
 public static class IntSumReducer3 extends Reducer<Text, Text, Text, Text> {
  private Text result = new Text();
 
  public void reduce(Text key, Iterable<Text> values, Context context)
    throws IOException, InterruptedException {
 
  String valueArray = new String();
    for (Text value : values) {
    valueArray += value.toString() + ";";
    }
 
  result.set(valueArray);
 
  context.write(key, result);
  }
 
 }
 
 
 
 public static void main(String[] args) throws Exception {
  log.info("===============Begin at : " + new Date());
 
  String[] argsTemp = { "hdfs://hadoop11:8020/user/root/in1", "out4" };
  args = argsTemp;
 
  File jarFile = EJob.createTempJar("bin");
  Configuration conf = new Configuration();
  conf.set("hadoop.job.ugi", "root,root");
  conf.set("fs.default.name", "hdfs://hadoop11:8020/");
  conf.set("mapred.job.tracker", "hadoop11:8021");
 
  String[] otherArgs = new GenericOptionsParser(conf, args)
    .getRemainingArgs();
  if (otherArgs.length != 2) {
    System.err.println("Usage: wordcount <in> <out>");
    System.exit(2);
  }
  Job job = new Job(conf, "word count");
  job.setJarByClass(LogCount.class);
  ((JobConf) job.getConfiguration()).setJar(jarFile.toString());
 
 
  job.setMapperClass(TokenizerMapper1.class);
  job.setCombinerClass(IntSumReducer.class);
 
  job.setReducerClass(IntSumReducer3.class);
 
  FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
  FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
 
  job.setOutputKeyClass(Text.class);
  job.setOutputValueClass(Text.class);
 
  System.exit(job.waitForCompletion(true) ? 0 : 1);
 
  log.info("===============End at : " + new Date());
  }
 } 

相关阅读:

Ubuntu 13.04上搭建Hadoop环境 http://www.linuxidc.com/Linux/2013-06/86106.htm

Ubuntu 12.10 +Hadoop 1.2.1版本集群配置 http://www.linuxidc.com/Linux/2013-09/90600.htm

Ubuntu上搭建Hadoop环境(单机模式+伪分布模式) http://www.linuxidc.com/Linux/2013-01/77681.htm

Ubuntu下Hadoop环境的配置 http://www.linuxidc.com/Linux/2012-11/74539.htm

单机版搭建Hadoop环境图文教程详解 http://www.linuxidc.com/Linux/2012-02/53927.htm

搭建Hadoop环境(在Winodws环境下用虚拟机虚拟两个Ubuntu系统进行搭建) http://www.linuxidc.com/Linux/2011-12/48894.htm
 
 
参考apache官网的例子;

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款