Mapreduce实例-分组排重(group by distinct)
public class GroupComparator implements RawComparator<MyBinaryKey> {
@Override
public int compare(MyBinaryKey o1, MyBinaryKey o2) {
return o1.toString().compareTo(o2.toString());
}
@Override
public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
return WritableComparator.compareBytes(b1, s1, Long.SIZE / 8 + Integer.SIZE / 8 * 3, b2, s2, Long.SIZE / 8 + Integer.SIZE / 8 * 3);
}
}
public abstract class UVBinaryKey extends BinaryComparable implements WritableComparable<BinaryComparable>{
//根据需要添加属性;
@Override
public void readFields(DataInput in) throws IOException {
}
@Override
public byte[] getBytes() {
}
}
public class MyPartitioner extends Partitioner<MyBinaryKey, NullWritable> {
/**
* 根据uv/ip取模分区,保证相同uv/ip落在同一分区
*/
@Override
public int getPartition(MyBinaryKey key, NullWritable value, int numPartitions) {
int k=0;
for(byte b : key.getAttr()){
k+=b&0xff;
}
return k%numPartitions;
}
}
job.setMapOutputKeyClass(UVBinaryKey.class);
job.setGroupingComparatorClass(GroupComparator.class);
job.setPartitionerClass(MyPartitioner.class);
map 略
combiner(根据需要添加)
reduce中的实现:
@Override
protected void reduce(UVBinaryKey key, Iterable<NullWritable> values, Context context)
throws IOException,
InterruptedException {
long count = 0;
byte[] tbsign = null;
for (NullWritable nullWritable : values) {
byte[] attr = key.getAttr();
if (tbsign == null) {
tbsign = attr;
count++;
}
if (tbsign != null) {
if (tbsign.length != attr.length) {
count++;
tbsign = attr;
} else {
for (int i = 0; i < tbsign.length; i++) {
if (tbsign[i] != attr[i]) {
count++;
tbsign = attr;
break;
}
}
}
} }
StringBuffer out = new StringBuffer();
out.append(new String(key.getCity()))
.append(Constants.FIELDS_TERMINATED).append(count);
context.write(new Text(out.toString()), NullWritable.get()); }
Mapreduce实例-分组排重(group by distinct)的更多相关文章
- MapReduce实例2(自定义compare、partition)& shuffle机制
MapReduce实例2(自定义compare.partition)& shuffle机制 实例:统计流量 有一份流量数据,结构是:时间戳.手机号.....上行流量.下行流量,需求是统计每个用 ...
- SQL 数据排重,去掉重复数据 有用
.最大的错误: 在对数据排重的时候,首先想到的就是Distinct,虽然这很管用,但多数场合下不适用,因为通常排重后还要做进一步处理,比如对编号排重后要按日期统计等. 无法排重的Group by ...
- 《MYSQL》----字符串的复杂函数,检索的七-天-排-重
接到了一个新的需求,拿到需求的时候瞬间有点头大,因为实在是有些棘手. 我们这个系统本身是个接口系统,总接口数大概在200个左右.外部会有很多用户在 不同的时间拿着不同参数去调我们的这些接口,用户的调集 ...
- MySQL对数据表进行分组查询(GROUP BY)
MySQL对数据表进行分组查询(GROUP BY) GROUP BY关键字可以将查询结果按照某个字段或多个字段进行分组.字段中值相等的为一组.基本的语法格式如下: GROUP BY 属性名 [HAVI ...
- MapReduce实例&YARN框架
MapReduce实例&YARN框架 一个wordcount程序 统计一个相当大的数据文件中,每个单词出现的个数. 一.分析map和reduce的工作 map: 切分单词 遍历单词数据输出 r ...
- 转:C#微信公众号开发之接收事件推送与消息排重的方法
本文实例讲述了C#微信公众号开发之接收事件推送与消息排重的方法.分享给大家供大家参考.具体分析如下: 微信服务器在5秒内收不到响应会断掉连接,并且重新发起请求,总共重试三次.这样的话,问题就来了.有这 ...
- Sicily 1051: 魔板(BFS+排重)
相对1150题来说,这道题的N可能超过10,所以需要进行排重,即相同状态的魔板不要重复压倒队列里,这里我用map储存操作过的状态,也可以用康托编码来储存状态,这样时间缩短为0.03秒.关于康托展开可以 ...
- C#微信公众号开发系列教程五(接收事件推送与消息排重)
微信公众号开发系列教程一(调试环境部署) 微信公众号开发系列教程一(调试环境部署续:vs远程调试) C#微信公众号开发系列教程二(新手接入指南) C#微信公众号开发系列教程三(消息体签名及加解密) C ...
- linux取某个字段排重
排重统计 cat a.txt | awk -F ';' '{print $2}' | sort -u | wc -l
随机推荐
- Android中播放本地SD卡中歌曲须要的加入的权限
使用MediaPlayer播放本地Mp3文件时.须要注意的訪问路径的问题以及訪问权限的问题. 1.訪问路径:/storage/emulated/0 此路径即为手机的根路径,能够通过下载ES文件浏览器软 ...
- What is an OPC .NET Wrapper ?
An OPC .NET wrapper is a software layer that makes OPC COM servers accessible from a .NET client app ...
- MVC文件上传04-使用客户端jQuery-File-Upload插件和服务端Backload组件实现多文件异步上传
本篇使用客户端jQuery-File-Upload插件和服务端Badkload组件实现多文件异步上传.MVC文件上传相关兄弟篇: MVC文件上传01-使用jquery异步上传并客户端验证类型和大小 ...
- ExtJS BorderLayout
<HTML> <HEAD> <TITLE>布局</TITLE> <link rel="stylesheet" type=&qu ...
- 【linux】centos7终端中文显示乱码,命令返回中文乱码
centos7终端中文显示乱码,命令返回中文乱码 1.查看服务器编码的命令 1.1 echo $LANG 1.2 locale 1.3 查看终端xshell编码 如果以上的三点依旧保持一致,而依旧乱码 ...
- Windows Phone 7 开发环境的搭建
本节开始进行Windows Phone 开发环境的搭建,包括所需要的操作系统及硬件的介绍,开发工具的下载与安装,以及开发工具的介绍等.由于Jake Lin老师的视频中讲解的是早期的Windows Ph ...
- [翻译] AFNetworking 2.0
大名鼎鼎的开源网络库AFNetworking 2.0,目前只是翻译了Github上的链接文章,使用教程请点击 http://www.cnblogs.com/YouXianMing/p/3651462. ...
- Xcode界面切换动画效果
CATransition *animation = [CATransition animation]; [animation setDuration:0.2f]; [animation setTimi ...
- Python for everyone chapter 1
Chapter 1 10 试题 1. When Python is running in the interactive mode and displaying the chevron prompt ...
- Gunicorn+Flask中重复启动后台线程问题
假设程序如下: if __name__ == '__main__': t = Thread(target=test) t.start() app.run(host='0.0.0.0',port=808 ...