1. 程式人生 > >hadoop深入研究:(十)——序列化與Writable介面

hadoop深入研究:(十)——序列化與Writable介面

package com.sweetop.styhadoop;

import junit.framework.Assert;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.util.StringUtils;
import org.junit.Before;
import org.junit.Test;

import java.io.*;

/**
 * Created with IntelliJ IDEA.
 * User: lastsweetop
 * Date: 13-7-4
 * Time: 下午10:25
 * To change this template use File | Settings | File Templates.
 */
public class TestWritable {
    byte[] bytes=null;

    /**
     * 初始化一個IntWritable例項,並且呼叫系列化方法
     * @throws IOException
     */
    @Before
    public void init() throws IOException {
        IntWritable writable = new IntWritable(163);
        bytes = serialize(writable);
    }

    /**
     * 一個IntWritable序列號後的四個位元組的位元組流
     * 並且使用big-endian的佇列排列
     * @throws IOException
     */
    @Test
    public void testSerialize() throws IOException {
        Assert.assertEquals(bytes.length,4);
        Assert.assertEquals(StringUtils.byteToHexString(bytes),"000000a3");
    }

    /**
     * 建立一個沒有值的IntWritable物件,並且通過呼叫反序列化方法將bytes的資料讀入到它裡面
     * 通過呼叫它的get方法,獲得原始的值,163
     */
    @Test
    public void testDeserialize() throws IOException {
        IntWritable newWritable = new IntWritable();
        deserialize(newWritable,bytes);
        Assert.assertEquals(newWritable.get(),163);
    }

    /**
     * 將一個實現了Writable介面的物件序列化成位元組流
     * @param writable
     * @return
     * @throws 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();
    }

    /**
     * 將位元組流轉化為實現了Writable介面的物件
     * @param writable
     * @param bytes
     * @return
     * @throws IOException
     */
    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;
    }
}