【转】hadoop深入研究:(十一)——序列化与Writable实现
原文链接 http://blog.csdn.net/lastsweetop/article/details/9249411
所有源码在github上,https://github.com/lastsweetop/styhadoop
简介
java原生类型
Text类型
检索的不同
- @Test
- public void testTextIndex(){
- Text text=new Text("hadoop");
- Assert.assertEquals(text.getLength(), 6);
- Assert.assertEquals(text.getBytes().length, 6);
- Assert.assertEquals(text.charAt(2),(int)'d');
- Assert.assertEquals("Out of bounds",text.charAt(100),-1);
- }
Text还有个find方法,类似String里indexOf方法
- @Test
- public void testTextFind() {
- Text text = new Text("hadoop");
- Assert.assertEquals("find a substring",text.find("do"),2);
- Assert.assertEquals("Find first 'o'",text.find("o"),3);
- Assert.assertEquals("Find 'o' from position 4 or later",text.find("o",4),4);
- Assert.assertEquals("No match",text.find("pig"),-1);
- }
Unicode的不同
- @Test
- public void string() throws UnsupportedEncodingException {
- String str = "\u0041\u00DF\u6771\uD801\uDC00";
- Assert.assertEquals(str.length(), 5);
- Assert.assertEquals(str.getBytes("UTF-8").length, 10);
- Assert.assertEquals(str.indexOf("\u0041"), 0);
- Assert.assertEquals(str.indexOf("\u00DF"), 1);
- Assert.assertEquals(str.indexOf("\u6771"), 2);
- Assert.assertEquals(str.indexOf("\uD801\uDC00"), 3);
- Assert.assertEquals(str.charAt(0), '\u0041');
- Assert.assertEquals(str.charAt(1), '\u00DF');
- Assert.assertEquals(str.charAt(2), '\u6771');
- Assert.assertEquals(str.charAt(3), '\uD801');
- Assert.assertEquals(str.charAt(4), '\uDC00');
- Assert.assertEquals(str.codePointAt(0), 0x0041);
- Assert.assertEquals(str.codePointAt(1), 0x00DF);
- Assert.assertEquals(str.codePointAt(2), 0x6771);
- Assert.assertEquals(str.codePointAt(3), 0x10400);
- }
- @Test
- public void text() {
- Text text = new Text("\u0041\u00DF\u6771\uD801\uDC00");
- Assert.assertEquals(text.getLength(), 10);
- Assert.assertEquals(text.find("\u0041"), 0);
- Assert.assertEquals(text.find("\u00DF"), 1);
- Assert.assertEquals(text.find("\u6771"), 3);
- Assert.assertEquals(text.find("\uD801\uDC00"), 6);
- Assert.assertEquals(text.charAt(0), 0x0041);
- Assert.assertEquals(text.charAt(1), 0x00DF);
- Assert.assertEquals(text.charAt(3), 0x6771);
- Assert.assertEquals(text.charAt(6), 0x10400);
- }
这样一比较就很明显了。
Text的迭代
- package com.sweetop.styhadoop;
- import org.apache.hadoop.io.Text;
- import java.nio.ByteBuffer;
- /**
- * Created with IntelliJ IDEA.
- * User: lastsweetop
- * Date: 13-7-9
- * Time: 下午5:00
- * To change this template use File | Settings | File Templates.
- */
- public class TextIterator {
- public static void main(String[] args) {
- Text text = new Text("\u0041\u00DF\u6771\uD801\udc00");
- ByteBuffer buffer = ByteBuffer.wrap(text.getBytes(), 0, text.getLength());
- int cp;
- while (buffer.hasRemaining() && (cp = Text.bytesToCodePoint(buffer)) != -1) {
- System.out.println(Integer.toHexString(cp));
- }
- }
- }
Text的修改
- @Test
- public void testTextMutability() {
- Text text = new Text("hadoop");
- text.set("pig");
- Assert.assertEquals(text.getLength(), 3);
- Assert.assertEquals(text.getBytes().length, 3);
- }
但要注意的就是,在某些情况下Text的getBytes方法返回的字节数组的长度和Text的getLength方法返回的长度不一致。因此,在调用getBytes()方法的同时最好也调用一下getLength方法,这样你就知道在字节数组里有多少有效的字符。
- @Test
- public void testTextMutability2() {
- Text text = new Text("hadoop");
- text.set(new Text("pig"));
- Assert.assertEquals(text.getLength(),3);
- Assert.assertEquals(text.getBytes().length,6);
- }
BytesWritable类型
- @Test
- public void testByteWritableSerilizedFromat() throws IOException {
- BytesWritable bytesWritable=new BytesWritable(new byte[]{3,5});
- byte[] bytes=SerializeUtils.serialize(bytesWritable);
- Assert.assertEquals(StringUtils.byteToHexString(bytes),"000000020305");
- }
和Text一样,ByteWritable也可以通过set方法修改,getLength返回的大小是真实大小,而getBytes返回的大小确不是。
- <span style="white-space:pre"> </span>bytesWritable.setCapacity(11);
- bytesWritable.setSize(4);
- Assert.assertEquals(4,bytesWritable.getLength());
- Assert.assertEquals(11,bytesWritable.getBytes().length);
NullWritable类型
- package com.sweetop.styhadoop;
- import org.apache.hadoop.io.NullWritable;
- import org.apache.hadoop.util.StringUtils;
- import java.io.IOException;
- /**
- * Created with IntelliJ IDEA.
- * User: lastsweetop
- * Date: 13-7-16
- * Time: 下午9:23
- * To change this template use File | Settings | File Templates.
- */
- public class TestNullWritable {
- public static void main(String[] args) throws IOException {
- NullWritable nullWritable=NullWritable.get();
- System.out.println(StringUtils.byteToHexString(SerializeUtils.serialize(nullWritable)));
- }
- }
ObjectWritable类型
ObjectWritable是其他类型的封装类,包括java原生类型,String,enum,Writable,null等,或者这些类型构成的数组。当你的一个field有多种类型时,ObjectWritable类型的用处就发挥出来了,不过有个不好的地方就是占用的空间太大,即使你存一个字母,因为它需要保存封装前的类型,我们来看瞎示例:
- package com.sweetop.styhadoop;
- import org.apache.hadoop.io.ObjectWritable;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.util.StringUtils;
- import java.io.IOException;
- /**
- * Created with IntelliJ IDEA.
- * User: lastsweetop
- * Date: 13-7-17
- * Time: 上午9:14
- * To change this template use File | Settings | File Templates.
- */
- public class TestObjectWritable {
- public static void main(String[] args) throws IOException {
- Text text=new Text("\u0041");
- ObjectWritable objectWritable=new ObjectWritable(text);
- System.out.println(StringUtils.byteToHexString(SerializeUtils.serialize(objectWritable)));
- }
- }
仅仅是保存一个字母,那么看下它序列化后的结果是什么:
- 00196f72672e6170616368652e6861646f6f702e696f2e5465787400196f72672e6170616368652e6861646f6f702e696f2e546578740141
太浪费空间了,而且类型一般是已知的,也就那么几个,那么它的代替方法出现,看下一小节
GenericWritable类型
- package com.sweetop.styhadoop;
- import org.apache.hadoop.io.GenericWritable;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.io.Writable;
- class MyWritable extends GenericWritable {
- MyWritable(Writable writable) {
- set(writable);
- }
- public static Class<? extends Writable>[] CLASSES=null;
- static {
- CLASSES= (Class<? extends Writable>[])new Class[]{
- Text.class
- };
- }
- @Override
- protected Class<? extends Writable>[] getTypes() {
- return CLASSES; //To change body of implemented methods use File | Settings | File Templates.
- }
- }
然后输出序列化后的结果
- package com.sweetop.styhadoop;
- import org.apache.hadoop.io.IntWritable;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.io.VIntWritable;
- import org.apache.hadoop.util.StringUtils;
- import java.io.IOException;
- /**
- * Created with IntelliJ IDEA.
- * User: lastsweetop
- * Date: 13-7-17
- * Time: 上午9:51
- * To change this template use File | Settings | File Templates.
- */
- public class TestGenericWritable {
- public static void main(String[] args) throws IOException {
- Text text=new Text("\u0041\u0071");
- MyWritable myWritable=new MyWritable(text);
- System.out.println(StringUtils.byteToHexString(SerializeUtils.serialize(text)));
- System.out.println(StringUtils.byteToHexString(SerializeUtils.serialize(myWritable)));
- }
- }
结果是:
- 024171
- 00024171
GenericWritable的序列化只是把类型在type数组里的索引放在了前面,这样就比ObjectWritable节省了很多空间,所以推荐大家使用GenericWritable
集合类型的Writable
ArrayWritable和TwoDArrayWritable
- package com.sweetop.styhadoop;
- import org.apache.hadoop.io.ArrayWritable;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.io.Writable;
- import org.apache.hadoop.util.StringUtils;
- import java.io.IOException;
- /**
- * Created with IntelliJ IDEA.
- * User: lastsweetop
- * Date: 13-7-17
- * Time: 上午11:14
- * To change this template use File | Settings | File Templates.
- */
- public class TestArrayWritable {
- public static void main(String[] args) throws IOException {
- ArrayWritable arrayWritable=new ArrayWritable(Text.class);
- arrayWritable.set(new Writable[]{new Text("\u0071"),new Text("\u0041")});
- System.out.println(StringUtils.byteToHexString(SerializeUtils.serialize(arrayWritable)));
- }
- }
看下输出:
- 0000000201710141
可知,ArrayWritable以一个整型开始表示数组长度,然后数组里的元素一一排开。
MapWritable和SortedMapWritable
转载于:https://www.cnblogs.com/ihongyan/p/5137275.html
【转】hadoop深入研究:(十一)——序列化与Writable实现相关推荐
- hadoop中的序列化与Writable类
本文地址:http://www.cnblogs.com/archimedes/p/hadoop-writable-class.html,转载请注明源地址. hadoop中自带的org.apache.h ...
- hadoop深入研究:(五)——Archives
转载请注明来源地址:http://blog.csdn.net/lastsweetop/article/details/9123155 简介 我们在hadoop深入研究:(一)--hdfs介绍里已讲过, ...
- Hadoop IO 文件压缩 序列化
文件中使用getcodec()方法,可以将文件属性名映射到一个CompressionCodec,该去文件的Path对象当参数. CodecPool使用原生的代码库并且需要在用执行中执行大量解压和压缩, ...
- Hadoop入门(十一)Mapreduce的InputFomrat各种子类
一.TextInputFormat extends FileInputFomrat<LongWritable,Text> 是默认读取文件的切分器,其内的LineRecordReader: ...
- hadoop的Avro数据序列化系统
1.什么是Avro Avro设计用于支持大批量数据交换的应用 Avro可以将数据结构或者对象转换成便于存储或者传输的格式. 为了hadoop的前途考虑,DougCutting主导开发的一套新的序列化系 ...
- hadoop详细笔记(十一) mapreduce数据分析案例之线段重叠案例
1 数据 1,4 2,5 3,4 2,5 2,4 3,4 2,6 2 需求 统计x轴上每个点线段重叠的次数 3 代码实现 package com._51doit.mr.line;import org. ...
- 【大数据之Hadoop】三十一、HDFS集群迁移之Apache和Apache集群间数据拷贝
数据迁移场景: 冷热集群数据分类存储:集群数据整体迁移:数据的准实时同步,目的在于数据的双备份可用. 数据迁移需要考虑的因素: 带宽.性能.增量同步(原始数据文件进行了追加写.被删除或重命名) ...
- java程序员的大数据之路(6):定制的Writable类型
序列化 序列化是指将结构化对象转化为字节流,以便在网络上传输或写入磁盘.反序列化是指将字节流转回结构化对象的过程. 在Hadoop中,系统中多个节点上进程间的通信是通过"远程过程调用&quo ...
- 又双叒叕来更新啦!Hadoop———MapReduce篇
文章目录 MapReduce(计算) MapReduce概述 MapReduce定义 MapReduce的优缺点 核心思想 MapReduce计算程序运行时的相关进程 官方WordCount源码 Ma ...
最新文章
- 日赚 5 亿的腾讯工资又涨了:员工上半年人均月薪 7.8 万
- python【力扣LeetCode算法题库】169-多数元素
- opencv三维重建_基于OpenCV和C++的多视图三维重建
- CTF-Python打包成的exe文件Re逆向
- php大量数据库抽象,数据库的数据抽象有几个级别
- 从鲁班造木鸢到智能控制,图解世界无人机发展简史
- Windows下配置Squid反向代理服务器
- 事情又没有后续,吾真想质问头目,汝是为吾干活吗?
- Atitit.收银系统pos 以及打印功能的行业标准
- Linux系统重要日志文件
- 如何快速构建一个企业revit族库(含插件)
- c#使用word、excel、pdf ——转
- 简单matlab仿真实例教程,simulink仿真教程
- 怎么改自己手机的ip地址
- pr用什么显卡比较好_用 PR 剪辑视频应该用什么 CPU 和显卡?
- Win10提示“为了对电脑进行保护,已经阻止此应用”如何解决
- 小白如何装重装操作系统(使用PE辅助)
- 安卓集成Google登录并进一步获取性别生日手机号等信息
- imx8mqevk OPTEE 全系统构建
- 笔记本计算机在桌面显示器,解决笔记本电脑屏幕出现条纹的五大方法