Search code examples
nosqlcassandrathrift

Cassandra Thrift adding dynamic row with DateType Comparator


I have the following column family:

CREATE COLUMN FAMILY messages with comparator=DateType and key_validation_class=UTF8Type and default_validation_class=UTF8Type;

now I'm using Cassandra Thrift to store new Data:

 TTransport tr = this.dataSource.getConnection();
         TProtocol proto = new TBinaryProtocol(tr);      
         Cassandra.Client client = new Cassandra.Client(proto);
         long timestamp = System.currentTimeMillis();
            client.set_keyspace("myTestSPace");
             ColumnParent parent = new ColumnParent("messages");
             Column messageColumn = new Column();
             String time = String.valueOf(timestamp);        
             messageColumn.setName(time.getBytes());
             messageColumn.setValue(toByteBuffer(msg.getText));
             messageColumn.setTimestamp(timestamp);
             client.insert(toByteBuffer(msg.getAuthor()), parent, messageColumn, ConsistencyLevel.ONE);

but I'm getting exception:

InvalidRequestException(why:Expected 8 or 0 byte long for date (16))
    at org.apache.cassandra.thrift.Cassandra$insert_result.read(Cassandra.java:15198)
    at org.apache.cassandra.thrift.Cassandra$Client.recv_insert(Cassandra.java:858)
    at org.apache.cassandra.thrift.Cassandra$Client.insert(Cassandra.java:830)
    at com.vanilla.cassandra.DaoService.addMessage(DaoService.java:57)

How to do it correct?


Solution

  • It appears that you're using the raw Thrift interface. For reasons like the one you've encountered and many, many more, I strongly suggest that you use an existing high-level client like Hector, Astyanax, or a CQL client.

    The root cause of your issue is that you have to pack different datatypes into a binary format. The higher level clients manage this automatically.