Cassandra切片查询不使用复合键

时间:2014-09-23 11:44:29

标签: scala playframework playframework-2.0 cassandra hector

我是scala的新手,我想将我的java代码转换为scala。 我的数据库是cassandra,列族定义看起来像这样

Family name :colFam
Rowkey: rowKey1
=>(name=comkey1:comkey1,value='xyz',timestamp=1554515485)
=>(name=comkey1:comkey2,value='xyz',timestamp=1554515485)
=>(name=comkey1:comkey3,value='xyz',timestamp=1554515485)
=>(name=comkey1:comkey4,value='xyz',timestamp=1554515485)
=>(name=comkey1:comkey5,value='xyz',timestamp=1554515485)

-------------------------------------------------------
Rowkey: rowKey2
=>(name=comkey1:comkey3,value='abc',timestamp=1554515485)
-------------------------------------------------------
Rowkey: rowKey4
=>(name=comkey1:comkey4,value='pqr',timestamp=1554515485)
-------------------------------------------------------

现在我想从cassandra获取所有记录,我的columnQuery正常工作

val sliceQuery = HFactory.createColumnQuery(rankqKeyspace, StringSerializer.get(), new CompositeSerializer(), StringSerializer.get())
我的sliceQuery中的问题,通常切片查询工作正常,具有简单的列名,但当我使用列类型复合时,它给我错误

 var startKey = new  Composite();
var endKey = new Composite();
startKey.addComponent("comkey1", se);
startKey.addComponent("comkey2", se);
endKey.addComponent("comkey1", se);
endKey.addComponent("comkey4", se);
val sliceQuery = HFactory.createSliceQuery(rankqKeyspace, se, new CompositeSerializer(), se)
slicesQuery.setColumnFamily("colFam");
slicesQuery.setKey(rowKey1)
slicesQuery.setRange(startKey, endKey, false, Integer.MAX_VALUE);
val result = slicesQuery.execute()
val orderedRows = result.get();

它给了我错误" NullPointerException"

val orderedRows = result.get();
println(orderedRows)

此行返回始终为空值

修改问题

堆栈跟踪

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:708)
at me.prettyprint.cassandra.serializers.CompositeSerializer.fromByteBuffer(CompositeSerializer.java:29)
at me.prettyprint.cassandra.serializers.CompositeSerializer.fromByteBuffer(CompositeSerializer.java:17)
at me.prettyprint.cassandra.model.HColumnImpl.getName(HColumnImpl.java:111)
at me.prettyprint.cassandra.model.HColumnImpl.toString(HColumnImpl.java:202)
at java.lang.String.valueOf(String.java:2854)
at java.lang.StringBuilder.append(StringBuilder.java:128)
at java.util.AbstractCollection.toString(AbstractCollection.java:450)
at me.prettyprint.cassandra.model.ColumnSliceImpl.toString(ColumnSliceImpl.java:54)
at java.lang.String.valueOf(String.java:2854)
at java.io.PrintStream.println(PrintStream.java:821)
at scala.Console$.println(Console.scala:240)
at scala.Predef$.println(Predef.scala:287)
at models.PinModel$.testColCompositeKey(PinModel.scala:178)
at controllers.PinController$$anonfun$test$1.apply(PinController.scala:52)
at controllers.PinController$$anonfun$test$1.apply(PinController.scala:49)
at play.api.mvc.ActionBuilder$$anonfun$apply$10.apply(Action.scala:221)
at play.api.mvc.ActionBuilder$$anonfun$apply$10.apply(Action.scala:220)
at play.api.mvc.Action$.invokeBlock(Action.scala:357)
at play.api.mvc.ActionBuilder$$anon$1.apply(Action.scala:309)
at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4$$anonfun$apply$5.apply(Action.scala:109)
at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4$$anonfun$apply$5.apply(Action.scala:109)
at play.utils.Threads$.withContextClassLoader(Threads.scala:18)
at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4.apply(Action.scala:108)
at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4.apply(Action.scala:107)
at scala.Option.map(Option.scala:145)
at play.api.mvc.Action$$anonfun$apply$1.apply(Action.scala:107)
at play.api.mvc.Action$$anonfun$apply$1.apply(Action.scala:100)
at play.api.libs.iteratee.Iteratee$$anonfun$mapM$1.apply(Iteratee.scala:481)
at play.api.libs.iteratee.Iteratee$$anonfun$mapM$1.apply(Iteratee.scala:481)
at play.api.libs.iteratee.Iteratee$$anonfun$flatMapM$1.apply(Iteratee.scala:517)
at play.api.libs.iteratee.Iteratee$$anonfun$flatMapM$1.apply(Iteratee.scala:517)
at play.api.libs.iteratee.Iteratee$$anonfun$flatMap$1$$anonfun$apply$13.apply(Iteratee.scala:493)
at play.api.libs.iteratee.Iteratee$$anonfun$flatMap$1$$anonfun$apply$13.apply(Iteratee.scala:493)
at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24)
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24)
at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:42)
at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:386)
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)

2 个答案:

答案 0 :(得分:2)

在这里遇到同样问题的人,Hector与最新版本的Google Guava不能很好地导致此错误,请检查是否有任何库,具体取决于不同版本的Guava。对于我的确切情况,我最终使用了Guava 13.0,因为它还包含了其他库所需的部分。

如果可能的话,我建议远离赫克托,不再维护它。我个人已经开始使用CQL迁移到新的Datastax驱动程序,它使很多事情变得更容易。

答案 1 :(得分:0)

我猜您需要为每个复合键明确设置序列化器的位置:

var startKey = new  Composite();
startKey.setSerializerByPosition(0, comkey1Serializer)
startKey.setSerializerByPosition(1, comkey2Serializer)
startKey.addComponent("comkey1", se);
startKey.addComponent("comkey2", se);