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 ...
随机推荐
- SQL Server ->> SQL Server 2016重要功能改进之 -- INSERT SELECT时并发插入数据
SQL Server 2016对INSERT INTO XXXX SELECT语句进行了优化,在某些情况下可以触发数据的并行插入,但是要求兼容模式是130(SQL Server 2016)以及在插入的 ...
- 【Leetcode】【Medium】Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- calculate fraction by oracle
QUESTION:When you meet calculate fraction in oracle SOLUTION: 1.Check out their values respectively. ...
- 查看dump oracle数据块查看
alter system dump datafile 8 block 2523; Block dump from disk:buffer tsn: 87 rdba: 0x160dd924 (88/90 ...
- Json.Net 中Linq to JSON的操作
Linq to JSON是用来操作JSON对象的.可以用于快速查询,修改和创建JSON对象.当JSON对象内容比较复杂,而我们仅仅需要其中的一小部分数据时,可以考虑使用Linq to JSON来读取和 ...
- 028class_part2
1.成员修饰符 2.特殊成员 3.metaclass,类的祖宗 ###成员修饰符###公有和私有 #__author:_nbloser #date:2018/1/19 #私有类.对象成员 ...
- 22、整合mybatis
搭建环境: 1).创建工程需要的maven坐标 这个mybatis的starter是mybatis官方出的适应springboot 2).数据连接池的使用 引入Druid数据连接池 <depen ...
- mapper.xml中动态sql抽取重复项
mabatis重点是通过标签对sql灵活的组织,通过配置的方式完成输入 输出映射. 1.对mapper.xml中重复的sql抽取统一维护,以及foreach使用 UserMapperCustom.xm ...
- LVS的DR模式负载均衡
参考项目:http://www.cnblogs.com/along21/p/7833261.html#auto_id_3 LVS的DR模式实现负载均衡 1.环境 lvs-server :192.168 ...
- [转]表单提交:button input submit 的区别
博客转自于 http://harttle.com/2015/08/03/form-submit.html ,同时自己做了稍微改动 最近项目代码中的表单提交的方式已经百花齐放了,现在用这篇文章来整 ...