1. 程式人生 > >【大資料】實驗三 文件倒排索引演算法

【大資料】實驗三 文件倒排索引演算法

實驗三 文件倒排索引演算法

151220129 計科 吳政億 [email protected]

151220130 計科 伍昱名 [email protected]

151220135 計科 許麗軍 [email protected]

151220142 計科 楊楠 [email protected]

1 實驗目的

  1. 應用課堂上介紹的“帶詞頻屬性的文件倒排演算法”

  2. 在統計詞語的倒排索引時,除了要輸出帶詞頻屬性的倒排索引,還請計算每個詞語的“平均提及次數”並輸出。

    平均提及次數= 詞語在全部文件中出現的頻數總和/ 包含該詞語的文件數

  3. 兩個計算任務請在同一個MapReduce Job中完成,輸出時兩個內容可以混雜在一起。

  4. 輸入輸出檔案的格式和其他具體要求請見FTP上“實驗要求”資料夾下對應的PDF文件。

2 實驗原理

倒排索引演算法是wordcount的擴充套件問題,他需要統計一個單詞在所有檔案中各自出現的次數,直觀的,我們可以設計一種Mapper和Reducer

class mapper
    for each word in file
        key = word
        value = {filename , 1}

calss reducer
    for each value in Values
        key += {filename, value}

但是這種方法過於樸素,為了降低mapper和reducer的傳輸開銷與儲存開銷,我們應用了combiner在每次mapper結束後進行一次reducer,將結果彙總。

class Mapper
    procedureMap(docid dn, doc d)
        F ← new AssociativeArray
        for all term t ∈doc d do
            F{t} ← F{t} + 1
        for all term t ∈Fdo
            Emit(term t, posting <dn, F{t}>)
class Reducer
    procedureReduce(term t, postings [<dn1, f1>, <dn2, f2>…])
        P← new
List for all posting <dn, f> ∈postings [<dn1, f1>, <dn2, f2>…] do Append(P, <dn, f>) Sort(P) Emit(term t; postings P)

然而,這樣會有一個新的問題:

當對鍵值對進行shuffle處理以傳送給合適的Reducer時,將按照新的鍵

Class NewPartitionerextends HashPartitioner<K,V>
// org.apache.hadoop.mapreduce.lib.partition.HashPartitioner
{ // override the method
    getPartition(Kkey, Vvalue, intnumReduceTasks)
    { 
        term = key.toString().split(“,”)[0]; //<term, docid>=>term
        super.getPartition(term, value, numReduceTasks);
    }
}
Set the customized partitionerin job configuration
Job. setPartitionerClass(NewPartitioner)

3 實驗程式碼

3.1 實驗思路

  1. 分別統計各個檔案各行各個詞的出現次數,最後彙總。
  2. 用map統計輸入行各個詞的出現次數,在combine的時候將相同詞的出現次數合併,partion根據詞進行劃分,reduce時將各個詞在不同檔案的出現次數進行彙總

3.2 程式碼解釋

Class Name Division Class Information
LineCombiner 許麗軍 將不同行的所有相同詞的map輸出在本地將出現次數相加,得到的即是詞在某個檔案的一個map輸入的出現次數的輸出
WordPartition 許麗軍 以詞而非詞加檔名為基準進行劃分決定reduce的輸入
InvertedIndexReducer 吳政億、伍昱名 在類中使用prevWord記錄上一次輸入的詞,sum_of_frequency記錄詞在檔案中的總出現次數,num_of_file記錄詞出現的檔案個數,postings記錄最後輸出的(小說:詞頻)。因為reduce得到的輸入是排過序的,由此對每一個輸入判斷是否是和上一個詞相同,如果不是,輸出詞和postings,重新設定上述變數值。否則正常更新上述變數
InvertedIndexMapper 許麗軍、楊楠 以詞為key,出現次數為value建立hash表,對輸入行的每個單詞判斷是否在hash表中,如果在則將出現次數加一,否則插入hash表中置出現次數為1,處理完輸入後再hash錶轉換為(詞+“,”+檔名,出現次數)輸出
main 許麗軍 設定MapReduce各個部分所需的類

4 實驗結果

4.1 部分截圖

實驗結果-201855

實驗結果

4.2 存放路徑

存放路徑:/user/2018st03/task2_out

5 實驗總結

效能擴充套件性等方面可能存在的不足和可能的改進之處