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

看起来一直有两个空字符串进行比较!为什么总是有空字符串?

4

1 回答 1

0

它应该关心“compare(s1,s2)”的代码;请粘贴比较功能的代码。

于 2012-10-08T12:50:09.710 回答