Hadoop學習筆記(8) ——實戰 做個倒排索引


Hadoop學習筆記(8)

——實戰 做個倒排索引

倒排索引是文檔檢索系統中最常用數據結構。根據單詞反過來查在文檔中出現的頻率,而不是根據文檔來,所以稱倒排索引(Inverted Index)。結構如下:

這張索引表中, 每個單詞都對應着一系列的出現該單詞的文檔,權表示該單詞在該文檔中出現的次數。現在我們假定輸入的是以下的文件清單:

T1 : hello world hello china

T2 : hello hadoop

T3 : bye world bye hadoop bye bye

 

輸入這些文件,我們最終將會得到這樣的索引文件:

bye    T3:4;

china    T1:1;

hadoop    T2:1;T3:1;

hello    T1:2;T2:1;

world    T1:1;T3:1;

 

接下來,我們就是要想辦法利用hadoop來把這個輸入,變成輸出。從上一章中,其實也就是分析如何將hadoop中的步驟個性化,讓其工作。整個步驟中,最主要的還是map和reduce過程,其它的都可稱之為配角,所以我們先來分析下map和reduce的過程將會是怎樣?

首先是Map的過程。Map的輸入是文本輸入,一條條的行記錄進入。輸出呢?應該包含:單詞、所在文件、單詞數。 Map的輸入是key-value。 那這三個信息誰是key,誰是value呢? 數量是需要累計的,單詞數肯定在value里,單詞在key中,文件呢?不同文件內的相同單詞也不能累加的,所以這個文件應該在key中。這樣key中就應該包含兩個值:單詞和文件,value則是默認的數量1,用於后面reduce來進行合並。

所以Map后的結果應該是這樣的:

Key value

Hello;T1 1

Hello:T1 1

World:T1 1

China:T1 1

Hello:T2 1

即然這個key是復合的,所以常歸的類型已經不能滿足我們的要求了,所以得設置一個復合健。復合健的寫法在上一章中描述到了。所以這里我們就直接上代碼:

  1. public static class MyType implements WritableComparable<MyType>{
  2.       public MyType(){
  3.       }
  4.  
  5.       private String word;
  6.       public String Getword(){return word;}
  7.       public void Setword(String value){ word = value;}
  8.  
  9.       private String filePath;
  10.       public String GetfilePath(){return filePath;}
  11.       public void SetfilePath(String value){ filePath = value;}
  12.  
  13.       @Override
  14.       public void write(DataOutput out) throws IOException {
  15.          out.writeUTF(word);
  16.          out.writeUTF(filePath);
  17.       }
  18.  
  19.       @Override
  20.       public void readFields(DataInput in) throws IOException {
  21.          word = in.readUTF();
  22.          filePath = in.readUTF();
  23.       }
  24.  
  25.       @Override
  26.       public int compareTo(MyType arg0) {
  27.             if (word != arg0.word)
  28.                return word.compareTo(arg0.word);
  29.          return filePath.compareTo(arg0.filePath);
  30.       }
  31. }

有了這個復合健的定義后,這個Map函數就好寫了:

  1. public static class InvertedIndexMapper extends
  2.          Mapper<Object, Text, MyType, Text> {
  3.  
  4.       public void map(Object key, Text value, Context context)
  5.             throws InterruptedException, IOException {
  6.  
  7.          FileSplit split = (FileSplit) context.getInputSplit();
  8.          StringTokenizer itr = new StringTokenizer(value.toString());
  9.  
  10.          while (itr.hasMoreTokens()) {
  11.             MyType keyInfo = new MyType();
  12.             keyInfo.Setword(itr.nextToken());
  13.             keyInfo.SetfilePath(split.getPath().toUri().getPath().replace("/user/zjf/in/", ""));
  14.             context.write(keyInfo, new Text("1"));
  15.          }
  16.       }
  17.    }

注意:第13行,路徑是全路徑的,為了看起來方便,我們把目錄替換掉,直接取文件名。

 

有了Map,接下來就可以考慮Recude了,以及在Map之后的Combine。Map的輸出的Key類型是MyType,所以Reduce以及Combine的輸入就必須是MyType了。

如果直接將Map的結果送到Reduce后,發現還需要做大量的工作來將Key中的單詞再重排一下。所以我們考慮在Reduce前加一個Combine,先將數量進行一輪合並。

這個Combine將會輸入下面的值:

Key value

bye    T3:4;

china    T1:1;

hadoop    T2:1;

hadoop    T3:1;

hello    T1:2;

hello    T2:1;

world    T1:1;

world    T3:1;

代碼如下:

  1. public static class InvertedIndexCombiner extends
  2.          Reducer<MyType, Text, MyType, Text> {
  3.  
  4.       public void reduce(MyType key, Iterable<Text> values, Context context)
  5.             throws InterruptedException, IOException {
  6.          int sum = 0;
  7.          for (Text value : values) {
  8.             sum += Integer.parseInt(value.toString());
  9.          }
  10.          context.write(key, new Text(key.GetfilePath()+ ":" + sum));
  11.       }
  12.    }

 

有了上面Combine后的結果,再進行Reduce就容易了,只需要將value結果進行合並處理:

  1. public static class InvertedIndexReducer extends
  2.          Reducer<MyType, Text, Text, Text> {
  3.  
  4.       public void reduce(MyType key, Iterable<Text> values, Context context)
  5.             throws InterruptedException, IOException {
  6.          Text result = new Text();
  7.  
  8.          String fileList = new String();
  9.          for (Text value : values) {
  10.             fileList += value.toString() + ";";
  11.          }
  12.          result.set(fileList);
  13.  
  14.          context.write(new Text(key.Getword()), result);
  15.       }
  16.    }

    經過這個Reduce處理,就得到了下面的結果:

bye    T3:4;

china    T1:1;

hadoop    T2:1;T3:1;

hello    T1:2;T2:1;

world    T1:1;T3:1;

 

最后,MapReduce函數都寫完后,就可以掛在Job中運行了。

  1. public static void main(String[] args) throws IOException,
  2.          InterruptedException, ClassNotFoundException {
  3.       Configuration conf = new Configuration();
  4.       System.out.println("url:" + conf.get("fs.default.name"));
  5.  
  6.       Job job = new Job(conf, "InvertedIndex");
  7.       job.setJarByClass(InvertedIndex.class);
  8.       job.setMapperClass(InvertedIndexMapper.class);
  9.       job.setMapOutputKeyClass(MyType.class);
  10.       job.setMapOutputValueClass(Text.class);
  11.  
  12.       job.setCombinerClass(InvertedIndexCombiner.class);
  13.       job.setReducerClass(InvertedIndexReducer.class);
  14.  
  15.       job.setOutputKeyClass(Text.class);
  16.       job.setOutputValueClass(Text.class);
  17.  
  18.       Path path = new Path("out");
  19.       FileSystem hdfs = FileSystem.get(conf);
  20.       if (hdfs.exists(path))
  21.          hdfs.delete(path, true);
  22.  
  23.       FileInputFormat.addInputPath(job, new Path("in"));
  24.       FileOutputFormat.setOutputPath(job, new Path("out"));
  25.       job.waitForCompletion(true);
  26. }

注:這里為了調試方便,我們把in和out都寫死,不用傳入執行參數了,並且,每次執行前,判斷out文件夾是否存在,如果存在則刪除。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM