2017-05-04 73 views
1

我有一個從.txt文件創建哈希表的方法,並使用該哈希表爲傳遞給Reducer的值中的單詞賦值。下面是我試圖做到這一點:爲什麼我的Reducer不能讀取文件?

@Override 
public void setup(Context context) throws IOException { 
    Path pt = new Path("hdfs:/user/jk/sentiwords.txt"); 
    FileSystem fs = FileSystem.get(new Configuration()); 
    BufferedReader br = new BufferedReader(new InputStreamReader(fs.open(pt))); 
    String line = br.readLine(); 
    while (line!=null) { 
     String[] split = line.split("\t"); 
     String word = split[0].substring(0, split[0].length() - 2); 
     double score = Double.parseDouble(split[1]); 
     int hashCode = word.hashCode(); 
     sentiTable.put(hashCode, score); 
     line = br.readLine(); 
     System.out.println("Success"); 
    } 
} 

它,然後在此方法中,這就是所謂的在鍵/值對每個值使用:

public double analyzeString(String str) { 
    double stringScore = 0.0; 
    String[] strArr = str.replaceAll("[^a-zA-Z ]", "").toLowerCase().split(" "); 
    for (String segment: strArr) { 
     int hashedSeg = segment.hashCode(); 

     if (sentiTable.containsKey(hashedSeg)) { 
      double value = (double) sentiTable.get(hashedSeg); 
      stringScore += value; 
     } 
    } 
    return stringScore; 
} 

理想的情況下,這應該-1和1之間返回一個數字在現實中,它總是返回0

編輯:

我要指出,sentiTable在類級別創建。

回答

1

得到0因此可能意味着沒有任何內容正在從這個文件中讀取。我看到兩件事情,可能出現了問題:

  1. 錯誤路徑:我認爲HDFS路徑應以hdfs://...開始,而不是hdfs:/...

  2. Path和FileSystem導入錯誤。確保你導入了Hadoop提供的。

您可以隨時在設置方法中打印消息以查看是否找到了該文件。

附加:您可能想重新考慮您的包容性檢查,因爲在大數據中使用字符串的hashCode時,預計會發生許多衝突。

+0

我想通了 - 我正在閱讀的文件有格式錯誤!它應該是製表符分隔的,並且大部分是,但是有些新的線路在某個時刻悄悄溜走。我很感激幫助 – treo

相關問題