[英]Weird formatting issue in hadoop map/reduce program in java
我有以下示例記錄的csv文件。
| publisher | site | ad clicks | ad views |
|============|====================|===========|==========|
| publisher1 | www.sampleSite.com | 50 | 75 |
| publisher1 | www.sampleSite2.com| 10 | 40 |
| publisher2 | www.newSite1.com | 100 | 175 |
| publisher2 | www.newSite2.com | 50 | 65 |
我在Java中使用map / reduce,試圖對每個發布者的所有廣告點擊次數和廣告觀看次數進行匯總。 所以輸出應該是這樣的
publisher1 60, 115
publisher2 150, 240
我寫了下面的代碼。
import java.io.IOException;
import java.util.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.*;
public class SGSClickViewStats
{
public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, Text>
{
int recNo = 0;
private Text publisherName = new Text();
private Text mapOpValue = new Text();
public void map(LongWritable key, Text inputs, OutputCollector <Text, Text> output, Reporter rptr) throws IOException{
String line = inputs.toString();
String [] fields = line.split(",");
String pubName = formatStats(fields[0]);
String click = fields[2];
String views = fields[3];
// ***** send stats to reducer as a string separated by :
String value = click+":"+views;
mapOpValue.set(formatStats(value));
publisherName.set(pubName);
output.collect(publisherName, mapOpValue);
}
private String formatStats(String stat) {
while((stat.indexOf("\"") >= 0) && (stat.indexOf(",")) >= 0){
stat = stat.replace("\"","");
stat = stat.replace(",","");
}
return stat;
}
}
public static class Reduce extends MapReduceBase implements Reducer< Text, Text, Text, Text >
{
private Text pubName = new Text();
public void reduce(Text key, Iterator<Text> value, OutputCollector<Text, Text> oc, Reporter rptr) throws IOException {
int views = 0;
int clicks = 0;
String val = "";
String opVal = "";
Text textOpVal= new Text();
while(value.hasNext()){
val = value.next().toString();
String [] tokens = val.split(":");
try {
clicks = clicks + Integer.parseInt(tokens[0]);
views = views + Integer.parseInt(tokens[1]);
} catch (Exception e) {
System.out.println("This is Command HQ, code red\nError Message: "+e.getLocalizedMessage()+" Error class: "+e.getClass()+"Extra, Array length: "+tokens.length);
}
}
try {
// ******* want to separate stats by comma but can't !!
opVal = Integer.toString(clicks) + ":"+ Integer.toString(views);
} catch (Exception e) {
System.out.println("This is Command HQ, code Yellow\nError Message: "+e.getLocalizedMessage()+" Error class: "+e.getClass());
}
textOpVal.set(opVal);
oc.collect(key, textOpVal);
}
}
public static void main(String [] args) throws Exception {
JobConf jc = new JobConf(SGSClickViewStats.class);
jc.setJobName("SGSClickViewStats");
jc.setOutputKeyClass(Text.class);
jc.setOutputValueClass(Text.class);
jc.setMapperClass(Map.class);
jc.setReducerClass(Reduce.class);
jc.setCombinerClass(Reduce.class);
jc.setInputFormat(TextInputFormat.class);
jc.setOutputFormat(TextOutputFormat.class);
FileInputFormat.setInputPaths(jc, new Path(args[0]));
FileOutputFormat.setOutputPath(jc, new Path(args[1]));
JobClient.runJob(jc);
}
}
該程序運行良好,但是在reducer的輸出中,我無法用逗號分隔最終的統計信息,在第二個注釋中使用* * 。 如果這樣做,我的所有統計信息將變為0、0。當我嘗試用逗號分隔時,會出現此錯誤。
Error Message: For input string: "50, 75"
Error class: class java.lang.NumberFormatExceptionExtra, Array length: 1
數組長度是reducer函數中令牌數組的長度,因為我正在將輸出從mapper發送到reducer冒號(:)分隔的令牌應包含2個元素,當我將reducer逗號的輸出設置為分隔時,會看到一個元素。
我已經推薦了許多文章,但是找不到答案。 我真誠地希望有人幫助! :)
為什么要使用Reducer作為合並器? 當您的數據進入簡化階段時,它已經是“ publisher \\ tclicks,views”格式,我想這可能會引起問題。
您可以在下面的行中注釋並檢查嗎?
jc.setCombinerClass(Reduce.class);
NumberFormatException
絕對是Integer.parseInt
拋出的,因此在計算點擊次數和觀看次數總和時,您的錯誤必須在第一次嘗試中。 檢查映射器傳遞的輸出。 我很確定您沒有在映射器中正確格式化字符串。
編輯 :為將來的讀者明確:問題是將Reducer類也誤用作了Combiner,從而產生了與map階段不同的輸出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.