java - Hadoop - 按前缀聚合

标签 java hadoop mapreduce hadoop2

我有带前缀的词。例如:

city|new york
city|London
travel|yes
...
city|new york

我想计算有多少个city|new yorkcity|London(这是经典的wordcount)。但是,reducer 输出应该是一个键值对,如 city:{"new york":2, "london":1}。对于每个 city 前缀,我想聚合所有字符串及其计数。

public void reduce(Text key, Iterable<IntWritable> values,
               Context context
               ) throws IOException, InterruptedException {
  int sum = 0;
  for (IntWritable val : values) {
    sum += val.get();
  }
  result.set(sum);
  // Instead of just result count, I need something like {"city":{"new york" :2, "london":1}}
  context.write(key, result);
}

有什么想法吗?

最佳答案

您可以使用 reducer 的 cleanup() 方法来实现这一点(假设您只有一个 reducer)。它在 reduce 任务结束时调用一次。

我将针对“城市”数据进行解释。

代码如下:

package com.hadooptests;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Cities {

    public static class CityMapper
            extends Mapper<LongWritable, Text, Text, IntWritable> {

        private Text outKey = new Text();
        private IntWritable outValue = new IntWritable(1);

        public void map(LongWritable key, Text value, Context context
        ) throws IOException, InterruptedException {
              outKey.set(value);
              context.write(outKey, outValue);
        }
    }

    public static class CityReducer
            extends Reducer<Text,IntWritable,Text,Text> {

        HashMap<String, Integer> cityCount = new HashMap<String, Integer>();

        public void reduce(Text key, Iterable<IntWritable>values,
                           Context context
        ) throws IOException, InterruptedException {

            for (IntWritable val : values) {
                String keyStr = key.toString();
                if(keyStr.toLowerCase().startsWith("city|")) {
                    String[] tokens = keyStr.split("\\|");

                    if(cityCount.containsKey(tokens[1])) {
                        int count = cityCount.get(tokens[1]);
                        cityCount.put(tokens[1], ++count);
                    }
                    else
                        cityCount.put(tokens[1], val.get());
                }
            }
        }

        @Override
        public void cleanup(org.apache.hadoop.mapreduce.Reducer.Context context)
                throws IOException,
                InterruptedException
        {
            String output = "{\"city\":{";
            Iterator iterator = cityCount.entrySet().iterator();
            while(iterator.hasNext())
            {
                Map.Entry entry = (Map.Entry) iterator.next();
                output = output.concat("\"" + entry.getKey() + "\":" + Integer.toString((Integer) entry.getValue()) + ", ");
            }

            output = output.substring(0, output.length() - 2);
            output = output.concat("}}");
            context.write(output, "");
        }
    }


    public static void main(String[] args) throws Exception {

        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "KeyValue");

        job.setJarByClass(Cities.class);
        job.setMapperClass(CityMapper.class);
        job.setReducerClass(CityReducer.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.addInputPath(job, new Path("/in/in.txt"));
        FileOutputFormat.setOutputPath(job, new Path("/out/"));

        System.exit(job.waitForCompletion(true) ? 0:1);

    }
}

映射器:

  1. 它只是输出它遇到的每个键的计数。例如如果遇到记录"city|new york",那么它将输出(key, value)("city|new york", 1)

reducer :

  1. 对于每条记录,它检查键是否包含"city|"。它拆分管道(“|”)上的 key 。并将每个城市的计数存储在 HashMap 中。
  2. Reducer 还覆盖了 cleanup 方法。一旦 reduce 任务结束,就会调用此方法。在此任务中,HashMap 的内容被组合成所需的输出。
  3. cleanup()中,key作为HashMap的内容输出,value作为空字符串输出。

例如我将以下数据作为输入:

city|new york
city|London
city|new york
city|new york
city|Paris
city|Paris

我得到了以下输出:

{"city":{"London":1, "new york":3, "Paris":2}}

关于java - Hadoop - 按前缀聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34101292/

相关文章:

java - 这个双端队列实现有什么问题

java - 在 hbase 中有效地发送许多 get 请求

shell - Oozie 电子邮件操作附件

hadoop - 如果我只有一个工作节点,处理文件需要多长时间?

java - JAXB 与应用程序其余部分之间的方法冲突

java - @IdClass 使用 JPA 和 Hibernate 生成 'Identifier of an Instance was Altered'

java - MR 实现在 Hadoop 集群中不起作用

log4j - 如何配置 hadoop mapreduce 以便我的 mapreduce 类的日志可以输出到文件?

java - GUI JButton,如何将按钮(问题)分配给答案

powershell - 更改 hadoop 命令的 Powershell 前景色或抑制信息