正在获取Cassandra行键

vqlkdk9b  于 2023-02-15  发布在  Cassandra
关注(0)|答案(3)|浏览(172)

假设Cassandra数据存储有20行,行键名为"r1" .. "r20"
问题:

  • 如何获取前十行(r1r10)的行键?
  • 如何获取接下来10行(r11r20)的行键?

我在寻找Cassandra的比喻:

SELECT row_key FROM table LIMIT 0, 10;
SELECT row_key FROM table LIMIT 10, 10;
cyej8jka

cyej8jka1#

请看:

list<KeySlice> get_range_slices(keyspace, column_parent, predicate, range, consistency_level)

其中,KeyRange元组为(开始键,结束键)==(r1,r10)

abithluo

abithluo2#

根据我的测试,这些行没有顺序(与列不同)。CQL 3.0.0可以检索行键,但不是唯一的(应该有一个方法,我不知道)。我我的情况下我不知道我的关键范围是什么,所以我试图检索所有的关键与赫克托和节俭,并稍后对键进行排序。使用CQL 3.0.0对100000列200行进行的性能测试大约为500毫秒,Hector大约为100毫秒,而thrift大约为50毫秒。这里的My Row键是整数。Hector代码如下:

public void queryRowkeys() {
    myCluster = HFactory.getOrCreateCluster(CLUSTER_NAME, "127.0.0.1:9160");
    ConfigurableConsistencyLevel ccl = new ConfigurableConsistencyLevel();
    ccl.setDefaultReadConsistencyLevel(HConsistencyLevel.ONE);
    myKeyspace = HFactory.createKeyspace(KEYSPACE_NAME, myCluster, ccl);
    RangeSlicesQuery<Integer, Composite, String> rangeSlicesQuery = HFactory.createRangeSlicesQuery(myKeyspace, IntegerSerializer.get(), 
            CompositeSerializer.get(), StringSerializer.get());
    long start = System.currentTimeMillis();
    QueryResult<OrderedRows<Integer, Composite, String>> result =
      rangeSlicesQuery.setColumnFamily(CF).setKeys(0, -1).setReturnKeysOnly().execute();
    OrderedRows<Integer, Composite, String> orderedRows = result.get();
    ArrayList<Integer> list = new ArrayList<Integer>();
    for(Row<Integer, Composite, String> row: orderedRows){
        list.add(row.getKey());
    }
    
    System.out.println((System.currentTimeMillis()-start));
    Collections.sort(list);
    for(Integer i: list){
        System.out.println(i);
    }
}

这是节俭代码:

public void retreiveRows(){
    try {
        transport = new TFramedTransport(new TSocket("localhost", 9160));
        TProtocol protocol = new TBinaryProtocol(transport);
        client = new Cassandra.Client(protocol);
        transport.open();
        client.set_keyspace("prefdb");
        ColumnParent columnParent = new ColumnParent("events"); 
        SlicePredicate predicate = new SlicePredicate();
        predicate.setSlice_range(new SliceRange(ByteBuffer.wrap(new byte[0]), ByteBuffer.wrap(new byte[0]), false, 1));              
        KeyRange keyRange = new KeyRange();  //Get all keys
        keyRange.setStart_key(new byte[0]);
        keyRange.setEnd_key(new byte[0]);
        long start = System.currentTimeMillis();
        List<KeySlice> keySlices = client.get_range_slices(columnParent, predicate, keyRange, ConsistencyLevel.ONE);
        ArrayList<Integer> list = new ArrayList<Integer>();
        for (KeySlice ks : keySlices) {
                 list.add(ByteBuffer.wrap(ks.getKey()).getInt());
        }    
        Collections.sort(list);
        System.out.println((System.currentTimeMillis()-start));
        for(Integer i: list){
            System.out.println(i);
        }

        transport.close();
    } catch (Exception e) {
        e.printStackTrace();
    
    }
}
piok6c0g

piok6c0g3#

首先在cassandra1.1.o版本中修改cassandra.yaml,设置如下:

partitioner: org.apache.cassandra.dht.ByteOrderedPartitioner

其次,应定义如下:

create keyspace DEMO with placement_strategy =
  'org.apache.cassandra.locator.SimpleStrategy' and
  strategy_options = [{replication_factor:1}];

use DEMO;

create column family Users with comparator = AsciiType and
  key_validation_class = LongType and
  column_metadata = [
    {
      column_name: aaa,
      validation_class: BytesType
    },{
      column_name: bbb,
      validation_class: BytesType
    },{
      column_name: ccc,
      validation_class: BytesType
    }
  ];

最后,您可以将数据插入cassandra,并可以实现范围查询。

相关问题