摘要
本来先讲数据存储的,但是绕不开序列化,因为在网络通信、数据存储中都用到了序列化。 这一节就讲zk的序列化
简介
Zookeeper从最早的版本开始就一直使用Jute作为序列化工具,直到现在最新的版本zookeeper-3.4.9依然使用Jute;至于为什么没有换成性能更好,通用性更强的如:Apache Avro,Thrift,Protobuf等序列化组件,主要还是由于考虑到新老版本序列化组件的兼容性,另一方面Jute并没有成为Zookeeper的瓶颈所在;
序列化主要在zookeeper.jute包中, 主要是
InputArchive:反序列化器要实现的接口
OutputArchive:序列化器需要实现的接口
Index:反序列化器的迭代器
Record:用于网络传输或者本地存储的类型都实现该接口
接口介绍
InputArchive
public interface InputArchive {
public byte readByte(String tag) throws IOException;
public boolean readBool(String tag) throws IOException;
public int readInt(String tag) throws IOException;
public long readLong(String tag) throws IOException;
public float readFloat(String tag) throws IOException;
public double readDouble(String tag) throws IOException;
public String readString(String tag) throws IOException;
public byte[] readBuffer(String tag) throws IOException;
//读取特定tag的record
public void readRecord(Record r, String tag) throws IOException;
//读取特定tag的record之前干的事情
public void startRecord(String tag) throws IOException;
//读取特定tag的record之后干的事情
public void endRecord(String tag) throws IOException;
//开始读取向量
public Index startVector(String tag) throws IOException;
//结束读取向量
public void endVector(String tag) throws IOException;
//开始读取map
public Index startMap(String tag) throws IOException;
//结束读取map
public void endMap(String tag) throws IOException;
}
这里除了基础类型,容器以外就是record,下面会介绍这种类型以及Index
OutputArchive
public interface OutputArchive {
public void writeByte(byte b, String tag) throws IOException;
public void writeBool(boolean b, String tag) throws IOException;
public void writeInt(int i, String tag) throws IOException;
public void writeLong(long l, String tag) throws IOException;
public void writeFloat(float f, String tag) throws IOException;
public void writeDouble(double d, String tag) throws IOException;
public void writeString(String s, String tag) throws IOException;
public void writeBuffer(byte buf[], String tag)
throws IOException;
public void writeRecord(Record r, String tag) throws IOException;
public void startRecord(Record r, String tag) throws IOException;
public void endRecord(Record r, String tag) throws IOException;
public void startVector(List v, String tag) throws IOException;
public void endVector(List v, String tag) throws IOException;
public void startMap(TreeMap v, String tag) throws IOException;
public void endMap(TreeMap v, String tag) throws IOException;
}
这里就不加注释了,和InputArchive 相反就是了
index
public interface Index {
// 是否已经完成
public boolean done();
// 下一项
public void incr();
}
Record
public interface Record {
// 序列化
public void serialize(OutputArchive archive, String tag) throws IOException;
// 反序列化
public void deserialize(InputArchive archive, String tag) throws IOException;
}
这个实现类就很多了
思考
对demo感兴趣的,看下面的refer就好了 jute这个序列化组件,目前就看到在zk上面用,倒是让我对protobuf这个序列化工具产生兴趣,后续专门讲序列化的时候,再研究好了
refer
http://www.cnblogs.com/leesf456/p/6278853.html jute序列化与protoBuf性能对比 http://blog.csdn.net/quhongwei_zhanqiu?viewmode=contents