一 自定义数据类型的实现

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;
} }

最新文章

  1. Hibernate中事务声明
  2. arcgis api for js共享干货系列之一自写算法实现地图量算工具
  3. django 1.10 CSRF验证失败的解决过程
  4. java基础1_标识符,数据类型
  5. Java中的值传递和引用传递
  6. visualSVN Server 设置外网可连接
  7. 【转载】Gambit使用教程
  8. ceph--磁盘和rbd、rados性能测试工具和方法
  9. Hibernate知识点总结
  10. 2.10 工具使用 after effects(图形视频处理软件)
  11. java对String进行sha1加密
  12. 【读书笔记】《Effective Java》——创建和销毁对象
  13. Winform美化MessageBox
  14. Gradle构建Java工程配置详解
  15. PHP中使用jQuery+Ajax实现分页查询多功能操作
  16. jquery(入门篇)无缝滚动
  17. 从父子组件的mounted钩子的同步执行与页面的异步渲染看nextTick的用法
  18. C#:XML操作(简单)
  19. oozie开发注意事项
  20. 160331、使用@Controller注解为什么要配置&lt;mvc:annotation-driven /&gt;

热门文章

  1. SQL Server -&gt;&gt; SQL Server 2016新特性之 -- sp_set_session_context存储过程和SESSION_CONTEXT函数
  2. java excel转pdf 工具类
  3. 全文检索之solr学习
  4. June 20th 2017 Week 25th Tuesday
  5. [转]Android开源项目收藏分享
  6. 在switch中的case语句中声明变量编译出错的解决方案
  7. 三.Shell脚本提取文件名称和所在的目录
  8. Windows环境下ELK简单搭建记录
  9. 剑指offer5 从尾到头打印链表
  10. write函数过程解析