Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Hadoop learning-serialization and deserialization of int types-day07

Shulou Source: shulou.com Published: 2022-06-03 06:17:47 09月22日 Update

import java.io.ByteArrayInputStream;

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);

//serialize

age.write(dataOut);

dataOut.close();

byte[] bts = baos.toByteArray();

for (byte by : bts) {

System.out.println(by);

}

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

//deserialize

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());

// //test reading in short type mode

// ShortWritable short1 = new ShortWritable();

// short1.readFields(dis);

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

// ShortWritable short2 = new ShortWritable();

// short2.readFields(dis);

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

}

}

Tags: Sequence type mode testing learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Information Linux Apple OPPO Reno