我正在尝试实现一个数据模型,其中行键是字符串,列名称是长整型,列值是动态复合 Material 。使用 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 作为列值。因此我想问:我做错了什么?从异常(exception)情况来看,我似乎只是忘记在某个地方设置一些东西。
最佳答案
拉多万,
这可能是由于与 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 一起使用时,它对我来说效果很好。
关于cassandra - 如何反序列化动态复合列值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15322094/