Mapreduce实例(六):倒排索引

大家好,我是风云,欢迎大家关注我的博客 或者 微信公众号【笑看风云路】,在未来的日子里我们一起来学习大数据相关的技术,一起努力奋斗,遇见更好的自己!

倒排索引原理

  • "倒排索引"是文档检索系统中最常用的数据结构,被广泛地应用于全文搜索引擎。
  • 它主要是用来存储某个单词(或词组)在一个文档或一组文档中的存储位置的映射,即提供了一种根据"内容来查找文档"的方式。由于不是根据"文档来确定文档所包含"的内容,而是进行相反的操作,因而被称为倒排索引(Inverted Index)
  • 实现"倒排索引"主要关注的信息为:单词、文档URL及词频

倒排索引主要是用来存储某个单词(或词组)在一个文档或一组文档中的存储位置的映射,即提供了一种根据"内容来查找文档"的方式。

实现思路

根据MapReduce的处理过程给出倒排索引的设计思路:

(1)Map过程

首先使用默认的TextInputFormat类对输入文件进行处理,得到文本中每行的偏移量及其内容。显然,Map过程首先必须分析输入的<key,value>对,得到倒排索引中需要的三个信息:单词、文档URL和词频,接着我们对读入的数据利用Map操作进行预处理,如下图所示:

img

这里存在两个问题:
第一,<key,value>对只能有两个值,在不使用Hadoop自定义数据类型的情况下,需要根据情况将其中两个值合并成一个值,作为key或value值。
第二,通过一个Reduce过程无法同时完成词频统计和生成文档列表,所以必须增加一个Combine过程完成词频统计

这里将商品ID和URL组成key值(如"1024600:goods3"),将词频(商品ID出现次数)作为value,这样做的好处是可以利用MapReduce框架自带的Map端排序,将同一文档的相同单词的词频组成列表,传递给Combine过程,实现类似于WordCount的功能。

(2)Combine过程

经过map方法处理后,Combine过程将key值相同的value值累加,得到一个单词在文档中的词频,如下图所示。如果直接将下图所示的输出作为Reduce过程的输入,在Shuffle过程时将面临一个问题:所有具有相同单词的记录(由单词、URL和词频组成)应该交由同一个Reducer处理,但当前的key值无法保证这一点,所以必须修改key值和value值。这次将单词(商品ID)作为key值,URL和词频组成value值(如"goods3:1")。这样做的好处是可以利用MapReduce框架默认的HashPartitioner类完成Shuffle过程,将相同单词的所有记录发送给同一个Reducer进行处理。

img

(3)Reduce过程

经过上述两个过程后,Reduce过程只需将相同key值的所有value值组合成倒排索引文件所需的格式即可,剩下的事情就可以直接交给MapReduce框架进行处理了。如下图所示

img

代码编写

Map代码

首先使用默认的TextInputFormat类对输入文件进行处理,得到文本中每行的偏移量及其内容。显然,Map过程首先必须分析输入的<key,value>对,得到倒排索引中需要的三个信息:单词、文档URL和词频,这里存在两个问题:第一,<key,value>对只能有两个值,在不使用Hadoop自定义数据类型的情况下,需要根据情况将其中两个值合并成一个值,作为key或value值。第二,通过一个Reduce过程无法同时完成词频统计和生成文档列表,所以必须增加一个Combine过程完成词频统计。

public static class doMapper extends Mapper<Object, Text, Text, Text>{  
    public static Text myKey = new Text();   // 存储单词和URL组合  
    public static Text myValue = new Text();  // 存储词频  
    //private FileSplit filePath;     // 存储Split对象  
  
    @Override   // 实现map函数  
    protected void map(Object key, Text value, Context context)  
      throws IOException, InterruptedException {  
      String filePath=((FileSplit)context.getInputSplit()).getPath().toString();  
      if(filePath.contains("goods")){  
        String val[]=value.toString().split("\t");  
        int splitIndex =filePath.indexOf("goods");  
        myKey.set(val[0] + ":" + filePath.substring(splitIndex));  
      }else if(filePath.contains("order")){  
        String val[]=value.toString().split("\t");  
        int splitIndex =filePath.indexOf("order");  
        myKey.set(val[2] + ":" + filePath.substring(splitIndex));  
      }  
      myValue.set("1");  
      context.write(myKey, myValue);  
    }  
  }

Combiner代码

经过map方法处理后,Combine过程将key值相同的value值累加,得到一个单词在文档中的词频。如果直接将输出作为Reduce过程的输入,在Shuffle过程时将面临一个问题:所有具有相同单词的记录(由单词、URL和词频组成)应该交由同一个Reducer处理,但当前的key值无法保证这一点,所以必须修改key值和value值。这次将单词作为key值,URL和词频组成value值。这样做的好处是可以利用MapReduce框架默认的HashPartitioner类完成Shuffle过程,将相同单词的所有记录发送给同一个Reducer进行处理。

public static class doCombiner extends Reducer<Text, Text, Text, Text>{  
    public static Text myK = new Text();  
    public static Text myV = new Text();  
  
    @Override //实现reduce函数  
    protected void reduce(Text key, Iterable<Text> values, Context context)  
      throws IOException, InterruptedException {  
      // 统计词频  
      int sum = 0 ;  
      for (Text value : values) {  
        sum += Integer.parseInt(value.toString());  
      }  
      int mysplit = key.toString().indexOf(":");  
      // 重新设置value值由URL和词频组成  
      myK.set(key.toString().substring(0, mysplit));  
      myV.set(key.toString().substring(mysplit + 1) + ":" + sum);  
      context.write(myK, myV);  
    }  
  }  

Reduce代码

经过上述两个过程后,Reduce过程只需将相同key值的value值组合成倒排索引文件所需的格式即可,剩下的事情就可以直接交给MapReduce框架进行处理了。

public static class doReducer extends Reducer<Text, Text, Text, Text>{  
  
    public static Text myK = new Text();  
    public static Text myV = new Text();  
  
    @Override     // 实现reduce函数  
    protected void reduce(Text key, Iterable<Text> values, Context context)  
      throws IOException, InterruptedException {  
      // 生成文档列表  
      String myList = new String();  
  
      for (Text value : values) {  
        myList += value.toString() + ";";  
      }  
      myK.set(key);  
      myV.set(myList);  
      context.write(myK, myV);  
    }  
  }

完整代码

package mapreduce;  
  import java.io.IOException;  
  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 MyIndex {  
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {  
      Job job = Job.getInstance();  
      job.setJobName("InversedIndexTest");  
      job.setJarByClass(MyIndex.class);  
  
      job.setMapperClass(doMapper.class);  
      job.setCombinerClass(doCombiner.class);  
      job.setReducerClass(doReducer.class);  
  
      job.setOutputKeyClass(Text.class);  
      job.setOutputValueClass(Text.class);  
  
      Path in1 = new Path("hdfs://localhost:9000/mymapreduce9/in/goods3");  
      Path in2 = new Path("hdfs://localhost:9000/mymapreduce9/in/goods_visit3");  
      Path in3 = new Path("hdfs://localhost:9000/mymapreduce9/in/order_items3");  
      Path out = new Path("hdfs://localhost:9000/mymapreduce9/out");  
  
      FileInputFormat.addInputPath(job, in1);  
      FileInputFormat.addInputPath(job, in2);  
      FileInputFormat.addInputPath(job, in3);  
      FileOutputFormat.setOutputPath(job, out);  
  
      System.exit(job.waitForCompletion(true) ? 0 : 1);  
    }  
  
    public static class doMapper extends Mapper<Object, Text, Text, Text>{  
      public static Text myKey = new Text();  
      public static Text myValue = new Text();  
      //private FileSplit filePath;  
  
      @Override  
      protected void map(Object key, Text value, Context context)  
        throws IOException, InterruptedException {  
        String filePath=((FileSplit)context.getInputSplit()).getPath().toString();  
        if(filePath.contains("goods")){  
          String val[]=value.toString().split("\t");  
          int splitIndex =filePath.indexOf("goods");  
          myKey.set(val[0] + ":" + filePath.substring(splitIndex));  
        }else if(filePath.contains("order")){  
          String val[]=value.toString().split("\t");  
          int splitIndex =filePath.indexOf("order");  
          myKey.set(val[2] + ":" + filePath.substring(splitIndex));  
        }  
        myValue.set("1");  
        context.write(myKey, myValue);  
      }  
    }  
    public static class doCombiner extends Reducer<Text, Text, Text, Text>{  
      public static Text myK = new Text();  
      public static Text myV = new Text();  
  
      @Override  
      protected void reduce(Text key, Iterable<Text> values, Context context)  
        throws IOException, InterruptedException {  
        int sum = 0 ;  
        for (Text value : values) {  
          sum += Integer.parseInt(value.toString());  
        }  
        int mysplit = key.toString().indexOf(":");  
        myK.set(key.toString().substring(0, mysplit));  
        myV.set(key.toString().substring(mysplit + 1) + ":" + sum);  
        context.write(myK, myV);  
      }  
    }  
  
    public static class doReducer extends Reducer<Text, Text, Text, Text>{  
  
      public static Text myK = new Text();  
      public static Text myV = new Text();  
  
      @Override  
      protected void reduce(Text key, Iterable<Text> values, Context context)  
        throws IOException, InterruptedException {  
  
        String myList = new String();  
  
        for (Text value : values) {  
          myList += value.toString() + ";";  
        }  
        myK.set(key);  
        myV.set(myList);  
        context.write(myK, myV);  
      }  
    }  
  }  

-------------- end ----------------

微信公众号:扫描下方二维码或 搜索 笑看风云路 关注
笑看风云路

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
倒排索引是一种常用的数据结构和算法,用于快速定位某个单词在大规模文本中出现的位置。在倒排索引中,单词作为关键词,而文本作为关键词的集合。通过倒排索引,我们可以快速找到包含某个关键词的文本片段。 在MapReduce中,倒排索引也是一个常见的应用案例。它可以将输入的文本数据分割成若干个独立的小块,然后通过Map阶段将每个小块中的单词作为关键词,将其所在的文本块作为值进行映射。接着,在Reduce阶段中,将具有相同关键词的文本块进行合并,形成一个完整的倒排索引。 为了实现倒排索引MapReduce程序,我们可以使用一个压缩包来运行。这个压缩包包含了所有必要的代码、配置文件和依赖项,以及输入文本数据。在运行压缩包时,MapReduce框架会自动加载其中的内容,并按照预定义的Map和Reduce函数进行处理。 通过将倒排索引程序打包成压缩包,可以方便地将程序部署到集群中的所有节点上。在集群中的每个节点上运行倒排索引程序,可以并行地处理大规模的输入数据。在MapReduce框架的控制下,每个Map和Reduce任务都会得到正确的输入和输出,并最终生成完整的倒排索引。 总之,倒排索引MapReduce的一个经典案例,通过使用压缩包来运行倒排索引程序,可以方便地部署到集群中,并实现高效的并行处理。这种方式可以加速倒排索引的生成过程,并提高数据处理的效率。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

笑看风云路

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值