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.pswp.cn/news/389020.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/389020.shtml
英文地址,請注明出處:http://en.pswp.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;以便大家可以輕松獲得免…