Hadoop MapReduce自定义数据类型
一 自定义数据类型的实现
1.继承接口Writable,实现其方法write()和readFields(), 以便该数据能被序列化后完成网络传输或文件输入/输出;
2.如果该数据需要作为主键key使用,或需要比较数值大小时,则需要实现WritalbeComparable接口,实现其方法write(),readFields(),CompareTo() 。
3.重写toString()、hashCode()、equals()方法。
二 自定义数据类型示例
OrderWritable — 作为key
UserWritable — 作为value
package com.ibeifeng.mapreduce.io; import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.apache.hadoop.io.WritableComparable; public class OrderWritable implements WritableComparable<OrderWritable> { private String orderId;
private float price; public OrderWritable() { } public OrderWritable(String orderId, float price) {
this.set(orderId, price);
} public void set(String orderId, float price) {
this.orderId = orderId;
this.price = price;
} public String getOrderId() {
return orderId;
} public void setOrderId(String orderId) {
this.orderId = orderId;
} public float getPrice() {
return price;
} public void setPrice(float price) {
this.price = price;
} public void write(DataOutput out) throws IOException {
out.writeUTF(orderId);
out.writeFloat(price); } public void readFields(DataInput in) throws IOException { this.orderId = in.readUTF();
this.price = in.readFloat();
} public int compareTo(OrderWritable o) { int comp = this.getOrderId().compareTo(o.getOrderId()); if (0 == comp) {
return Float.valueOf(this.getPrice()).compareTo(
Float.valueOf(o.getPrice()));
} return comp;
} @Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((orderId == null) ? 0 : orderId.hashCode());
result = prime * result + Float.floatToIntBits(price);
return result;
} @Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
OrderWritable other = (OrderWritable) obj;
if (orderId == null) {
if (other.orderId != null)
return false;
} else if (!orderId.equals(other.orderId))
return false;
if (Float.floatToIntBits(price) != Float.floatToIntBits(other.price))
return false;
return true;
} @Override
public String toString() {
return orderId + "\t" + price;
} }
package com.ibeifeng.mapreduce.io; import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.apache.hadoop.io.Writable; public class UserWritable implements Writable { private int id;
private String name; public UserWritable() { } public UserWritable(int id, String name) {
this.set(id, name);
} public void set(int id, String name) { this.id = id;
this.name = name;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public void write(DataOutput out) throws IOException {
out.writeInt(id);
out.writeUTF(name); } public void readFields(DataInput in) throws IOException {
this.id = in.readInt();
this.name = in.readUTF();
} @Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
} @Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
UserWritable other = (UserWritable) obj;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
} @Override
public String toString() {
return id + "\t" + name;
} }
Hadoop MapReduce自定义数据类型的更多相关文章
- hadoop的自定义数据类型和与关系型数据库交互
最近有一个需求就是在建模的时候,有少部分数据是postgres的,只能读取postgres里面的数据到hadoop里面进行建模测试,而不能导出数据到hdfs上去. 读取postgres里面的数据库有两 ...
- Hadoop mapreduce自定义分组RawComparator
本文发表于本人博客. 今天接着上次[Hadoop mapreduce自定义排序WritableComparable]文章写,按照顺序那么这次应该是讲解自定义分组如何实现,关于操作顺序在这里不多说了,需 ...
- mapreduce 自定义数据类型的简单的应用
本文以手机流量统计为例: 日志中包含下面字段 现在需要统计手机的上行数据包,下行数据包,上行总流量,下行总流量. 分析:可以以手机号为key 以上4个字段为value传传递数据. 这样则需要自己定义一 ...
- [Hadoop] - Mapreduce自定义Counter
在Hadoop的MR程序开发中,经常需要统计一些map/reduce的运行状态信息,这个时候我们可以通过自定义Counter来实现,这个实现的方式是不是通过配置信息完成的,而是通过代码运行时检查完成的 ...
- Hadoop mapreduce自定义分区HashPartitioner
本文发表于本人博客. 在上一篇文章我写了个简单的WordCount程序,也大致了解了下关于mapreduce运行原来,其中说到还可以自定义分区.排序.分组这些,那今天我就接上一次的代码继续完善实现自定 ...
- Hadoop mapreduce自定义排序WritableComparable
本文发表于本人博客. 今天继续写练习题,上次对分区稍微理解了一下,那根据那个步骤分区.排序.分组.规约来的话,今天应该是要写个排序有关的例子了,那好现在就开始! 说到排序我们可以查看下hadoop源码 ...
- Hadoop MapReduce编程 API入门系列之自定义多种输入格式数据类型和排序多种输出格式(十一)
推荐 MapReduce分析明星微博数据 http://git.oschina.net/ljc520313/codeexample/tree/master/bigdata/hadoop/mapredu ...
- 自定义MapReduce中数据类型
数据类型(都实现了Writable接口) BooleanWritable 布尔类型 ByteWritable 单字节数值 DoubleWritable 双字节数值 FloatWritable 浮点数 ...
- hadoop自定义数据类型
统计某手机数据库的每个手机号的上行数据包数量和下行数据包数量 数据库类型如下: 数据库内容如下: 下面自定义类型SimLines,类似于平时编写的model import java.io.DataIn ...
随机推荐
- mongodb数据库备份恢复-windows系统
备份语法: mongodump命令脚本语法如下: >mongodump -h dbhost -d dbname -o dbdirectory -h: MongDB所在服务器地址,例如:127.0 ...
- POI读取xls和xlsx
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import ...
- 记开发个人图书收藏清单小程序开发(四)DB设计
早上起来,又改动了一下: 主要是,将非常用信息全部拆分出来,让Table尽量的小,小到不能继续拆分了,这样区分DB逻辑.增加了FileBank存储Book的封面图片,统一管理图片资源. 新添加的Typ ...
- ASP.NET常用内置对象之——Server
简介 Server对象是HttpServerUtility的一个实例,也是上下文对象context的一个属性,提供用于处理Web请求的Helper方法. 常用成员 一.Server.MapPath() ...
- @vue-cli3配合element和scss打包的配置
@vue-cli3配合element和scss打包的配置 标签(空格分隔): Vue 使用@vue-cli3初始化项目,且使用ElementUI,然后按照官方文档自定义主题. 自己的scss全局变量文 ...
- Nginx能干啥?
Nginx能干的事很多,很强大.官方文档详见点我. Nginx的命令行参数 -? | -h 打印帮助信息 -c file 读取指定配置文件,而不是默认的nginx.conf -g directives ...
- yii2.0里别名的定义
别名用来表示文件路径和URL,为了避免在代码中硬编码一些绝对路径和URL,一个别名必须以‘@’符开头. 用Yii::setAlias()的方法来设置: //文件别名 Yii::setAlias('@f ...
- nginx知识图谱
nginx启动起来后有两个进程,一个是主进程,一个是守护进程
- [EffectiveC++]item36:绝不重新定义继承而来的non-virtual函数
- python进阶介绍(进阶1)
转载请标明出处: http://www.cnblogs.com/why168888/p/6411664.html 本文出自:[Edwin博客园] python进阶介绍(进阶1) 1. python基础 ...