测试序列化后的长度

提示:需要用到的类,以及继承关系如下:

1.java.lang.Object
  |__ java.io.OutputStream
           |__ java.io.ByteArrayOutputStream
                  //description
                     public class ByteArrayOutputStream
                     extends OutputStream
                   //method
                     byte[] toByteArray()   //Creates a newly allocated byte array.
2.java.lang.Object
      |__ java.io.OutputStream
               |__ java.io.FilterOutputStream
                        |__ java.io.DataOutputStream
                             //description
                                public class DataOutputStream
                                extends FilterOutputStream
                                implements DataOutput
3. java.lang.Object
      |__ java.io.OutputStream
           // description
               public abstract class OutputStream
               extends Object
                implements Closeable, Flushable
            //mdthods
               void close()
               void flush()
               void write(byte[] b)
               void write(byte[] b, int off, int len)
               abstract void write(int b)
4.org.apache.hadoop.io
Interface Writable
 //description
    public interface Writable
 //methods
   void readFields(DataInput in)
   void write(DataOutput out)
5. java.lang.Object
       |__ org.apache.hadoop.io.IntWritable
              //description
                 public class IntWritableextends Object
                 implements   WritableComparable
               // methods
                   int get()
                   void readFields(DataInput in)
                   void set(int value)
                   void write(DataOutput out)
6. java.lang.Object
      |__ org.apache.hadoop.util.StringUtils
            //description
              public class StringUtils
              extends Object
            //method
               static String byteToHexString(byte[] bytes)
               static String capitalize(String s)
               static String[] split(String str)
               static Path[] stringToPath(String[] str)
               static URI[] stringToURI(String[] str)
               static String uriToString(URI[] uris) 

代码

import java.io.DataOutputStream;
import java.io.ByteArrayOutputStream;
import java.lang.Byte;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.StringUtils;
public class TestWritable extends IntWritable {

  public static byte[] serialize(IntWritable writable) throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    DataOutputStream dataOut = new DataOutputStream(out);
    writable.write(dataOut);
    dataOut.close();
    return out.toByteArray();
  }

  public static void main(String[] args) throws Exception {
    IntWritable i = new IntWritable();
    i.set(8);
    System.out.println(i.get());
    byte[] bytes = serialize(i);
    System.out.println(bytes.toString().length());
    System.out.println(StringUtils.byteToHexString(bytes));
  }
}

hadoop2.2编程:序列化的更多相关文章

  1. hadoop2.2编程: 重写comparactor

    要点: 类型比较在hadoop的mapreduce中非常重要,主要用来比较keys; hadoop中的RawComparator<T>接口继承自java的comparator, 主要用来比 ...

  2. hadoop2.2编程:使用MapReduce编程实例(转)

    原文链接:http://www.cnblogs.com/xia520pi/archive/2012/06/04/2534533.html 从网上搜到的一篇hadoop的编程实例,对于初学者真是帮助太大 ...

  3. hadoop2.2编程:各种API

    hadoop2.2 API http://hadoop.apache.org/docs/r0.23.9/api/index.html junit API http://junit.org/javado ...

  4. hadoop2.2编程:DFS API 操作

    1. Reading data from a hadoop URL 说明:想要让java从hadoop的dfs里读取数据,则java 必须能够识别hadoop hdfs URL schema, 因此我 ...

  5. hadoop2.2编程:mapreduce编程之二次排序

    mr自带的例子中的源码SecondarySort,我重新写了一下,基本没变. 这个例子中定义的map和reduce如下,关键是它对输入输出类型的定义:(java泛型编程) public static ...

  6. c#基础语言编程-序列化

    引言 程序员在编写应用程序的时候往往要将程序的某些数据存储在内存中,然后将其写入某个文件或是将它传输到网络中的另一台计算机上以实现通讯.这个将程序数据转化成能被存储并传输的格式的过程被称为" ...

  7. ASP.NET Web API编程——序列化与内容协商

    1 多媒体格式化器 多媒体类型又叫MIME类型,指示了数据的格式.在HTTP协议中多媒体类型描述了消息体的格式.一个多媒体类型包括两个字符串:类型和子类型. 例如: text/html.image/p ...

  8. Python高级编程-序列化

    在程序运行的过程中,所有的变量都是在内存中,比如,定义一个dict: dict1 = {'name': 'Rob', 'age': 19, 'score': 90} 可以随时修改变量,比如把age改成 ...

  9. python IO编程-序列化

    原文链接:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143192607 ...

随机推荐

  1. Oracle之Merge用法

    Merge用来从一个表中选择一些数据更新或者插入到另一个表中.而最终是用更新还是用插入的方式取决于该语句中的条件. 下面我们简单的举一个例子: SQL)) 表已创建. SQL)) 表已创建. SQL, ...

  2. PHP session 失效不传递的解决办法

    PHP中,session不能传递到下一个页面去,一般有两种情况: 我们先写个php文件:<?=phpinfo()?>, 传到服务器去看看服务器的参数配置. 转到session部分,看到se ...

  3. Unix环境高级编程学习笔记——dup

    dup 和 dup2   dup和dup2,都是用来将一个文件描述符复制给另一个文件描述符上,这两个文件描述符都指向同一个文件状态标志上. 只是文件描述符的大小不一样,dup所执行下的复制,肯定是返回 ...

  4. C# 制作卸载文件

    1.建一个控制台应用程序Uninstall: 2.在应用程序的mian方法中添加 static void Main(string[] args) { System.Diagnostics.Proces ...

  5. atexit()函数

    atexit()函数  头文件:#include<stdlib.h> 功 能: 注册终止函数(即main执行结束后调用的函数) 用 法: int atexit(void (*func)(v ...

  6. tpl demo

    using System; using System.Collections.Concurrent; using System.Threading; using System.Threading.Ta ...

  7. uboot顶层config.mk分析

    uboot顶层目录中的config.mk定义了确定了当前执行makefile所对应的源文件目录.目标文件目录,编译的程序编译.连接的选项,以及目标文件生成的规则等等.它被包含在顶层的makefile以 ...

  8. .net source code

    .NET 类库的强大让我们很轻松的解决常见问题,作为一个好专研的程序员,为了更上一层楼,研究CLR的基础类库实现是快速稳定的捷径. 一般场景下,采用 Reflector可以反射出.NET 的部分实现出 ...

  9. uva 812 Trade on Verweggistan

    题意: 给w个货架, 每个货架上有bi个货物, 每次只能拿最上面的货物, 每个货物有个价值, 所有货物的售价均为10. 问:能获得的最大利润, 以及能获得这个利润需要多少个货物. (有多种组合时只需输 ...

  10. 深入探究JavaScript中的比较问题

    先用最近遇到的几个问题做引子: 1 console.log(null==undefined); //true 2 console.log(null==false);//false 3 console. ...