一 自定义数据类型的实现

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自定义数据类型的更多相关文章

  1. hadoop的自定义数据类型和与关系型数据库交互

    最近有一个需求就是在建模的时候,有少部分数据是postgres的,只能读取postgres里面的数据到hadoop里面进行建模测试,而不能导出数据到hdfs上去. 读取postgres里面的数据库有两 ...

  2. Hadoop mapreduce自定义分组RawComparator

    本文发表于本人博客. 今天接着上次[Hadoop mapreduce自定义排序WritableComparable]文章写,按照顺序那么这次应该是讲解自定义分组如何实现,关于操作顺序在这里不多说了,需 ...

  3. mapreduce 自定义数据类型的简单的应用

    本文以手机流量统计为例: 日志中包含下面字段 现在需要统计手机的上行数据包,下行数据包,上行总流量,下行总流量. 分析:可以以手机号为key 以上4个字段为value传传递数据. 这样则需要自己定义一 ...

  4. [Hadoop] - Mapreduce自定义Counter

    在Hadoop的MR程序开发中,经常需要统计一些map/reduce的运行状态信息,这个时候我们可以通过自定义Counter来实现,这个实现的方式是不是通过配置信息完成的,而是通过代码运行时检查完成的 ...

  5. Hadoop mapreduce自定义分区HashPartitioner

    本文发表于本人博客. 在上一篇文章我写了个简单的WordCount程序,也大致了解了下关于mapreduce运行原来,其中说到还可以自定义分区.排序.分组这些,那今天我就接上一次的代码继续完善实现自定 ...

  6. Hadoop mapreduce自定义排序WritableComparable

    本文发表于本人博客. 今天继续写练习题,上次对分区稍微理解了一下,那根据那个步骤分区.排序.分组.规约来的话,今天应该是要写个排序有关的例子了,那好现在就开始! 说到排序我们可以查看下hadoop源码 ...

  7. Hadoop MapReduce编程 API入门系列之自定义多种输入格式数据类型和排序多种输出格式(十一)

    推荐 MapReduce分析明星微博数据 http://git.oschina.net/ljc520313/codeexample/tree/master/bigdata/hadoop/mapredu ...

  8. 自定义MapReduce中数据类型

    数据类型(都实现了Writable接口) BooleanWritable 布尔类型 ByteWritable 单字节数值 DoubleWritable 双字节数值 FloatWritable 浮点数 ...

  9. hadoop自定义数据类型

    统计某手机数据库的每个手机号的上行数据包数量和下行数据包数量 数据库类型如下: 数据库内容如下: 下面自定义类型SimLines,类似于平时编写的model import java.io.DataIn ...

随机推荐

  1. ASPNET MVC Error 403.14

    今天创建了一个新的ASPNET MVC 项目部署到本地, 生成成功后在浏览器中输入URL却发现报这个错 解决办法: 因为我的站点是4.5的,但是我没有设置Application Pool所以当前还是默 ...

  2. Detecting Client Connection in WCF Long Running Service (Heartbeat Implementation) z

    Download source - 45.3 KB Introduction Hello everyone! This is my first blog on WCF and I hope that ...

  3. [问题记录]cocos2dx编译打包apk过程&问题记录

    目录: 1. 入门 2. 编译 3. 问题 4. 总结 5. 参考 ------------------------------------------------------------------ ...

  4. 《C++ Primer Plus》读书笔记之——处理数据

    本文旨在记录在阅读<C++ Primer Plus>第五版的过程中,一些重点的知识点,方便以后查阅.本文将不断更新...... 一.第三章 处理数据 1.无符号整型不能存储负数值,其优点是 ...

  5. Bash命令行 bash &> >& Bash One-Liners Explained

    Bash One-Liners Explained, Part I: Working with files https://catonmat.net/bash-one-liners-explained ...

  6. 获取所有权windows目录所有权

    Takeown /r /f 盘符:\目录\目录 例如: Takeown /r /f C:\Windows\CSC

  7. 从0开始学CentOS7(1)

    首先,先来几句简介吧.. java开发一枚,总觉得自己的技术提升缓慢... 最近看到同事有在论坛发发自己的心得什么的...我脑中晃出的灵光就是:好记性不如烂笔头,试试吧~ 好了,正式开始了..cent ...

  8. C#图解教程读书笔记(第9章 语句)

    文件头的Using是Using指令,不是using语句 using (TextWriter tw = File.CreateText("xixi.txt")) { tw.Write ...

  9. std::unique实现

    std::unique适用于将排过序的数据结构重复的部分全部放在结尾 但用的时候发现会将原先容器中的内容改掉,看了源码发现这个函数会将不重复的数据结构直接覆盖到前一个重复的位置上,下面看源码 该函数s ...

  10. JavaScript中如何判断两变量是否“相等”?

    1 为什么要判断? 可能有些同学看到这个标题就会产生疑惑,为什么我们要判断JavaScript中的两个变量是否相等,JavaScript不是已经提供了双等号“==”以及三等号“===”给我们使用了吗? ...