0
votes

I have a simple column family 'Users'

    System.out.println("  read User");
    long timestamp = System.currentTimeMillis();
    clientA.insert(ByteBuffer.wrap("mike".getBytes()), new ColumnParent("Users"), 
            new Column(ByteBuffer.wrap("email".getBytes())).setValue(ByteBuffer.wrap("[email protected]".getBytes())).setTimestamp(timestamp)
            , ConsistencyLevel.ONE);

    clientA.insert(ByteBuffer.wrap("mike".getBytes()), new ColumnParent("Users"), 
            new Column(ByteBuffer.wrap("totalPosts".getBytes())).setValue(ByteBuffer.allocate(4).putInt(27).array()).setTimestamp(timestamp)
            , ConsistencyLevel.ONE);

    SlicePredicate predicate = new SlicePredicate();
    predicate.setSlice_range(new SliceRange(ByteBuffer.wrap(new byte[0]), ByteBuffer.wrap(new byte[0]), false, 10));

    ColumnParent parent = new ColumnParent("Users");

    List<ColumnOrSuperColumn> results = clientA.get_slice(ByteBuffer.wrap("mike".getBytes()), 
            parent, 
            predicate, 
            ConsistencyLevel.ONE);

    for (ColumnOrSuperColumn result : results) {
        Column column = result.column;
        System.out.println(new String(column.getName()) + " -> "+ new String(column.getValue())+", "+column.getValue().length);
    }

it returns

  read User
email -> [email protected], 14
totalPosts -> 

So totalPosts can't be read with the thrift client

with cassandra-cli

[default@test] get Users['mike']['totalPosts']; => (column=totalPosts, value=0000001b, timestamp=1336493080621) Elapsed time: 10 msec(s).

How can I retrieve this Integer value with Java thrift client?

using cassandra 1.1

edit: it seems due to this part

for (ColumnOrSuperColumn result : results) {
        Column column = result.column;
        System.out.println(new String(column.getName()) + " -> "+Arrays.toString(column.getValue()));
    }

returns

email -> [109, 105, 107, 101, 64, 103, 109, 97, 105, 108, 46, 99, 111, 109]
totalPosts -> [0, 0, 0, 27]
1

1 Answers

0
votes

Your column values are being returned as bytes, the same way you're inserting them. So the value for the 'email' column is [109, 105, 107, 101, 64, 103, 109, 97, 105, 108, 46, 99, 111, 109], which interpreted as ascii is [email protected]. And the number 27 which you write into the ByteBuffer as [0, 0, 0, 27] (via the putint call) comes out the same way.

If you absolutely have to be using the raw thrift interface, you'll probably want to retrieve your totalPosts int using ByteBuffer.getInt(). But if at all possible, I recommend using a library to wrap around the ugliness of the thrift interface, which should take care of value serialization issues like this. Maybe look at Hector, or skip the old interface entirely and go straight to CQL with Cassandra-JDBC.