map-reduce输出中的“键”重复?

时间:2012-04-30 20:14:04

标签: hadoop mapreduce

众所周知,这个

public static class SReducer extends MapReduceBase implements Reducer<Text, Text, Text, Text>
     {

        public void reduce(Text key, Iterator<Text> values, OutputCollector<Text, Text> output, Reporter reporter) throws IOException
        {
            StringBuilder sb = new StringBuilder();
            while (key.hasNext()) 
            {
                sb.append(key.next().toString());   
            }
            output.collect(key, new Text(sb.toString()));
        }

     }

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 
        {
            boolean start = true;
            StringBuilder sb = new StringBuilder();
            while (values.hasNext()) 
            {
                if(!start)
                {
                start=false;
                sb.append(values.next().toString());
                }           
            }
            output.collect(key, new Text(sb.toString()));
        }
    }

这是我们用来消除输出中重复“值”的减速器函数。但是我应该怎么做才能消除重复的“密钥”?任何的想法? 感谢。

PS:更多信息:在我的&lt;键,值&gt;对,键包含链接,值包含单词。但在我的输出中,每个单词只出现一次,但我得到了许多重复的链接。

1 个答案:

答案 0 :(得分:2)

Reducer中,reduce()会收到Reducer收到的每个唯一密钥的reduce()。它将接收该密钥的所有值。但是,如果你只关心密钥,只关心唯一密钥,那么,完全忽略这些值。您将获得每个密钥一个{{1}};用那个(非重复的)密钥做你想做的事。

相关问题