/ Published in: Java
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
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
import java.nio.ByteBuffer; import org.apache.cassandra.thrift.*; import org.apache.cassandra.thrift.TBinaryProtocol; import org.apache.thrift.protocol.*; import org.apache.thrift.transport.*; import org.apache.thrift.transport.TTransport; /** * Example of how to insert a new Column into a Cassandra ColumnFamily. * * This example assumes the keyspace is "mindplex", the ColumnFamily is * "User", the row to add a new Column too is row id "100" and the new * Column is "description". * * @author Abel perez */ public class CassandraInsertExample { TTransport transport = new TFramedTransport(new TSocket("localhost", 9160)); TProtocol protocol = new TBinaryProtocol(transport); Cassandra.Client client = new Cassandra.Client(protocol); client.set_keyspace("mindplex"); ColumnParent parent = new ColumnParent("User"); ByteBuffer rowid = ByteBuffer.wrap("100".getBytes()); Column column = new Column(); column.setName("description".getBytes()); column.setValue("some value here...".getBytes()); client.insert(rowid, parent, column, ConsistencyLevel.ONE); transport.flush(); transport.close(); } }
URL: http://abel-perez.com/cassandra-insert-column-example