hadoop-按前缀聚合

xmakbtuz  于 2021-06-02  发布在  Hadoop
关注(0)|答案(2)|浏览(313)

我有带前缀的单词。如:

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

我想数数有多少 city|new york 以及 city|London (这是经典的字数)。但是,减速机输出应该是一个键对,如 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);
}

有什么想法吗?

drkbr07n

drkbr07n1#

很简单。
使用“city”作为输出键,整个记录作为输出值,从mapper发出。
你将城市划分为一个单一的小组在一个减速机和旅行作为另一组。
使用hash-map对城市和旅游示例进行统计,并将其细化到较低的级别。

4jb9z9bj

4jb9z9bj2#

你可以用 cleanup() 减速机的方法来实现这一点(假设您只有一个减速机)。在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);

    }
}

Map器:
它只输出它遇到的每个键的计数。例如,如果它遇到记录“city | new york”,那么它将输出(key,value)为(“city | new york”,1)
减速器:
对于每条记录,它检查键是否包含“city |”。它将管道上的键(“|”)拆分。并将每个城市的计数存储在hashmap中。
减速器也覆盖 cleanup 方法。reduce任务完成后,将调用此方法。在这个任务中,hashmap的内容被合成到所需的输出中。
cleanup() ,键作为hashmap的内容输出,值作为空字符串输出。
例如,我将以下数据作为输入:

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

我得到了以下输出:

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

相关问题