2
這是從輸入文件中的示例:奇怪輸出
1,name1,name2
2,name3,name4
3,name5,name6
,這是我的地圖的方法:
public void map(LongWritable key, Text value, OutputCollector<Text, Text> output, Reporter reporter) throws IOException
{
String line = value.toString();
StringTokenizer tk = new StringTokenizer(line, ",");
String keyValue = tk.nextToken();
String s1Value = tk.nextToken();
String s2Value = tk.nextToken();
String valueString = s1Value+","+s2Value;
output.collect(new Text(keyValue), new Text(valueString));
}
,這是我的降低功能:
public static class Reduce extends MapReduceBase implements Reducer<Text, Text, Text, Text>
{
public void reduce(Text key, Iterator<Text> values,
OutputCollector<Text, Text> output, Reporter reporter) throws IOException
{
String item="";
Text tmp= new Text();
while (values.hasNext())
{
tmp = values.next();
}
item = tmp.toString();
StringTokenizer tk = new StringTokenizer(item, ",");
String s1="";
String s2="";
boolean entered = false;
try
{
while (tk.hasMoreTokens() && !entered)
{
s1 = tk.nextToken();
s2 = tk.nextToken();
entered = true;
}
}
catch (Exception e)
{
System.out.println("PROBLEM:"+item);
}
double result = compare(s1,s2);
String result2 = s1+" & "+s2+"="+result;
output.collect(key, new Text(result2));
}
}
所以我期望輸出是(例如):
name1 & name2=1.0
但我得到的是:
name1 & name2=1.0 & =0.0
看起來像所有的時間有兩個空的字符串會比較! 爲什麼總是有空串?
計數器轉儲對映射器輸出記錄的數量和還原器方法的數量有何說法? –
它說:兩者都是13,因爲我在輸入文件中有13行,並且我希望每行都單獨處理。 – Jarvis