如何反序列化DynamicComposite列值?

时间:2013-03-10 12:14:29

标签: cassandra hector

我正在尝试实现一个数据模型,其中行键是字符串,列名称是Longs,列值是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作为列值。所以我想问:我做错了什么?从例外情况来看,我似乎忘了在某个地方设置一些东西。

1 个答案:

答案 0 :(得分:2)

拉​​多,

这可能是由于与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一起使用时,它对我来说很好。