Hadoop学习--int类型的序列化和反序列化--day07

import java.io.ByteArrayInputStream;

专注于为中小企业提供成都做网站、网站设计服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业延边朝鲜族免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了近1000家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

import java.io.ByteArrayOutputStream;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import org.apache.hadoop.io.IntWritable;

import org.apache.hadoop.io.ShortWritable;

import org.junit.Test;

public class TestWritable {

/*

* test writable

*/

@Test

public void test1() throws Exception {

IntWritable age = new IntWritable();

age.set(163);

ByteArrayOutputStream baos = new ByteArrayOutputStream();

DataOutputStream dataOut = new DataOutputStream(baos);

// 序列化

age.write(dataOut);

dataOut.close();

byte[] bts = baos.toByteArray();

for (byte by : bts) {

System.out.println(by);

}

System.out.println(bts.toString());

// 反序列化

IntWritable age2 = new IntWritable();

age2.readFields(new DataInputStream(new ByteArrayInputStream(baos

.toByteArray())));

System.out.println(age2.get());

ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());

DataInputStream dis = new DataInputStream(bais);

System.out.println(dis.readInt());

// //测试用short类型方式的读取

// ShortWritable short1 = new ShortWritable();

// short1.readFields(dis);

// System.out.println(short1.get());

// ShortWritable short2 = new ShortWritable();

// short2.readFields(dis);

// System.out.println(short2.get());

}

}


网页名称:Hadoop学习--int类型的序列化和反序列化--day07
网页网址:http://ybzwz.com/article/gghgsd.html