Hadoop Serialization -- hadoop序列化详解 (3)【ObjectWritable,集合Writable以及自定义的Writable】
回顾:
woDArrayWritable 的使用都必须实例化相同的类,这是在构造时指定的,如下所示:
ArrayWritable 足够了,但是存储不间的类型在一个单列表中,可以使用GenericWritable 封装到ArrayWritable 中。同时,也可以用MapWritable 的思路写一个通用的ListWritable。
Writable 应用已得到很好的优化,但为了对付更复杂的结构, 最好创建一个新的Writable 类型,而不是使用已有的类型。为了横示如何创建一个自定义的Writable ,我们编写了一个表示一对字符串的实现,名为TextPair:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
// cc TextPair A Writable implementation that stores a pair of Text objects // cc TextPairComparator A RawComparator for comparing TextPair byte representations // cc TextPairFirstComparator A custom RawComparator for comparing the first field of TextPair byte representations // vv TextPair import java.io.*; import org.apache.hadoop.io.*; public class TextPair implements WritableComparable<TextPair> { private Text first; private Text second; //* 必须有默认的构造器皿,这样Mapreduce方法才能创建对象,然后通过readFields方法从序列化的数据流中读出进行赋值 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); } public void set(Text first, Text second) { this .first = first; this .second = second; } public Text getFirst() { return first; } public Text getSecond() { return second; } /** @Override public void write(DataOutput out) throws IOException { first.write(out); second.write(out); } //同上调用成员对象本身的readFields方法,从输入流中反序列化每一个成员对象
@Override public void readFields(DataInput in) throws IOException { first.readFields(in); second.readFields(in); } /*MapReduce需要一个分割者(Partitioner)把map的输出作为输入分成一块块的喂给多个reduce)
* 默认的是HashPatitioner,他是通过对象的hashcode函数进行分割,所以hashCode的好坏决定 * 了分割是否均匀,他是一个很关键性的方法。 /
@Override public int hashCode() { return first.hashCode() * 163 + second.hashCode(); } @Override public boolean equals(Object o) { if (o instanceof TextPair) { TextPair tp = (TextPair) o; return first.equals(tp.first) && second.equals(tp.second); } return false ; } //* 如果你想自定义TextOutputformat作为输出格式时的输出,你需要重写toString方法
@Override public String toString() { return first + "\t" + second; } // * implements WritableComparable必须要实现的方法,用于比较 排序 @Override public int compareTo(TextPair tp) { int cmp = first.compareTo(tp.first); if (cmp != 0 ) { return cmp; } return second.compareTo(tp.second); } } |
是易变的、经常重用的,所以我们应该尽量避免在write() 或readFields ()方法中分配对象。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public static class Comparator extends WritableComparator {
private static final Text.Comparator TEXT_COMPARATOR = new Text.Comparator(); public Comparator() { super (TextPair. class ); } @Override public int compare( byte [] b1, int s1, int l1, byte [] b2, int s2, int l2) { try { /**
* Text是标准的UTF-8字节流,
* 由一个变长整形开头表示Text中文本所需要的长度,接下来就是文本本身的字节数组
* decodeVIntSize返回变长 整形的长度,readVInt 表示 文本字节数组的长度,加起来就是第一个成员first的长度
*/
int firstL1 = WritableUtils.decodeVIntSize(b1[s1]) + readVInt(b1, s1); int firstL2 = WritableUtils.decodeVIntSize(b2[s2]) + readVInt(b2, s2); int cmp = TEXT_COMPARATOR.compare(b1, s1, firstL1, b2, s2, firstL2);//首先比较first if (cmp != 0 ) { return cmp; } //如果first一样,那就比较second second的位置要在s1的位置上加firstL1,长度要总长度减去第一个first的长度
return TEXT_COMPARATOR.compare(b1, s1 + firstL1, l1 - firstL1, b2, s2 + firstL2, l2 - firstL2);} catch (IOException e) { throw new IllegalArgumentException(e); } } } static { WritableComparator.define(TextPair. class , new Comparator());//定义我们compare用哪个 } |
画了一个简图帮助大家理解:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
// cc TextPair A Writable implementation that stores a pair of Text objects // cc TextPairComparator A RawComparator for comparing TextPair byte representations // cc TextPairFirstComparator A custom RawComparator for comparing the first field of TextPair byte representations // vv TextPair import java.io.*; import org.apache.hadoop.io.*; public class TextPair implements WritableComparable<TextPair> { private Text first; private Text second; //* 必须有默认的构造器皿,这样Mapreduce方法才能创建对象,然后通过readFields方法从序列化的数据流中读出进行赋值 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); } public void set(Text first, Text second) { this .first = first; this .second = second; } public Text getFirst() { return first; } public Text getSecond() { return second; } /** * 通过成员对象本身的write方法,序列化每一个成员对象到输出流中 @Override public void write(DataOutput out) throws IOException { first.write(out); second.write(out); } //同上调用成员对象本身的readFields方法,从输入流中反序列化每一个成员对象
@Override public void readFields(DataInput in) throws IOException { first.readFields(in); second.readFields(in); } /*MapReduce需要一个分割者(Partitioner)把map的输出作为输入分成一块块的喂给多个reduce)
* 默认的是HashPatitioner,他是通过对象的hashcode函数进行分割,所以hashCode的好坏决定 * 了分割是否均匀,他是一个很关键性的方法。 /
@Override public int hashCode() { return first.hashCode() * 163 + second.hashCode(); } @Override public boolean equals(Object o) { if (o instanceof TextPair) { TextPair tp = (TextPair) o; return first.equals(tp.first) && second.equals(tp.second); } return false ; } //* 如果你想自定义TextOutputformat作为输出格式时的输出,你需要重写toString方法
@Override public String toString() { return first + "\t" + second; } // * implements WritableComparable必须要实现的方法,用于比较 排序 @Override public int compareTo(TextPair tp) { int cmp = first.compareTo(tp.first); if (cmp != 0 ) { return cmp; } return second.compareTo(tp.second); } // ^^ TextPair // vv TextPairComparator public static class Comparator extends WritableComparator { private static final Text.Comparator TEXT_COMPARATOR = new Text.Comparator(); public Comparator() { super (TextPair. class ); } @Override public int compare( byte [] b1, int s1, int l1, byte [] b2, int s2, int l2) { try { int firstL1 = WritableUtils.decodeVIntSize(b1[s1]) + readVInt(b1, s1); int firstL2 = WritableUtils.decodeVIntSize(b2[s2]) + readVInt(b2, s2); int cmp = TEXT_COMPARATOR.compare(b1, s1, firstL1, b2, s2, firstL2); if (cmp != 0 ) { return cmp; } return TEXT_COMPARATOR.compare(b1, s1 + firstL1, l1 - firstL1, b2, s2 + firstL2, l2 - firstL2); } catch (IOException e) { throw new IllegalArgumentException(e); } } } static { WritableComparator.define(TextPair. class , new Comparator());//注册WritableComparator } // ^^ TextPairComparator // vv TextPairFirstComparator自定义实现的comparator public static class FirstComparator extends WritableComparator { private static final Text.Comparator TEXT_COMPARATOR = new Text.Comparator(); public FirstComparator() { super (TextPair. class ); } @Override public int compare( byte [] b1, int s1, int l1, byte [] b2, int s2, int l2) { try { int firstL1 = WritableUtils.decodeVIntSize(b1[s1]) + readVInt(b1, s1); int firstL2 = WritableUtils.decodeVIntSize(b2[s2]) + readVInt(b2, s2); return TEXT_COMPARATOR.compare(b1, s1, firstL1, b2, s2, firstL2); } catch (IOException e) { throw new IllegalArgumentException(e); } } @Override public int compare(WritableComparable a, WritableComparable b) { if (a instanceof TextPair && b instanceof TextPair) { return ((TextPair) a).first.compareTo(((TextPair) b).first); } return super .compare(a, b); } } // ^^ TextPairFirstComparator // vv TextPair } // ^^ TextPair |
Hadoop Serialization -- hadoop序列化详解 (3)【ObjectWritable,集合Writable以及自定义的Writable】的更多相关文章
- hadoop应用开发技术详解
<大 数据技术丛书:Hadoop应用开发技术详解>共12章.第1-2章详细地介绍了Hadoop的生态系统.关键技术以及安装和配置:第3章是 MapReduce的使用入门,让读者了解整个开发 ...
- 《Hadoop应用开发技术详解》
<Hadoop应用开发技术详解> 基本信息 作者: 刘刚 丛书名: 大数据技术丛书 出版社:机械工业出版社 ISBN:9787111452447 上架时间:2014-1-10 出版日期:2 ...
- Hadoop Hive sql语法详解
Hadoop Hive sql语法详解 Hive 是基于Hadoop 构建的一套数据仓库分析系统,它提供了丰富的SQL查询方式来分析存储在Hadoop 分布式文件系统中的数据,可以将结构 化的数据文件 ...
- Hadoop生态圈-Kafka配置文件详解
Hadoop生态圈-Kafka配置文件详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.默认kafka配置文件内容([yinzhengjie@s101 ~]$ more /s ...
- Hadoop基础-Idea打包详解之手动添加依赖(SequenceFile的压缩编解码器案例)
Hadoop基础-Idea打包详解之手动添加依赖(SequenceFile的压缩编解码器案例) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.编辑配置文件(pml.xml)(我 ...
- 详解如何在Laravel中增加自定义全局函数
http://www.php.cn/php-weizijiaocheng-383928.html 如何在Laravel中增加自定义全局函数?在我们的应用里经常会有一些全局都可能会用的函数,我们应该怎么 ...
- Hadoop MapReduce执行过程详解(带hadoop例子)
https://my.oschina.net/itblog/blog/275294 摘要: 本文通过一个例子,详细介绍Hadoop 的 MapReduce过程. 分析MapReduce执行过程 Map ...
- 【大数据】Linux下安装Hadoop(2.7.1)详解及WordCount运行
一.引言 在完成了Storm的环境配置之后,想着鼓捣一下Hadoop的安装,网上面的教程好多,但是没有一个特别切合的,所以在安装的过程中还是遇到了很多的麻烦,并且最后不断的查阅资料,终于解决了问题,感 ...
- hadoop之hdfs命令详解
本篇主要对hadoop命令和hdfs命令进行阐述,yarn命令会在之后的文章中体现 hadoop fs命令可以用于其他文件系统,不止是hdfs文件系统内,也就是说该命令的使用范围更广可以用于HDFS. ...
随机推荐
- Kali Linux安装SSH Server
Kali Linux默认并没有安装SSH服务,为了实现远程登录Kali Linux,我们需要安装SSH服务. 安装 OpenSSH Server # apt-get install openssh-s ...
- POJ 1265 Area (pick定理)
题目大意:已知机器人行走步数及每一步的坐标变化量,求机器人所走路径围成的多边形的面积.多边形边上和内部的点的数量. 思路:叉积求面积,pick定理求点. pick定理:面积=内部点数+边上点数/2-1 ...
- CF632E: Thief in a Shop(快速幂+NTT)(存疑)
A thief made his way to a shop. As usual he has his lucky knapsack with him. The knapsack can contai ...
- python库之_thread
官方参考文档:https://docs.python.org/3.7/library/_thread.html _thread库方法 (1) _thread.error (2)_thread.Lock ...
- python笔记-4(装饰器、生成器、迭代器)
一.熟练掌握装饰器的原理 (在装饰器学习的过程中,查了看了很多资料,个人感觉走了很多的弯路,这个笔记,分享我的理解,希望能帮助到一些人.本文对装饰器的描述,侧重点是条理与逻辑思路,想通过从无到有的方式 ...
- JDBC 2 封装
1 封装 新建类 DBConnUtil ,新建database.properties 文件储存链接信息如下所示 jdbcDriver=com.mysql.jdbc.Driver jdbcUrl=jd ...
- 转载.Avalon-MM 阿窝龙妹妹应用笔记
Avalon Interface Special http://www.altera.com.cn/literature/manual/mnl_avalon_spec.pdf Avalon总线是SOP ...
- 使用MSBuild实现完整daily build流程
一.MSBuild 在微软软件开发中,每日构建是最重要的过程之一,被称为微软产品开发的“心跳”.简单来看,每天构建系统将整个产品解决方案完整构建一遍,生成的目标文件和安装文件被放置在一个共享位置.接着 ...
- 关于UNIDAC连接SQLITE3的心得笔记
关于查询某个SQLITE3DB的所有表单的语句: UniQuery1.SQL.Add('SELECT * FROM sqlite_master'); 关于UNIDAC提交数据: //在提交数据之前,必 ...
- 让Delphi XE5跟其他版本的Delphi共存
找到Delphi XE5的安装根目录 .... \Program Files (x86)\Embarcadero\RAD Studio\12.0\bin下的cglm.ini文件, 打开cglm.i ...