1

我的数据存储格式是:

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));
}    
4

1 回答 1

3

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

从测试中选择不同的 a、b

(分配ab成为列名。)

这是一个很好的例子,说明 CQL 使 Cassandra 开发变得多么简单,这就是为什么我们要推动人们使用原生 CQL 驱动程序而不是像 Hector 这样的遗留客户端。

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

于 2013-10-21T03:49:42.863 回答