Return to Snippet

Revision: 44293
at April 9, 2011 06:27 by abelperez


Initial Code
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
{
    public static void main(String[] args) throws Exception {

        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());
        column.setTimestamp(System.currentTimeMillis());

        client.insert(rowid, parent, column, ConsistencyLevel.ONE);

        transport.flush();
        transport.close();
    }
}

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

Initial Description
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

Initial Title
Cassandra - Insert Column into ColumnFamily

Initial Tags
java

Initial Language
Java