Database Reference
In-Depth Information
Let's unpack what we've done here. First, we create a connection to the Cassandra server:
TTransport tr = new TSocket(HOST, PORT);
//new default in 0.7 is framed transport
TFramedTransport tf = new TFramedTransport(tr);
TProtocol proto = new TBinaryProtocol(tf);
Cassandra.Client client = new Cassandra.Client(proto);
tf.open();
client.set_keyspace("Keyspace1");
Here we're using the framed transport, which is the new default in Cassandra 0.7. This code con-
nects to Cassandra at the specified keyspace.
Then, we create representations for the column family we'll use, and convenience values for the
row key and clock that indicate when this insert was performed:
String cfName = "Standard1";
byte[] userIDKey = "1".getBytes(); //this is a row key
Clock clock = new Clock(System.currentTimeMillis());
Next, we use the client object with the column path to insert a new value:
ColumnParent cp = new ColumnParent(cfName);
//insert the name column
LOG.debug("Inserting row for key " + new String(userIDKey));
client.insert(userIDKey, cp,
new Column("name".getBytes(UTF8),
"George Clinton".getBytes(), clock), CL);
The insert operation requires a row key, as well as the column object that includes the column
name and the value we want to assign to it for this row key. We also specify the clock represent-
ing when this insert was performed, and the consistency level to apply.
We then basically repeat this operation to write to the same row, but now to the age column,
giving it a value of 69:
client.insert(userIDKey, cp,
new Column("age".getBytes(UTF8),
"69".getBytes(), clock), CL);
So at this point we have inserted two columns into a single row and are ready to read it back to
verify.
Search WWH ::




Custom Search