运维开发网

基于Hadoop的数据序列化与反序列化实例

运维开发网 https://www.qedev.com 2020-04-01 18:13 出处:网络
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import org.apache.hadoop.io.Int
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Writable;

public class SerializeOrDeserialize {
	
	private static IntWritable intWritable = new IntWritable(163);
	private static IntWritable intWritable2 = new IntWritable();
	
	public static void main(String[] args) throws IOException {
		
		byte[] bytes1 = serialize(intWritable); 
		//输出结果为[B@277c0f21
		System.out.println(bytes1);
		
		deserialize(intWritable2, bytes1);
		//输出结果为163
		System.out.println(intWritable2.get());
	}
	/**
	 * 序列化
	 * @param writable
	 * @return
	 * @throws IOException
	 */
	public static byte[] serialize(Writable writable) throws IOException{	
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
		//将对象转换为字节流并写入到输出流dataOutputStream中
		writable.write(dataOutputStream);
		dataOutputStream.close();
		return byteArrayOutputStream.toByteArray();
	}
	/**
	 * 反序列化
	 * @param writable
	 * @param bytes
	 * @return
	 * @throws IOException
	 */
	public static byte[] deserialize(Writable writable, byte[] bytes) throws IOException{
		ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
		DataInputStream dataInputStream = new DataInputStream(byteArrayInputStream);
		//从输入流dataInputStream中读取字节流并反序列化为对象
		writable.readFields(dataInputStream);
		dataInputStream.close();
		return bytes;
	}
}
0

精彩评论

暂无评论...
验证码 换一张
取 消