Search code examples
cassandrahector

confusing with Queryslice.setrange


i am new to cassandra and hector. now i am trying to retrieve some data which i stored in cassandra. there are lot of columns, some of which has a prefix.

column1 column2 column3 prefix1_prefix2_column3 prefix1_prefix2_column4 ....and so on.

now i want to get all the columns with prefix1_prefix2_

however, i got more than i wanted, some other columns are also returend.

the CF comparator is bytestype, i also tried utf8 type, it doesn't work.

following is my code,

SliceQuery<UUID, String, ByteBuffer> query = HFactory.createSliceQuery(
                keyspace, UUIDSerializer.get(), stringSerializer,
                ByteBufferSerializer.get());
        String columnPrifx = "prefix1_prefix2";
        query.setKey(keyuuid).setColumnFamily("UserLogin");

    query.setRange(columnPrifx, columnPrifx, false, Integer.MAX_VALUE);
    //query.setRange(columnPrifx, null, false, Integer.MAX_VALUE);
            //i also tried null above
    ColumnSliceIterator<UUID, String, ByteBuffer> iterator = new ColumnSliceIterator<UUID, String, ByteBuffer>(
            query, null, "\uFFFF", false);


    while (iterator.hasNext()) {

        HColumn<String, ByteBuffer> c = iterator.next();
        System.out.println(c.getName());


    }

so, that all, i got more columns than i expected... any one could help me ? thank you very much


Solution

  • Dont concat string while generating key ,Try to split your column name to composite key like "prefix1_prefix2_" and "column3" . Now if you fetch data as shown below you will get your result

    Composite startRange = new Composite();
    startRange.addComponent(0, "prefix1_prefix2_",Composite.ComponentEquality.EQUAL);
    
    Composite endRange = new Composite();
    endRange.addComponent(0, "prefix1_prefix2_",Composite.ComponentEquality.GREATER_THAN_EQUAL);
    
    query.setRange(startRange, endRange, false, Integer.MAX_VALUE);