一、《Hadoop权威指南》一书中的示例,测试了一下。
定制的Writable类型:TextPair
功能:存储一对Text对象。代码如下:
package testWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.io.WritableComparable; import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; public class TextPair implements WritableComparable<TextPair> { private Text first; private Text second; public TextPair() { set(new Text(), new Text()); } public TextPair(String first, String second) { set(new Text(first), new Text(second)); } public TextPair(Text first, Text second) { set(first, second); } private void set(Text first, Text second) { this.first = first; this.second = second; } @Override public int compareTo(TextPair o) { int i = first.compareTo(o.first); if (i == 0) { return second.compareTo(o.second); } return i; } @Override public void write(DataOutput dataOutput) throws IOException { first.write(dataOutput); second.write(dataOutput); } @Override public void readFields(DataInput dataInput) throws IOException { first.readFields(dataInput); second.readFields(dataInput); } @Override public String toString() { return first + "\t" + second; } }
TextPair类,继承了WritableComparable,分别实现三个方法,compareTo, write,readFields。
write方法:实现序列化; readFields方法:实现反序列化。
当TextPair被用作MapReduce中的键时,需要将数据流反序列化为对象,再调用compareTo进行比较;也可以直接比较序列化得出结果(需要自已定义comparator,继承自WritableComparator,具体参考《Hadoop权威指南》Page.99)
二、定制的Writable:Record (成员变量有int,String类型)
class Record implements WritableComparable<Record> { private int id; private String name; Record() { id = -1; name = "null"; } @Override public int compareTo(Record o) { if (this.id > o.id) return 1; else if (this.id < o.id) return -1; else return 0; } @Override public void write(DataOutput dataOutput) throws IOException { dataOutput.writeInt(id); dataOutput.writeUTF(name); } @Override public void readFields(DataInput dataInput) throws IOException { id = dataInput.readInt(); name = dataInput.readUTF(); } @Override public String toString() { return id + "," + name ; } }
三、使用定制的Writable时需要注意的地方(如下面的代码所示)
static class Reduce extends Reducer<IntWritable, Record, Record, IntWritable> {
@Override
protected void reduce(IntWritable key, Iterable<Record> values, Context context) throws IOException, InterruptedException {
ArrayList<Record> array = new ArrayList<Record>();
for (Record rec : values) {
if (一个条件) {
//使用了values的迭代,不能够直接array.add(),否则array里面的对象都是初始值,得不到修改后的对象值,因此一定要重新创建一个新的对象,很重要
Record record = new Record();
record.id = rec.id;
record.name = rec.name;
array.add(record);
}
}
for (Record rec : array) {
...其他操作
context.write(rec, new IntWritable(1));
}
}
}
相关推荐
mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce ...
4 分别在自编 MapReduce 程序 WordCount 运行过程中和运行结束后查看 MapReduce Web 界面。 5. 分别在自编 MapReduce 程序 WordCount 运行过程中和运行结束后练习 MapReduce Shell 常用命令。 。。
本文对MapReduce中的数据处理模型进行整体说明,分别对输入和输出的各种类及可口进行讲解,从而可以处理比如文件不分片,非文本文件,多个文件合并等问题
MapReduce发明人关于MapReduce的介绍
(2)打开网站localhost:8088和localhost:50070,查看MapReduce任务启动情况 (3)写wordcount代码并把代码生成jar包 (4)运行命令 (1):把linus下的文件放到hdfs上 (2):运行MapReduce (5):查看运行结果 ...
【MapReduce篇07】MapReduce之数据清洗ETL1
简单的在MapReduce中实现两个表的join连接简单的在MapReduce中实现两个表的join连接简单的在MapReduce中实现两个表的join连接
Hadoop 用mapreduce实现Wordcount实例,绝对能用
图解MapReduce,系统介绍Hadoop MapReduce工作过程原理
Map:主要功能是读取经过切割split文件形成一个map任务,分析map任务,得到 中间结构并且将同一类型的中间文件存放在同一个区域内等待特定的reduce程 序读取。 3. Reduce:不同的Reduce读取各个Map得到的特定的中间...
hadoop网站通过SVN下载下来的mapreduce代码。欢迎现在学习!
MapReduce求取行平均值 MapReduce小实例 数据有经过处理已经添加行号的 也有未添加的 行平均值的四种求法
《MapReduce2.0源码分析与编程实战》最后部分介绍了数据挖掘的初步知识,以及不同应用类型的MapReduce2.0编程实战。《MapReduce2.0源码分析与编程实战》强调理论联系实际,帮助读者在掌握MapReduce2.0基本知识和特性...
《MapReduce 2.0源码分析与编程实战》最后部分介绍了数据挖掘的初步知识,以及不同应用类型的MapReduce 2.0编程实战。 《MapReduce 2.0源码分析与编程实战》强调理论联系实际,帮助读者在掌握MapReduce 2.0基本...
08.mapreduce编程案例--流量统计求和--自定义数据类型.mp4
MapReduce的实现细节,对mapreduce的具体实现讲解
MapReduce 设计模式,深入理解MapReduce编程模式,更好的利用MapReduce模型
MapReduce简单程序示例
mapreduce example
mapreduce基本数据读取,通俗易懂。 此项目情景为,遗传关系族谱。 (爷爷 、父母 、孩子) 经典案例