Hadoop Serialization hadoop序列化详解(最新版) (1)【java和hadoop序列化比较和writable接口】
|
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
|
public class Person implements Serializable { private String name = null; private Integer age = null; private Gender gender = null; public Person() { System.out.println("none-arg constructor"); } public Person(String name, Integer age, Gender gender) { System.out.println("arg constructor"); this.name = name; this.age = age; this.gender = gender; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Gender getGender() { return gender; } public void setGender(Gender gender) { this.gender = gender; } @Override public String toString() { return "[" + name + ", " + age + ", " + gender + "]"; }} |
SimpleSerial,是一个简单的序列化程序,它先将一个Person对象保存到文件person.out中,然后再从该文件中读出被存储的Person对象,并打印该对象。|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public class SimpleSerial { public static void main(String[] args) throws Exception { File file = new File("person.out"); ObjectOutputStream oout = new ObjectOutputStream(new FileOutputStream(file)); Person person = new Person("John", 101, Gender.MALE); oout.writeObject(person); oout.close(); ObjectInputStream oin = new ObjectInputStream(new FileInputStream(file)); Object newPerson = oin.readObject(); // 没有强制转换到Person类型 oin.close(); System.out.println(newPerson); }} |
上述程序的输出的结果为:arg constructor[John, 31, MALE]这就是序列化。序列化的对象,他们超越了JVM的生死,不顾生他们的母亲,化作永恒。
hadoop通讯格式需求
hadoop存储格式需求
Java的序列化机制的缺点就是计算量开销大,且序列化的结果体积大太,有时能达到对象大小的数倍乃至十倍。它的引用机制也会导致大文件不能分割的问题。这些缺点使得Java的序列化机制对Hadoop来说是不合适的。于是Hadoop根据自己上门的需求设计了自己的序列化机制。
|
1
2
3
4
5
|
package org.apache.hadoop.io;public interface Writable { void write(DataOutput out) throws IOException; void readFields(DataInput in) throws IOException;} |
WritableComparable实现Writable,Comparable接口
- package org.apache.hadoop.io;
- public interface WritableComparable <T> extends org.apache.hadoop.io.Writable, java.lang.Comparable<T> {
- }
- package org.apache.hadoop.io;
- public interface RawComparator <T> extends java.util.Comparator<T> {
- int compare(byte[] bytes, int i, int i1, byte[] bytes1, int i2, int i3);
- }
它可以做到,不先反序列化就可以直接比较二进制字节流的大小:
|
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
|
public class TestComparator { RawComparator<IntWritable> comparator; IntWritable w1; IntWritable w2; /** * 获得IntWritable的comparator,并初始化两个IntWritable */ @Before public void init() { comparator = WritableComparator.get(IntWritable.class); w1 = new IntWritable(163); w2 = new IntWritable(76); } /** * 比较两个对象大小 */ @Test public void testComparator() { Assert.assertEquals(comparator.compare(w1, w2) > 0, true); } /** * 序列号后进行直接比较 * * @throws IOException */ @Test public void testcompare() throws IOException { byte[] b1 = serialize(w1); byte[] b2 = serialize(w2); Assert.assertTrue(comparator.compare(b1, 0, b1.length, b2, 0, b2.length) > 0); } /** * 将一个实现了Writable接口的对象序列化成字节流 * * @param writable * @return * @throws java.io.IOException */ public static byte[] serialize(Writable writable) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); DataOutputStream dataOut = new DataOutputStream(out); writable.write(dataOut); dataOut.close(); return out.toByteArray(); }} |
|
1
2
3
4
5
6
7
|
public static byte[] serialize(Writable writable) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); DataOutputStream dataOut = new DataOutputStream(out); writable.write(dataOut); dataOut.close(); return out.toByteArray(); } |
以及反序列化:
|
1
2
3
4
5
6
7
8
|
public static byte[] deserialize(Writable writable, byte[] bytes)throws IOException {ByteArrayInputStream in = new ByteArrayInputStream(bytes);DataInputStream dataIn = new DataInputStream(in);writable.readFields(dataIn);dataIn.close();return bytes;} |
Charles 2015-12-23 于P.P
Hadoop Serialization hadoop序列化详解(最新版) (1)【java和hadoop序列化比较和writable接口】的更多相关文章
- Hadoop Serialization(third edition)hadoop序列化详解(最新版) (1)
初学java的人肯定对java序列化记忆犹新.最开始很多人并不会一下子理解序列化的意义所在.这样子是因为很多人还是对java最底层的特性不是特别理解,当你经验丰富,对java理解更加深刻之后,你就会发 ...
- Hadoop Hive sql语法详解
Hadoop Hive sql语法详解 Hive 是基于Hadoop 构建的一套数据仓库分析系统,它提供了丰富的SQL查询方式来分析存储在Hadoop 分布式文件系统中的数据,可以将结构 化的数据文件 ...
- hadoop应用开发技术详解
<大 数据技术丛书:Hadoop应用开发技术详解>共12章.第1-2章详细地介绍了Hadoop的生态系统.关键技术以及安装和配置:第3章是 MapReduce的使用入门,让读者了解整个开发 ...
- 《Hadoop应用开发技术详解》
<Hadoop应用开发技术详解> 基本信息 作者: 刘刚 丛书名: 大数据技术丛书 出版社:机械工业出版社 ISBN:9787111452447 上架时间:2014-1-10 出版日期:2 ...
- Hadoop生态圈-Kafka配置文件详解
Hadoop生态圈-Kafka配置文件详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.默认kafka配置文件内容([yinzhengjie@s101 ~]$ more /s ...
- Hadoop基础-Idea打包详解之手动添加依赖(SequenceFile的压缩编解码器案例)
Hadoop基础-Idea打包详解之手动添加依赖(SequenceFile的压缩编解码器案例) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.编辑配置文件(pml.xml)(我 ...
- hadoop之hdfs命令详解
本篇主要对hadoop命令和hdfs命令进行阐述,yarn命令会在之后的文章中体现 hadoop fs命令可以用于其他文件系统,不止是hdfs文件系统内,也就是说该命令的使用范围更广可以用于HDFS. ...
- Hadoop Serialization -- hadoop序列化详解 (3)【ObjectWritable,集合Writable以及自定义的Writable】
前瞻:本文介绍ObjectWritable,集合Writable以及自定义的Writable TextPair 回顾: 前面了解到hadoop本身支持java的基本类型的序列化,并且提供相应的包装实现 ...
- Hadoop Serialization -- hadoop序列化详解 (2)
回顾: 回顾序列化,其实原书的结构很清晰,我截图给出书中的章节结构: 序列化最主要的,最底层的是实现writable接口,wiritable规定读和写的游戏规则 (void write(DataOut ...
随机推荐
- 第三方模块requests下载
requests下载 英文网站 http://docs.python-requests.org/en/master/ 中文网站 http://docs.python-requests.org/zh_C ...
- mybatis-sql语句传参
MyBatis中的映射语句有一个parameterType属性来制定输入参数的类型.但是parameterType属性只可以写一个参数,所以如果我们想给映射语句传入多个参数的话,我们可以将所有的输入参 ...
- 腾讯bugly接入插件(CocosCreator)
下载: plugin-bugly.zip (1.4 MB) 插件开源地址: https://github.com/tidys/CocosCreatorPlugins/tree/master/packa ...
- 2016.9.10初中部上午NOIP普及组比赛总结
2016.9.10初中部上午NOIP普及组比赛总结 链接:https://jzoj.net/junior/#contest/home/1340 好不爽!翻车了!不过排名差不多在中间偏上一点, 还好不是 ...
- ActiveMQ 基础
Apache ActiveMQ是Apache软件基金会所研发的开放源代码消息中间件:由于ActiveMQ是一个纯Java程序,因此只需要操作系统支持Java虚拟机,ActiveMQ便可执行.它能很好地 ...
- htaccess apache重定向学习
1.推荐博客:http://www.cnblogs.com/adforce/archive/2012/11/23/2784664.html 2.测试工具:https://htaccess.madewi ...
- 为什么Java中的String是设计成不可变的?(Why String is immutable in java)
There are many reasons due to the string class has been made immutable in Java. These reasons in vie ...
- python3-常用模块之time
import time time模块主要是处理各种类型的时间 常用方法 1.time.sleep(secs) (线程)推迟指定的时间运行,单位为秒. 2.time.time() 获取当前时间戳 时间戳 ...
- Vuejs之Component slot 插槽详解
Vuejs的component的数据进行了沙箱隔离,除js全局变量如Math, Date之类外无法访问用户自定义的变量,所以使用component写组件或嵌套组件时明白变量的访问非常重要 编译作用域 ...
- css3之文本text-overflow 与 word-wrap, word-break
CSS3 Text Overflow属性 CSS3文本溢出属性指定应向用户如何显示溢出内容 语法: text-overflow:clip | ellipsis 但是text-overflow只是用来说 ...