各种类型的Writable(Text、ByteWritable、NullWritable、ObjectWritable、GenericWritable、ArrayWritable、MapWritable、SortedMapWritable)转
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
各种类型的Writable(Text、ByteWritable、NullWritable、ObjectWritable、GenericWritable、ArrayWritable、MapWritable、SortedMapWritable)转的更多相关文章
- Hadoop Serialization -- hadoop序列化具体解释 (2)【Text,BytesWritable,NullWritable】
回想: 回想序列化,事实上原书的结构非常清晰,我截图给出书中的章节结构: 序列化最基本的,最底层的是实现writable接口,wiritable规定读和写的游戏规则 (void write(DataO ...
- Hadoop Serialization -- hadoop序列化详解 (2)【Text,BytesWritable,NullWritable】
回顾: 回顾序列化,其实原书的结构很清晰,我截图给出书中的章节结构: 序列化最主要的,最底层的是实现writable接口,wiritable规定读和写的游戏规则 (void write(DataOut ...
- mysql列类型char,varchar,text,tinytext,mediumtext,longtext的比较与选择
储存不区分大小写的字符数据 TINYTEXT 最大长度是 255 (2^8 – 1) 个字符. TEXT 最大长度是 65535 (2^16 – 1) 个字符. MEDIUMTEXT 最大长度是 16 ...
- 叼叼叼,HTML5日期(Date)类型和文本(Text)类型互相转换
<input placeholder="From" class="form-control" type="text" onfocus= ...
- 【原创】大叔问题定位分享(12)Spark保存文本类型文件(text、csv、json等)到hdfs时为什么是压缩格式的
问题重现 rdd.repartition(1).write.csv(outPath) 写文件之后发现文件是压缩过的 write时首先会获取hadoopConf,然后从中获取是否压缩以及压缩格式 org ...
- hadoop自带的writable类型
Hadoop 中,并没有使用Java自带的基本类型类(Integer.Float等),而是使用自己开发的类.Hadoop 自带有很多序列化类型,大致分为以下两种: 实现了WritableCompara ...
- Hadoop Serialization -- hadoop序列化详解 (3)【ObjectWritable,集合Writable以及自定义的Writable】
前瞻:本文介绍ObjectWritable,集合Writable以及自定义的Writable TextPair 回顾: 前面了解到hadoop本身支持java的基本类型的序列化,并且提供相应的包装实现 ...
- Mysql 中 text类型和 blog类型的异同
MySQL存在text和blob: (1)相同 在TEXT或BLOB列的存储或检索过程中,不存在大小写转换,当未运行在严格模式时,如果你为BLOB或TEXT列分配一个超过该列类型的最大长度的值值,值被 ...
- mysql的text的类型注意
不要以为text就只有一种类型! Text也分为四种类型:TINYTEXT.TEXT.MEDIUMTEXT和LONGTEXT 其中 TINYTEXT 256 bytes TEXT 65,535 byt ...
随机推荐
- [LeetCode]题解(python):047-Permutations II
题目来源 https://leetcode.com/problems/permutations-ii/ Given a collection of numbers that might contain ...
- Shell Script-读取配置文件
需求 有时候在shell script里面需要一些执行,如果放在程序里面不便于统一管理,并且每一次修改路径都要去script里面改好麻烦,所以统一把路径放在配置文件中方便管理. 问题 如何读取相对应的 ...
- Android 开发-Intent传递普通字符串
假设A传递id到B中 ActivityA: Intent intent=new Intent(); intent.setClass(ActivityA.this,ActivityB.class) ...
- Selenium2学习-034-WebUI自动化实战实例-032-获取页面 body 大小
获取 body 元素大小的方法,非常简单,直接上码,敬请参阅! /** * Get body size * * @author Aaron.ffp * @version V1.0.0: autoSel ...
- linux i2c 设备节点读写
最近需要操作24C02,封装了一下函数方便以后操作. 参考链接: https://my.oschina.net/handawei/blog/68526 http://blog.csdn.net/one ...
- linux命令之echo
echo可以用来输出显示变量和常量,还可以用来写文件. 最简单的方式:使用 echo 命令 #echo abcbedf>>a.txt 将abcdef追加到a.txt文件末尾 往文件中写入内 ...
- <dependency>spring-webmvc</dependency>
Spring 4.2.0.RELEASE版本: <dependency> <groupId>org.springframework</groupId> <ar ...
- Android PickerView滚动选择器的使用方法
手机里设置闹钟需要选择时间,那个选择时间的控件就是滚动选择器,前几天用手机刷了MIUI,发现自带的那个时间选择器效果挺好看的,于是就自己仿写了一个,权当练手.先来看效果: 效果还行吧?实现思路就是自定 ...
- 【转】解决:fatal error C1083: 无法打开预编译头文件
http://blog.csdn.net/aafengyuan/article/details/7988584 是这样的,我创建了一个空项目,并通过"项目属性>C/C++>预编译 ...
- Java基础之集合框架——使用集合Vector<>挑选演员(TryVector)
控制台程序. public class Person implements Comparable<Person> { // Constructor public Person(String ...