Cassandra - Insert Column into ColumnFamily


/ Published in: Java
Save to your folder(s)

Simple way to insert a new Column into a Cassandra ColumnFamily using the Thrift API. This snippet uses Cassandra 0.7.3 and Thrift 0.5. For a full explanation of how this code works, check out a detailed tutorial with a screencast here: http://abel-perez.com/cassandra-insert-column-example


Copy this code and paste it in your HTML
  1. import java.nio.ByteBuffer;
  2.  
  3. import org.apache.cassandra.thrift.*;
  4. import org.apache.cassandra.thrift.TBinaryProtocol;
  5.  
  6. import org.apache.thrift.protocol.*;
  7. import org.apache.thrift.transport.*;
  8. import org.apache.thrift.transport.TTransport;
  9.  
  10. /**
  11.  * Example of how to insert a new Column into a Cassandra ColumnFamily.
  12.  *
  13.  * This example assumes the keyspace is "mindplex", the ColumnFamily is
  14.  * "User", the row to add a new Column too is row id "100" and the new
  15.  * Column is "description".
  16.  *
  17.  * @author Abel perez
  18.  */
  19. public class CassandraInsertExample
  20. {
  21. public static void main(String[] args) throws Exception {
  22.  
  23. TTransport transport = new TFramedTransport(new TSocket("localhost", 9160));
  24. TProtocol protocol = new TBinaryProtocol(transport);
  25.  
  26. Cassandra.Client client = new Cassandra.Client(protocol);
  27.  
  28. client.set_keyspace("mindplex");
  29.  
  30. ColumnParent parent = new ColumnParent("User");
  31.  
  32. ByteBuffer rowid = ByteBuffer.wrap("100".getBytes());
  33.  
  34. Column column = new Column();
  35. column.setName("description".getBytes());
  36. column.setValue("some value here...".getBytes());
  37. column.setTimestamp(System.currentTimeMillis());
  38.  
  39. client.insert(rowid, parent, column, ConsistencyLevel.ONE);
  40.  
  41. transport.flush();
  42. transport.close();
  43. }
  44. }

URL: http://abel-perez.com/cassandra-insert-column-example

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.