hadoop的序列化格式

hadoop自身的序列化存储格式就是实现了Writable接口的类,他只实现了前面两点,压缩和快速。但是不容易扩展,也不跨语言。
我们先来看下Writable接口,Writable接口定义了两个方法:
1.将数据写入到二进制流中
2.从二进制数据流中读取数据
  1. package org.apache.hadoop.io;
  2. public interface Writable {
  3. void write(java.io.DataOutput p1) throws java.io.IOException;
  4. void readFields(java.io.DataInput p1) throws java.io.IOException;
  5. }
我们再来看下Writable接口与序列化和反序列化是如何关联的:
  1. package com.sweetop.styhadoop;
  2. import junit.framework.Assert;
  3. import org.apache.hadoop.io.IntWritable;
  4. import org.apache.hadoop.io.Writable;
  5. import org.apache.hadoop.util.StringUtils;
  6. import org.junit.Before;
  7. import org.junit.Test;
  8. import java.io.*;
  9. /**
  10. * Created with IntelliJ IDEA.
  11. * User: lastsweetop
  12. * Date: 13-7-4
  13. * Time: 下午10:25
  14. * To change this template use File | Settings | File Templates.
  15. */
  16. public class TestWritable {
  17. byte[] bytes=null;
  18. /**
  19. * 初始化一个IntWritable实例,并且调用系列化方法
  20. * @throws IOException
  21. */
  22. @Before
  23. public void init() throws IOException {
  24. IntWritable writable = new IntWritable(163);
  25. bytes = serialize(writable);
  26. }
  27. /**
  28. * 一个IntWritable序列号后的四个字节的字节流
  29. * 并且使用big-endian的队列排列
  30. * @throws IOException
  31. */
  32. @Test
  33. public void testSerialize() throws IOException {
  34. Assert.assertEquals(bytes.length,4);
  35. Assert.assertEquals(StringUtils.byteToHexString(bytes),"000000a3");
  36. }
  37. /**
  38. * 创建一个没有值的IntWritable对象,并且通过调用反序列化方法将bytes的数据读入到它里面
  39. * 通过调用它的get方法,获得原始的值,163
  40. */
  41. @Test
  42. public void testDeserialize() throws IOException {
  43. IntWritable newWritable = new IntWritable();
  44. deserialize(newWritable,bytes);
  45. Assert.assertEquals(newWritable.get(),163);
  46. }
  47. /**
  48. * 将一个实现了Writable接口的对象序列化成字节流
  49. * @param writable
  50. * @return
  51. * @throws IOException
  52. */
  53. public static byte[] serialize(Writable writable) throws IOException {
  54. ByteArrayOutputStream out = new ByteArrayOutputStream();
  55. DataOutputStream dataOut = new DataOutputStream(out);
  56. writable.write(dataOut);
  57. dataOut.close();
  58. return out.toByteArray();
  59. }
  60. /**
  61. * 将字节流转化为实现了Writable接口的对象
  62. * @param writable
  63. * @param bytes
  64. * @return
  65. * @throws IOException
  66. */
  67. public static byte[] deserialize(Writable writable,byte[] bytes) throws IOException {
  68. ByteArrayInputStream in=new ByteArrayInputStream(bytes);
  69. DataInputStream dataIn = new DataInputStream(in);
  70. writable.readFields(dataIn);
  71. dataIn.close();
  72. return bytes;
  73. }
  74. }

WritableComparable和comparators

IntWritable实现了WritableComparable,接口看下源代码知道,WritableComparable是Writable接口和java.lang.Comparable<T>的一个子接口。
  1. package org.apache.hadoop.io;
  2. public interface WritableComparable <T>  extends org.apache.hadoop.io.Writable, java.lang.Comparable<T> {
  3. }

MapReduce在排序部分要根据key值的大小进行排序,因此类型的比较相当重要,RawComparator是Comparator的增强版

  1. package org.apache.hadoop.io;
  2. public interface RawComparator <T>  extends java.util.Comparator<T> {
  3. int compare(byte[] bytes, int i, int i1, byte[] bytes1, int i2, int i3);
  4. }

它可以做到,不先反序列化就可以直接比较二进制字节流的大小:

  1. package com.sweetop.styhadoop;
  2. import org.apache.hadoop.io.IntWritable;
  3. import org.apache.hadoop.io.RawComparator;
  4. import org.apache.hadoop.io.Writable;
  5. import org.apache.hadoop.io.WritableComparator;
  6. import org.eclipse.jdt.internal.core.Assert;
  7. import org.junit.Before;
  8. import org.junit.Test;
  9. import java.io.ByteArrayOutputStream;
  10. import java.io.DataOutputStream;
  11. import java.io.IOException;
  12. /**
  13. * Created with IntelliJ IDEA.
  14. * User: lastsweetop
  15. * Date: 13-7-5
  16. * Time: 上午1:26
  17. * To change this template use File | Settings | File Templates.
  18. */
  19. public class TestComparator {
  20. RawComparator<IntWritable> comparator;
  21. IntWritable w1;
  22. IntWritable w2;
  23. /**
  24. * 获得IntWritable的comparator,并初始化两个IntWritable
  25. */
  26. @Before
  27. public void init() {
  28. comparator = WritableComparator.get(IntWritable.class);
  29. w1 = new IntWritable(163);
  30. w2 = new IntWritable(76);
  31. }
  32. /**
  33. * 比较两个对象大小
  34. */
  35. @Test
  36. public void testComparator() {
  37. Assert.isTrue(comparator.compare(w1, w2) > 0);
  38. }
  39. /**
  40. * 序列号后进行直接比较
  41. * @throws IOException
  42. */
  43. @Test
  44. public void testcompare() throws IOException {
  45. byte[] b1 = serialize(w1);
  46. byte[] b2 = serialize(w2);
  47. Assert.isTrue(comparator.compare(b1, 0, b1.length, b2, 0, b2.length) > 0);
  48. }
  49. /**
  50. * 将一个实现了Writable接口的对象序列化成字节流
  51. *
  52. * @param writable
  53. * @return
  54. * @throws java.io.IOException
  55. */
  56. public static byte[] serialize(Writable writable) throws IOException {
  57. ByteArrayOutputStream out = new ByteArrayOutputStream();
  58. DataOutputStream dataOut = new DataOutputStream(out);
  59. writable.write(dataOut);
  60. dataOut.close();
  61. return out.toByteArray();
  62. }
  63. }

Writable、WritableComparable和comparators的更多相关文章

  1. hadoop中的序列化与Writable接口

    本文地址:http://www.cnblogs.com/archimedes/p/hadoop-writable-interface.html,转载请注明源地址. 简介 序列化和反序列化就是结构化对象 ...

  2. Hadoop开发相关问题

    总结自己在Hadoop开发中遇到的问题,主要在mapreduce代码执行方面.大部分来自日常代码执行错误的解决方法,还有一些是对Java.Hadoop剖析.对于问题,通过查询stackoverflow ...

  3. 分别使用Hadoop和Spark实现二次排序

    零.序(注意本部分与标题无太大关系,可直接调至第一部分) 既然没用为啥会有序?原因不想再开一篇文章,来抒发点什么感想或者计划了,就在这里写点好了: 前些日子买了几本书,打算学习和研究大数据方面的知识, ...

  4. 02Hadoop二次排序2

    案例: 数据: 邮编   |     日期     |金额 ILMN,2013-12-05,97.65GOOD,2013-12-09,1078.14IBM,2013-12-09,177.46ILMN, ...

  5. 01Hadoop二次排序

    我的目的: 示例: 2012,01,01,352011,12,23,-42012,01,01,432012,01,01,232011,12,23,52011,4,1,22011,4,1,56 结果: ...

  6. 解读:MultipleOutputs类

    //MultipleOutputs类用于简化多文件输出The MultipleOutputs class simplifies writing output data to multiple outp ...

  7. 详细讲解MapReduce二次排序过程

    我在15年处理大数据的时候还都是使用MapReduce, 随着时间的推移, 计算工具的发展, 内存越来越便宜, 计算方式也有了极大的改变. 到现在再做大数据开发的好多同学都是直接使用spark, hi ...

  8. 二次排序问题(分别使用Hadoop和Spark实现)

    不多说,直接上干货! 这篇博客里的算法部分的内容来自<数据算法:Hadoop/Spark大数据处理技巧>一书,不过书中的代码虽然思路正确,但是代码不完整,并且只有java部分的编程,我在它 ...

  9. 自定义Writable、RawComparatorWritable、comparators(转)

    自定义Writable hadoop虽然已经实现了一些非常有用的Writable,而且你可以使用他们的组合做很多事情,但是如果你想构造一些更加复杂的结果,你可以自定义Writable来达到你的目的,我 ...

随机推荐

  1. 获取设备唯一标识 uuid(采用第三方库SSKeychain)

    SSKeyChain 下载链接: http://pan.baidu.com/s/1booV3VD 密码: ivdi /** *  获取设备唯一标识 uuid */ +(NSString*) uuid ...

  2. iOS 隐藏Status Bar

    要隐藏,有3个地方要做: 1.在info.Plist里,将该属性的hidden,设置为YES,这样,在启动时,就不会显示了: 2.在application-didFinish里面写,这样,可以隐藏io ...

  3. JS-005-常见下拉列表 Select 和 datalist

    下拉列表在我们日常的网页浏览的过程中,随处可见,是 web 编程过程中大家非常熟悉的一个页面元素,随着 HTML 语言的日益强大,其在广大攻城狮的手中可谓是千变万化,有了很多不同的实现方式.本文主要以 ...

  4. java web filter 之一 基础实现

    本文主要对filter的基本使用进行了讲解,其中涉及到了 filter是什么 一个filter处理一个jsp 多个filter处理一个jsp filter是什么 Filter 是java下的一种过滤器 ...

  5. github上所有大于800 star OC框架

    https://github.com/XCGit/awesome-objc-frameworks#awesome-objc-frameworks awesome-objc-frameworks ID ...

  6. LeetCode Flip Game

    原题链接在这里:https://leetcode.com/problems/flip-game/ 题目: You are playing the following Flip Game with yo ...

  7. LeetCode Word Break II

    原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words  ...

  8. 股票中带有ST和*ST的股票是什么意思啊?一图了解新三板与主板、中小板、创业板制度差异!

    st表示已经亏损超过1年以上的 *st表示连续亏损3年以上,并且有退市风险的,随时可能会“退市”到时候你的钱可能都拿不会来 主板股票代码上海60开头,深圳000开头,通常指大中型企业:中小板002开头 ...

  9. Android Studio IDE 主题设置

    1.界面主题设置,如下图: 2.代码字体设置,如下图:

  10. C# Main函数的 args参数

    网上参考 博客,使用如下代码: using System; using System.Collections.Generic; using System.Linq; using System.Text ...