Domanda

Assumere una Cassandra datastore con 20 righe, con chiavi di riga denominata "r1" .. "r20".

Domande:

  • Come faccio a recuperare le chiavi di riga dei primi dieci righe (r1 per r10)?

  • Come faccio a recuperare le chiavi di riga di dieci righe (r11 per r20)?

Sto cercando la Cassandra analogia:

SELECT row_key FROM table LIMIT 0, 10;
SELECT row_key FROM table LIMIT 10, 10;
È stato utile?

Soluzione

Date un'occhiata a:

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

Dove il tuo tupla KeyRange è (start_key, end_key) == (R1, R10)

Altri suggerimenti

Basato sui miei test non c'è nessun ordine per le righe (a differenza di colonne).CQL 3.0.0 possibile recuperare le chiavi di riga ma non distinte (ci dovrebbe essere un modo che non so).So che il mio caso non so cosa la mia chiave di gamma, quindi ho cercato di recuperare tutte le chiavi con entrambi Hector e Parsimonia, e ordinare le chiavi in seguito.Il performance test con CQL 3.0.0 per 100000 colonne 200 righe era di circa 500 millisecondi, Hector circa 100 e risparmio circa 50 millisecondi.La mia Riga di chiave qui è di tipo integer.Hector codice seguente:

public void qureyRowkeys(){
    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);
    }
}

Questo è il programma di codice:

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

    }
}

Si dovrebbe innanzitutto modificare cassandra.yaml nella versione di cassandra1.1.o, in cui è necessario impostare nel modo seguente:

partitioner: org.apache.cassandra.dht.ByteOrderedPartitioner

In secondo luogo, è necessario definire quanto segue:

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

Infine, è possibile inserire i dati in cassandra e può realizzare query di gamma.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top