knorv
knorv

Reputation: 50127

Fetching Cassandra row keys

Assume a Cassandra datastore with 20 rows, with row keys named "r1" .. "r20".

Questions:

I'm looking for the Cassandra analogy to:

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

Upvotes: 8

Views: 2452

Answers (3)

Shahryar Sedghi
Shahryar Sedghi

Reputation: 19

Based on my tests there is no order for the rows (unlike columns). CQL 3.0.0 can retrieve row keys but not distinct (there should be a way that I do not know).I my case I do not know what my key range is, so I tried to retrieve all the keys with both Hector and Thrift, and sort the keys later. The performance test with CQL 3.0.0 for 100000 columns 200 rows was about 500 milliseconds, Hector around 100 and thrift about 50 milliseconds. My Row key here is integer. Hector code follows:

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

This is the Thrift code:

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

Upvotes: 1

LuSpine
LuSpine

Reputation: 11

You should firstly modify cassandra.yaml in the version of cassandra1.1.o, where you should set as follows:

partitioner: org.apache.cassandra.dht.ByteOrderedPartitioner

Secondly,you should define as follows:

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
    }
  ];

Finally, you can insert data into cassandra and can realize range query.

Upvotes: 0

Schildmeijer
Schildmeijer

Reputation: 20946

Take a look at:

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

Where your KeyRange tuple is (start_key, end_key) == (r1, r10)

Upvotes: 8

Related Questions