Mapreduce实例-分组排重(group by distinct)

 1 public class GroupComparator implements RawComparator<MyBinaryKey> {
 2  
 3  @Override
 4  public int compare(MyBinaryKey o1, MyBinaryKey o2) {
 5   return o1.toString().compareTo(o2.toString());
 6  }
 7 
 8  @Override
 9  public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
10   return WritableComparator.compareBytes(b1, s1, Long.SIZE / 8 + Integer.SIZE / 8 * 3, b2, s2,  Long.SIZE / 8 + Integer.SIZE / 8 * 3);
11  }
12 
13 }
14 
15 public abstract class UVBinaryKey  extends BinaryComparable implements WritableComparable<BinaryComparable>{
16  //根据需要添加属性;
17   @Override
18  public void readFields(DataInput in) throws IOException {
19 
20 } 
21 
22 @Override
23  public byte[] getBytes() {
24 
25 }
26 
27 }
28 
29 public class MyPartitioner extends Partitioner<MyBinaryKey, NullWritable> {
30 
31  /**
32   * 根据uv/ip取模分区,保证相同uv/ip落在同一分区
33   */
34  @Override
35  public int getPartition(MyBinaryKey key, NullWritable value, int numPartitions) {
36   
37   int k=0;
38   for(byte b : key.getAttr()){
39    k+=b&0xff;
40   }
41   return k%numPartitions;
42  }
43 
44 }
45 
46 
47 
48   job.setMapOutputKeyClass(UVBinaryKey.class);
49   job.setGroupingComparatorClass(GroupComparator.class);
50    job.setPartitionerClass(MyPartitioner.class);
51 
52   map 略
 1 combiner(根据需要添加)
 2 reduce中的实现:
 3        @Override
 4         protected void reduce(UVBinaryKey key, Iterable<NullWritable> values, Context context)
 5                 throws IOException,
 6                 InterruptedException {
 7             long count = 0;
 8             byte[] tbsign = null;
 9             for (NullWritable nullWritable : values) {
10                 byte[] attr = key.getAttr();
11                 if (tbsign == null) {
12                     tbsign = attr;
13                     count++;
14                 }
15                 if (tbsign != null) {
16                     if (tbsign.length != attr.length) {
17                         count++;
18                         tbsign = attr;
19                     } else {
20                         for (int i = 0; i < tbsign.length; i++) {
21                             if (tbsign[i] != attr[i]) {
22                                 count++;
23                                 tbsign = attr;
24                                 break;
25                             }
26                         }
27                     }
28                 }
29 
30             }
31             StringBuffer out = new StringBuffer();
32             out.append(new String(key.getCity()))
33                     .append(Constants.FIELDS_TERMINATED).append(count);
34             context.write(new Text(out.toString()), NullWritable.get());
35 
36         }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
假设我们有一个天气数据集,每个记录包含日期、城市、最高温度和最低温度。我们想要统计每个城市在每个月的平均最高温度和平均最低温度,我们可以使用MapReduce进行分组统计。 首先,我们需要定义Mapper函数的输入和输出。Mapper函数的输入应该是文本行,每行表示一个天气记录,包含日期、城市、最高温度和最低温度,以逗号分隔。Mapper函数的输出应该是键值对,其中键是由城市和月份组成的复合键,值是一个包含最高温度和最低温度的可序列化对象。 伪代码如下: ```python Mapper(输入: key, value): 解析输入行,获取城市、日期、最高温度和最低温度 从日期中提取出月份 构造复合键,由城市和月份组成 构造值对象,包含最高温度和最低温度 发射键值对,以复合键为键,值对象为值 ``` 接下来,我们需要定义Reducer函数的输入和输出。Reducer函数的输入应该是键值对,其中键是由城市和月份组成的复合键,值是一个包含最高温度和最低温度的可序列化对象的迭代器。Reducer函数的输出应该是键值对,其中键是由城市和月份组成的复合键,值是一个包含平均最高温度和平均最低温度的可序列化对象。 伪代码如下: ```python Reducer(输入: key, values): 计算所有值的平均最高温度和平均最低温度 构造值对象,包含平均最高温度和平均最低温度 发射键值对,以复合键为键,值对象为值 ``` 最后,我们需要运行MapReduce作业。我们可以使用Hadoop Streaming工具,通过标准输入和输出来传递数据。假设我们的Mapper函数定义在mapper.py文件中,Reducer函数定义在reducer.py文件中,我们可以使用以下命令来运行作业: ```bash hadoop jar hadoop-streaming.jar \ -mapper mapper.py \ -reducer reducer.py \ -input input_file \ -output output_dir ``` 其中,hadoop-streaming.jar是Hadoop Streaming工具的JAR文件;mapper.py和reducer.py是我们定义的Mapper和Reducer函数;input_file是输入文件的路径;output_dir是输出目录的路径。 通过这种方式,我们可以使用MapReduce分组统计天气信息,并且可以方便地扩展到更大的数据集和更复杂的统计任务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值