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 ...
随机推荐
- 使用Math.random()函数生成n到m间的随机数字
使用js生成n到m间的随机数字,主要目的是为后期的js生成验证码做准备,Math.random()函数返回0和1之间的伪随机数 讲解: 本文讲解如何使用js生成n到m间的随机数字,主要目的是为后期的j ...
- Spring 切面优先级(5)
之前我们提过的应用场景,一个原始对象可能会需要插入多个切面,如果我们按前几篇博客文章介绍的方法完成切面及其通知的注解声明,那么它的执行顺序是怎么样的呢? 本文将介绍AspectJ的切面如何划分优先级 ...
- 3.在vm上安装centos 7
在vm上安装centos 7 1.文件 → 新建虚拟机 3.选择安装Linux系统 4. 虚拟机命名,并选择安装的文件夹 5.选择分配的处理器 6.使用网络地址转换 7.默写选项 9.新建虚拟机 10 ...
- tensorboard在Mac OS X系统环境下如何启动
再次必须写一篇博客,一次来说明这打开tensorboard的艰难之路,遇到了好多错误,真的是走了好多弯路,最后还是解决了 一开始总是报错,不知道是为什么,其实还是自己没有看懂原理,就冲动的开始招呼画瓢 ...
- 【学术篇】oj.jzxx.net2701 无根树
这是一道来自OIerBBS的题目.. 原帖地址:http://www.oierbbs.com/forum.php?mod=viewthread&tid=512?fromuid=71 (似乎是个 ...
- Android开发 EditText的开发记录
设置显示内容与隐藏内容 if (isChecked){ editPassword.setTransformationMethod(HideReturnsTransformationMethod.get ...
- Python-数据分析模块
目录 numpy 模块 matplotlib 模块 pandas 模块 numpy 模块 numpy 模块主要用来做数据分析,对numpy数组 进行科学运算 主要方法和常用属性,都是用numpy 生成 ...
- Ubuntu clion下载及激活
1.下载 方法:去官网下载clion https://www.jetbrains.com/clion/download/#section=linux 或者使用我上传的百度网盘链接: https:// ...
- VS code 设置侧边栏字体大小
1.代码改写,进入默认安装的如下路径 C:\Users\Administrator\AppData\Local\Programs\Microsoft VS Code\resources\app\out ...
- pytorch基础2
下面是常见函数的代码例子 import torch import numpy as np print("分割线---------------------------------------- ...