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 ...
随机推荐
- spring boot过滤器FilterRegistrationBean
有2种方式可以实现过滤器 1:通过FilterRegistrationBean实例注册 2:通过@WebFilter注解生效 这里选择第一种,因为第二种不能设置过滤器之间的优先级 为了演示优先级,这里 ...
- <小知识>记录
lis = [2,3,"k",["qwe",20,["k1",["tt",3,"1"]],89],& ...
- 移植 inetd
inetd 的选择及获取 Busybox1.1.3 提供了 inetd 支持.如果读者使用的是较低版本的不提供 inetd 的 Busybox,那么可以考虑使 用 netkit 套件来提供网络服务.强 ...
- 记录一次工作中配置的Mysql主从复制过程
Mysql主从复制教程 1.安装mysql(安装步骤跳过)2.配置密码.(如果忘记密码或者误操作删除了root用户,使用如下命令,没有忘记就跳到3)将skip-grant-tables放在/etc/m ...
- jvisualvm 工具使用【转】
VisualVM 是Netbeans的profile子项目,已在JDK6.0 update 7 中自带(java启动时不需要特定参数,监控工具在bin/jvisualvm.exe). https:// ...
- odoo 下 get_object_reference 函数
get_object_reference是 ir.model.data 模块中下的一个函数 该函数通过调用ir.model.data 模块中另外一个函数 xmlid_lookup 返回结果 def g ...
- Idea安装Mevn
1.下载mevn安装包. 下载地址:http://maven.apache.org/ 点击Download 2.下面这两个选哪个都可以,取决于你用什么方式解压 3.把下载好的安装包解压到一个没有中文的 ...
- KMP算法简明法则
KMP算法也算是相当经典,但是对于初学者来说确实有点绕,大学时候弄明白过后来几年不看又忘记了,然后再弄明白过了两年又忘记了,好在之前理解到了关键点,看了一遍马上又能理解上来.关于这个算法的详解网上文章 ...
- Zuul的容错与回退与Zuul的高可用
容错与回退 复制zuul项目,修改ArtifactId 如zuul-falllback 写Zuul的回退类 @Component public class ZuulFallBackProvider ...
- 菜鸟nginx源码剖析数据结构篇(八) 缓冲区链表ngx_chain_t[转]
菜鸟nginx源码剖析数据结构篇(八) 缓冲区链表 ngx_chain_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.c ...