Hadoop 倒排索引

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

一、实例描述

  倒排索引简单地就是,根据单词,返回它在哪个文件中出现过,而且频率是多少的结果。这就像百度里的搜索,你输入一个关键字,那么百度引擎就迅速的在它的服务器里找到有该关键字的文件,并根据频率和其他的一些策略(如页面点击投票率)等来给你返回结果。这个过程中,倒排索引就起到很关键的作用。

  样例输入:

  

  样例输出:

  

二、设计思路

  倒排索引涉及几个过程:Map过程,Combine过程,Reduce过程。

  Map过程: 

  当你把需要处理的文档上传到hdfs时,首先默认的TextInputFormat类对输入的文件进行处理,得到文件中每一行的偏移量和这一行内容的键值对<偏移量,内容>做为map的输入。在改写map函数的时候,我们就需要考虑,怎么设计key和value的值来适合MapReduce框架,从而得到正确的结果。由于我们要得到单词,所属的文档URL,词频,而<key,value>只有两个值,那么就必须得合并其中得两个信息了。这里我们设计key=单词+URL,value=词频。即map得输出为<单词+URL,词频>,之所以将单词+URL做为key,时利用MapReduce框架自带得Map端进行排序。

  Combine过程:

  Combine过程将key值相同得value值累加,得到一个单词在文档上得词频。但是为了把相同得key交给同一个reduce处理,我们需要设计为key=单词,value=URL+词频。

  Reduce过程

  Reduce过程其实就是一个合并的过程了,只需将相同的key值的value值合并成倒排索引需要的格式即可。

三、程序代码

  程序代码如下:

 1 import java.io.IOException;
 2 import java.util.StringTokenizer;
 3 
 4 import org.apache.hadoop.conf.Configuration;
 5 import org.apache.hadoop.fs.Path;
 6 import org.apache.hadoop.io.LongWritable;
 7 import org.apache.hadoop.io.Text;
 8 import org.apache.hadoop.mapreduce.Job;
 9 import org.apache.hadoop.mapreduce.Mapper;
10 import org.apache.hadoop.mapreduce.Reducer;
11 import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
12 import org.apache.hadoop.mapreduce.lib.input.FileSplit;
13 import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
14 import org.apache.hadoop.util.GenericOptionsParser;
15 
16 
17 public class InvertedIndex {
18 
19     public static class Map extends Mapper<LongWritable, Text, Text, Text>{
20         private static Text word = new Text();
21         private static Text one = new Text();
22         
23         @Override
24         protected void map(LongWritable key, Text value,Mapper<LongWritable, Text, Text, Text>.Context context)
25                 throws IOException, InterruptedException {
26             //  super.map(key, value, context);
27             String fileName = ((FileSplit)context.getInputSplit()).getPath().getName();
28             StringTokenizer st = new StringTokenizer(value.toString());
29             while (st.hasMoreTokens()) {
30                 word.set(st.nextToken()+"\t"+fileName);
31                 context.write(word, one);
32             }
33         }
34     }
35     
36     public static class Combine extends Reducer<Text, Text, Text, Text>{
37         private static Text word = new Text();
38         private static Text index = new Text();
39         
40         @Override
41         protected void reduce(Text key, Iterable<Text> values,Reducer<Text, Text, Text, Text>.Context context)
42                 throws IOException, InterruptedException {
43             //  super.reduce(arg0, arg1, arg2);
44             String[] splits = key.toString().split("\t");
45             if (splits.length != 2) {
46                 return ;
47             }
48             long count = 0;
49             for(Text v:values){
50                 count++;
51             }
52             word.set(splits[0]);
53             index.set(splits[1]+":"+count);
54             context.write(word, index);
55         }
56     }
57     
58     public static class Reduce extends Reducer<Text, Text, Text, Text>{
59         private static StringBuilder sub = new StringBuilder(256);
60         private static Text index = new Text();
61         
62         @Override
63         protected void reduce(Text word, Iterable<Text> values,Reducer<Text, Text, Text, Text>.Context context)
64                 throws IOException, InterruptedException {
65             // super.reduce(arg0, arg1, arg2);
66             for(Text v:values){
67                 sub.append(v.toString()).append(";");
68             }
69             index.set(sub.toString());
70             context.write(word, index);
71             sub.delete(0, sub.length());
72         }
73     }
74     
75     public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
76         Configuration conf = new Configuration();
77         String[] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs();
78         if(otherArgs.length!=2){
79             System.out.println("Usage:wordcount <in> <out>");
80             System.exit(2);
81         }
82         Job job = new Job(conf,"Invert Index ");
83         job.setJarByClass(InvertedIndex.class);
84         
85         job.setMapperClass(Map.class);
86         job.setCombinerClass(Combine.class);
87         job.setReducerClass(Reduce.class);
88         
89         job.setMapOutputKeyClass(Text.class);
90         job.setMapOutputValueClass(Text.class);
91         job.setOutputKeyClass(Text.class);
92         job.setOutputValueClass(Text.class);
93         
94         FileInputFormat.addInputPath(job,new Path(args[0]));
95         FileOutputFormat.setOutputPath(job, new Path(args[1]));
96         System.exit(job.waitForCompletion(true)?0:1);
97     }
98 
99 }

 

转载于:https://www.cnblogs.com/xiaoyh/p/9361356.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/389020.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

koa2异常处理_读 koa2 源码后的一些思考与实践

koa2的特点优势什么是 koa2Nodejs官方api支持的都是callback形式的异步编程模型。问题&#xff1a;callback嵌套问题koa2 是由 Express原班人马打造的&#xff0c;是现在比较流行的基于Node.js平台的web开发框架&#xff0c;Koa 把 Express 中内置的 router、view 等功能都移除…

Bind9的dns解析服务

前言随着原中国电信集团按南北地域分家&#xff0c;新的中国电信和网通集团随即成立&#xff0c;互联网的骨干网也被一分为二了&#xff0c;北有网通、南有电信。从此&#xff0c;细心的网民可以发现&#xff0c;有些经常访问的网站速度一下子慢了下来&#xff0c;有时候还有访…

上凸包和下凸包_使用凸包聚类

上凸包和下凸包I recently came across the article titled High-dimensional data clustering by using local affine/convex hulls by HakanCevikalp in Pattern Recognition Letters. It proposes a novel algorithm to cluster high-dimensional data using local affine/c…

sqlmap手册

sqlmap用户手册 | by WooYun知识库 sqlmap用户手册 当给sqlmap这么一个url (http://192.168.136.131/sqlmap/mysql/get_int.php?id1) 的时候&#xff0c;它会&#xff1a; 1、判断可注入的参数 2、判断可以用那种SQL注入技术来注入 3、识别出哪种数据库 4、根据用户选择&…

幸运三角形 南阳acm491(dfs)

幸运三角形 时间限制&#xff1a;1000 ms | 内存限制&#xff1a;65535 KB 难度&#xff1a;3描述话说有这么一个图形&#xff0c;只有两种符号组成&#xff08;‘’或者‘-’&#xff09;&#xff0c;图形的最上层有n个符号&#xff0c;往下个数依次减一&#xff0c;形成倒置…

jsforim

var isMouseDownfalse;var isFirsttrue;var centerdivObj;var ndiv1;var ndiv2;var ndiv3;var kjX;var kjY; window.οnerrοrfunction(){ return true;}; var thurlhttp://qq.jutoo.net/;var wzId12345; function createDiv(){ var sWscreen.width; var sHscree…

决策树有框架吗_决策框架

决策树有框架吗In a previous post, I mentioned that thinking exhaustively is exhausting! Volatility and uncertainty are ever present and must be factored into our decision making — yet, we often don’t have the time or data to properly account for it.在上一…

凑个热闹-LayoutInflater相关分析

前言 最近给组内同学做了一次“动态换肤和换文案”的主题分享&#xff0c;其中的核心就是LayoutInflater类&#xff0c;所以把LayoutInflater源码梳理了一遍。巧了&#xff0c;这周掘金新榜和部分公众号都发布了LayoutInflater或者换肤主题之类的文章。那只好站在各位大佬的肩膀…

ASP.NET Core文件上传、下载与删除

首先我们需要创建一个form表单如下: <form method"post" enctype"multipart/form-data" asp-controller"UpLoadFile" asp-action"FileSave"> <div> <div> <p>Form表单多个上传文件:</p> <input type…

8 一点就消失_消失的莉莉安(26)

文|明鸢Hi&#xff0c;中午好&#xff0c;我是暖叔今天是免费连载《消失的莉莉安》第26章消失的莉莉安▶▶往期链接&#xff1a;▼ 向下滑动阅读1&#xff1a;“消失的莉莉安(1)”2&#xff1a; 消失的莉莉安(2)3&#xff1a;“消失的莉莉安(3)”4&#xff1a;“消失的莉莉安…

透明的WinForm窗体

this.Location new System.Drawing.Point(100, 100); this.Cursor System.Windows.Forms.Cursors.Hand; // 定义在窗体上&#xff0c;光标显示为手形 this.Text "透明的WinForm窗体&#xff01;"; // 定义窗体的标题…

mysql那本书适合初学者_3本书适合初学者

mysql那本书适合初学者为什么要书籍&#xff1f; (Why Books?) The internet is a treasure-trove of information on a variety of topics. Whether you want to learn guitar through Youtube videos or how to change a tire when you are stuck on the side of the road, …

junit与spring-data-redis 版本对应成功的

spring-data-redis 版本:1.7.2.RELEASE junit 版本:4.12 转载于:https://www.cnblogs.com/austinspark-jessylu/p/9366863.html

语音对话系统的设计要点与多轮对话的重要性

这是阿拉灯神丁Vicky的第 008 篇文章就从最近短视频平台的大妈与机器人快宝的聊天说起吧。某银行内&#xff0c;一位阿姨因等待办理业务的时间太长&#xff0c;与快宝机器人展开了一场来自灵魂的对话。对于银行工作人员的不满&#xff0c;大妈向快宝说道&#xff1a;“你们的工…

c读取txt文件内容并建立一个链表_C++链表实现学生信息管理系统

可以增删查改&#xff0c;使用链表存储&#xff0c;支持排序以及文件存储及数据读取&#xff0c;基本可以应付期末大作业&#xff08;狗头&#xff09; 界面为源代码为一个main.cpp和三个头文件&#xff0c;具体为 main.cpp#include <iostream> #include <fstream>…

注册表启动

public void SetReg() { RegistryKey hklmRegistry.LocalMachine; RegistryKey runhklm.CreateSubKey("Software/Microsoft/Windows/CurrentVersion/Run"); //定义hklm指向注册表的LocalMachine,对注册表的结构&#xff0c;可以在windows的运行里&#…

阎焱多少身价_2020年,数据科学家的身价是多少?

阎焱多少身价Photo by Christine Roy on Unsplash克里斯汀罗伊 ( Christine Roy) 摄于Unsplash Although we find ourselves in unprecedented times of uncertainty, current events have shown just how valuable the fields of Data Science and Computer Science truly are…

Django模型定义参考

字段 对字段名称的限制 字段名不能是Python的保留字&#xff0c;否则会导致语法错误字段名不能有多个连续下划线&#xff0c;否则影响ORM查询操作Django模型字段类 字段类说明AutoField自增ID字段BigIntegerField64位有符号整数BinaryField存储二进制数据的字段&#xff0c;对应…

精通Quartz-入门-Job

JobDetail实例&#xff0c;并且&#xff0c;它通过job的类代码引用这个job来执行。每次调度器执行job时&#xff0c;它会在调用job的execute(..)方法之前创建一个他的实例。这就带来了两个事实&#xff1a;一、job必须有一个不带参数的构造器&#xff0c;二、在job类里定义数据…

单据打印_Excel多功能进销存套表,自动库存单据,查询打印一键操作

Hello大家好&#xff0c;我是帮帮。今天跟大家分享一张Excel多功能进销存管理套表&#xff0c;自动库存&#xff0c;单据打印&#xff0c;查询统算一键操作。为了让大家能更稳定的下载模板&#xff0c;我们又开通了全新下载方式(见文章末尾)&#xff0c;以便大家可以轻松获得免…