Hadoop map减少了java

问题描述 投票:-1回答:1
public static class TokenizerMapper extends Mapper<Object, Text, Text, Text> {
    public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
        StringTokenizer itr = new StringTokenizer(value.toString(), " *$&#/\t\n\f\"'\\,.:;?![](){}<>~-_");
        while (itr.hasMoreTokens()) {
            String term = itr.nextToken().toLowerCase();
            List<Pair<String, Pair<Integer, Integer>>> map = new ArrayList<Pair<String, Pair<Integer, Integer>>>();
            /*here i am performing some operations*/
            for (Pair<String, Pair<Integer, Integer>> i : map){
                String w1 = i.getKey();
                Text word = new Text(w1);
                Pair<Integer, Integer> newValue = i.getValue();
                String merge = String.valueOf(newValue.getKey()) + " " + String.valueOf(newValue.getValue());
                Text val = new Text(merge);
                /*sending both the arguments as text into my context.*/
                context.write(word, val);
            }
        }
    }
}

public static class Reducer1 extends Reducer<Text, Text, Text, Text> {
    public void reduce(Text key, Text values, Context context) throws IOException, InterruptedException {
        /* here i want to extract the values, i tried using for loop but its saying cannot iterate, its expecting something iterable.*/
        for (Text t : values)
        {
            /*this is not working. I know we can use Iterable<IntWritable> for integers but in my case it is text.
        }
        //context.write(key, values);
    }
}

请参阅注释行以更好地了解我的问题。有没有办法在reducer中提取Text值。 For循环期待iterable

java hadoop mapreduce
1个回答
0
投票

在你的reducer中,你会得到一个Iterable<Text>,你必须循环并在一个空格上拆分,为mapper写的每个merge字符串重新创建String值。

注意:如果地图中的newValue.getKey()本身包含字符串,则拆分空间将不会非常可靠

© www.soinside.com 2019 - 2024. All rights reserved.