前段時間業務系統有個模塊數據沒有了,在排查問題的時候發現中間處理環節出錯了,錯誤日志為文件格式不正確,將數據導出后發現這個處理邏輯的輸入文件中每一行都多了一列,而且是一個空列(列分隔符是\t)。第一次檢查代碼后沒發現代碼里多寫了一列,第二次排查Reduce代碼時,發現在寫文件時value為空的Text():
public void reduce(Text key, Iterator<Text> values, OutputCollector<Text, Text> output, Reporter reporter) throws IOException { String keyString = key.toString(); Iterator<Text> iterValue = values; Double totalSize = 0D; while (iterValue.hasNext()) { String value = iterValue.next().toString(); totalSize += Double.valueOf(value); } keyString += "\t" + totalSize; //原來是這么寫的 // output.collect(new Text(keyString), new Text()); //應當這么寫(此處不推薦new Text(keyString),正確的做法是定義全局的Text,使用的時候用text.Set()): output.collect(new Text(keyString), null); }
參見上面的代碼段。
如果在輸出reduce結果時這么寫:
output.collect(new Text(keyString), new Text());
就會導致結果文件中有三個\t。
將new Text() 改成null就可以解決問題了。