今天在读取Zookeeper数据时报错:
Exception in thread "main" org.I0Itec.zkclient.exception.ZkMarshallingError: java.io.StreamCorruptedException: invalid stream header: 74657374
at org.I0Itec.zkclient.serialize.SerializableSerializer.deserialize(SerializableSerializer.java:37)
at org.I0Itec.zkclient.ZkClient.derializable(ZkClient.java:740)
at org.I0Itec.zkclient.ZkClient.readData(ZkClient.java:773)
at org.I0Itec.zkclient.ZkClient.readData(ZkClient.java:761)
at org.I0Itec.zkclient.ZkClient.readData(ZkClient.java:750)
at org.I0Itec.zkclient.ZkClient.readData(ZkClient.java:744)
at HelloZookeeper2.main(HelloZookeeper2.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.io.StreamCorruptedException: invalid stream header: 74657374
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:806)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
at org.I0Itec.zkclient.serialize.SerializableSerializer.deserialize(SerializableSerializer.java:31)
... 11 more
主要是数据序列化的问题,可以自己实现一个ZkSerializer接口
import org.I0Itec.zkclient.serialize.ZkSerializer;
public class MyZkSerializer implements ZkSerializer {
public Object deserialize(byte[] bytes) throws ZkMarshallingError {
return new String(bytes, Charset.forName("UTF-8"));
}
public byte[] serialize(Object obj) throws ZkMarshallingError {
return String.valueOf(obj).getBytes(Charset.forName("UTF-8"));
}
}
然后,使用自定义的ZkSerializer
zkClient.setZkSerializer(new MyZkSerializer());