【问题标题】:How to deserialize DynamicComposite column value?如何反序列化动态复合列值?
【发布时间】:2013-02-25 15:44:47
【问题描述】:

我正在尝试实现一个数据模型,其中行键是字符串,列名是 Long,列值是 DynamicComposites。使用 Hector,存储过程的示例如下所示:

// create the value
DynamicComposite colVal = new DynamicComposite(); 
colVal.add(0, "someString");
colVal.setComparatorByPosition(0, "org.apache.cassandra.db.marshal.UTF8Type");
colVal.setSerializerByPosition(0, StringSerializer.get());

// create the column
HColumnImpl<Long, DynamicComposite> newCol = new
    HColumnImpl<Long, DynamicComposite>(longSerializer, 
        dynamicCompositeSerializer);

newCol.setName(longValue);
newCol.setValue(colVal);
newCol.setClock(keySpace.createClock());

// insert the new column
Mutator<String> mutator = HFactory.createMutator(keySpace,stringSerializer);
mutator.addInsertion("rowKey","columnFamilyName",newCol);
mutator.execute();

现在,当我尝试检索数据时:

// create the query
SliceQuery<String,Long,DynamicComposite> sq =
    HFactory.createSliceQuery(keySpace, stringSerializer, longSerializer, 
        dynamicCompositeSerializer);

// set the query
sq.setColumnFamily("columnFamilyName");
sq.setKey("rowKey");
sq.setColumnNames(longValue);

// execute the query
QueryResult<ColumnSlice<Long, DynamicComposite>> qr = sq.execute();

// get the data
qr.get().getColumnByName(longValue).getValue();

或者当我试图简单地告别时:

// get the data    
dynamicSerializer.fromByteBuffer(qr.get().
    getColumnByName(longValue).getValueBytes());

我遇到了一个异常:

Exception in thread "main" java.lang.NullPointerException
    at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:191)
    at com.google.common.collect.ImmutableClassToInstanceMap.getInstance(ImmutableClassToInstanceMap.java:147)
    at me.prettyprint.hector.api.beans.AbstractComposite.serializerForComparator(AbstractComposite.java:321)
    at me.prettyprint.hector.api.beans.AbstractComposite.getSerializer(AbstractComposite.java:344)
    at me.prettyprint.hector.api.beans.AbstractComposite.deserialize(AbstractComposite.java:713)
    at me.prettyprint.hector.api.beans.DynamicComposite.fromByteBuffer(DynamicComposite.java:25)
    at me.prettyprint.cassandra.serializers.DynamicCompositeSerializer.fromByteBuffer(DynamicCompositeSerializer.java:35)

据我从阅读的所有教程中了解到,应该可以使用 DynamicComposite 作为列值。因此我想问:我做错了什么?从异常看来我只是忘记在某处设置一些东西。

【问题讨论】:

    标签: cassandra hector


    【解决方案1】:

    拉多万,

    这可能是由于与 Hector 版本结合使用的 Guava 库的兼容性问题。

    另请参阅:https://github.com/hector-client/hector/pull/591

    我在 Hector-core-1.1-1.jar 上,结合 Guava-14.0.jar 我得到了和你一样的错误。当我将它与 Guava-12.0.1.jar 一起使用时,它对我来说效果很好。

    【讨论】:

    • 感谢您的回复。我会检查一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 1970-01-01
    • 2015-11-22
    • 2021-04-18
    相关资源
    最近更新 更多