Hadoop inverted index

Source: Internet
Author: User

 

1. Preface

It is very important to study the inverted index algorithm of hadoop. This is the basis for future work. First, let's get to know what a reverse index is:

The inverted index is simply the result of returning the file in which the word appears and how often it is based on the word. This is like Baidu's search. If you enter a keyword, Baidu engine will quickly find the file with the keyword on its server, return results to you based on the frequency and other strategies (such as page Click voting rate. In this process, inverted indexes play a key role.

2. Analysis Design

Inverted indexes involve several processes: map, combine, and reduce. Next we will analyze the above process.

2.1map Process

When you upload a document to HDFS, the default textinputformat class processes the input file, get the offset of each row in the file and the <OFFSET, content> of the key-value pair of this row as the input of map. When rewriting the map function, we need to consider how to design the key and value values to fit the mapreduce framework and get the correct results. Because we want to get the word, the URL of the document, and the word frequency, and <key, value> only has two values, we have to merge the two information. Here we design key = word + URL, value = word frequency. That is, map outputs are <word + URL, Word Frequency>. When words + URL are used as the key, we use the mapreduce Framework's built-in map end for sorting.

The following is a simple example:

 

Figure 1 input/output of the MAP process

2.2 combine process

 

In the combine process, values of the same key value are accumulated to obtain the word frequency in the document. However, in order to give the same key to the same reduce processing, we need to design it as key = word, value = URL + Word Frequency

Figure 2 input/output of the combin Process

2.3reduce Process

The reduce process is actually a merge process. You only need to merge the values of the same key value into the format required by the inverted index.

 

Figure 3 input/output of reduce Process

3. Source Code

 

Package reverseindex; import Java. io. ioexception; import Java. util. stringtokenizer; import Org. apache. hadoop. conf. configuration; 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; import Org. apache. hadoop. util. genericoptionsparser; public class invertedindex {public static class invertedindexmapper extends mapper <object, text, text> {private text keyinfo = new text (); private text valueinfo = new text (); private filesplit split; Public void map (Object key, text value, context contex T) throws ioexception, interruptedexception {// get the <key, value> pair to the object split = (filesplit) context. getinputsplit (); stringtokenizer itr = new stringtokenizer (value. tostring (); While (itr. hasmoretokens () {// the key value consists of words and URLs, for example, "mapreduce: 1.txt" keyinfo. set (itr. nexttoken () + ":" + split. getpath (). tostring (); valueinfo. set ("1"); context. write (keyinfo, valueinfo) ;}} public static class invertedindexcombiner exte NDS reducer <text, text> {private text info = new text (); Public void reduce (Text key, iterable <text> values, context) throws ioexception, interruptedexception {// count the Word Frequency int sum = 0; For (Text Value: values) {sum + = integer. parseint (value. tostring ();} int splitindex = key. tostring (). indexof (":"); // reset the value by URL and Word Frequency composition info. set (key. tostring (). substring (splitindex + 1) + ":" + sum); // reset the key value to a word. Key. set (key. tostring (). substring (0, splitindex); context. write (Key, Info) ;}} public static class invertedindexreduce extends CER <text, text> {private text result = new text (); public void reduce (Text key, iterable <text> values, context) throws ioexception, interruptedexception {// generate document list string filelist = new string (); For (text value: values) {filelist + = value. tostring () + ";";} r Esult. set (filelist); context. write (Key, result) ;}} public static void main (string [] ARGs) throws exception {// todo auto-generated method stub configuration conf = new configuration (); string [] otherargs = new genericoptionsparser (Conf, argS ). getremainingargs (); If (otherargs. length! = 2) {system. err. println ("Usage: invertedindex <in> <out>"); system. exit (2);} job = new job (Conf, "invertedindex"); job. setjarbyclass (invertedindex. class); job. setmapperclass (invertedindexmapper. class); job. setmapoutputkeyclass (text. class); job. setoutputvalueclass (text. class); job. setcombinerclass (invertedindexcombiner. class); job. setreducerclass (invertedindexreduce. class); job. setoutputkeyclass (Tex T. class); job. setoutputvalueclass (text. class); fileinputformat. addinputpath (job, new path (otherargs [0]); fileoutputformat. setoutputpath (job, new path (otherargs [1]); system. exit (job. waitforcompletion (true )? );}}

 

Hadoop inverted index

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.