Zookeeper

今天在读取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());

 

转载于:https://my.oschina.net/u/118686/blog/1935238

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值