Cassandra-Hector-Scala:如何在列族中获得所有行复合键?

时间:2013-10-20 12:50:05

标签: scala playframework cassandra command-line-interface hector

我的数据存储格式为:

Family name :Test
Rowkey: comkey1:comkey2
=>(name=name,value='xyz',timestamp=1554515485)
-------------------------------------------------------
Rowkey: comkey1:comkey3
=>(name=name,value='abc',timestamp=1554515485)
-------------------------------------------------------
Rowkey: comkey1:comkey4
=>(name=name,value='pqr',timestamp=1554515485)
-------------------------------------------------------

现在我想从“test”系列中获取所有复合键 我正在尝试

def test=Action{

val cluster = HFactory.getOrCreateCluster("Test Cluster", "127.0.0.1:9160");
val keyspace = HFactory.createKeyspace("winoriatest", cluster)
var startKey = new Composite();
var endKey= new Composite();
startKey.addComponent("comkey1", StringSerializer.get());
startKey.addComponent("comkey2", StringSerializer.get());
endKey.addComponent("comkey1", StringSerializer.get());
endKey.addComponent("comkey4", StringSerializer.get());

val rangeSlicesQuery = HFactory.createRangeSlicesQuery(keyspace, CompositeSerializer.get(), StringSerializer.get(),StringSerializer.get())
rangeSlicesQuery.setColumnFamily("test");
// CompositeSerializer.get() is not working.
rangeSlicesQuery.setKeys(startKey,endKey)
rangeSlicesQuery.setRange(null,null,false,Integer.MAX_VALUE);
rangeSlicesQuery.setReturnKeysOnly()
val result = rangeSlicesQuery.execute()
val orderedRows = result.get();
import scala.collection.JavaConversions._
for (sc <- orderedRows) {
  println(sc.getKey())
  }
Ok(views.html.index("Your new application is ready."))
}

错误:行上的[NullPointerException:null]

val result = rangeSlicesQuery.execute() 

Cassandra 2.0 scala 2.10.2 感谢您提前解决此问题的帮助。

它给我空指针异常,并且相同的代码正在使用java
我的java代码是

Cluster cluster = HFactory.getOrCreateCluster("Test Cluster","127.0.0.1:9160");
Keyspace keyspace = HFactory.createKeyspace("winoriatest", cluster);
Serializer<String> se= StringSerializer.get() ;
Serializer<Long> le= LongSerializer.get() ;
Serializer<Integer> ie= IntegerSerializer.get() ;
CompositeSerializer ce = new CompositeSerializer();
RangeSlicesQuery<Composite,String,byte[]>     rangeSliceQuery=HFactory.createRangeSlicesQuery(keyspace,ce,se, BytesArraySerializer.get());
rangeSliceQuery.setColumnFamily("test");
rangeSliceQuery.setRange(null,null, false, Integer.MAX_VALUE);
QueryResult<OrderedRows<Composite,String,byte[]>>result=rangeSliceQuery.execute();
OrderedRows<Composite,String,byte[]> orderedRows=result.get();
for (Row<Composite,String,byte[]> r:orderedRows)
    {
    System.out.println("Compositekey="+r.getKey().get(0,se)+":"+r.getKey().get(1, se));
}    

1 个答案:

答案 0 :(得分:3)

我不太确定“我想在测试系列中获取所有复合键”是什么意思。如果您的意思是,您只想获得分区[row]关键组件,那么您可以在CQL中执行此操作,如下所示:

  

SELECT DISTINCT a,b FROM test

(将ab指定为列名。)

这是一个很好的例子,说明CQL对Cassandra的开发有多么简单,这就是为什么我们要求人们使用native CQL driver而不是像Hector这样的传统客户端。

有关CQL如何理解这样的Thrift数据模型的更多信息,请参阅http://www.datastax.com/dev/blog/cql3-for-cassandra-experts