Posted by eben.hewitt on Jan 11, 2011 at 06:21 AM | Permalink | Comments (0)
Reblog (0) | | Digg This | Save to del.icio.us |
I posted the Java examples from the Cassandra book. You can get the Zip file at http://bit.ly/hVHnRI .
Posted by eben.hewitt on Dec 12, 2010 at 10:33 AM in Cassandra-NoSQL | Permalink | Comments (1)
Reblog (0) | | Digg This | Save to del.icio.us |
I've been asked lately for some sample code in Java that writes and then reads a value with Cassandra, so I thought I'd post some code that does this. This should work for 0.7 beta 1:
package com.cassandraguide.rw;
import java.io.UnsupportedEncodingException;import java.util.List;
import org.apache.cassandra.thrift.Cassandra;import org.apache.cassandra.thrift.Clock;
import org.apache.cassandra.thrift.Column;import org.apache.cassandra.thrift.ColumnOrSuperColumn;import org.apache.cassandra.thrift.ColumnParent;
import org.apache.cassandra.thrift.ColumnPath;import org.apache.cassandra.thrift.ConsistencyLevel;
import org.apache.cassandra.thrift.InvalidRequestException;
import org.apache.cassandra.thrift.NotFoundException;
import org.apache.cassandra.thrift.SlicePredicate;
import org.apache.cassandra.thrift.SliceRange;
import org.apache.cassandra.thrift.TimedOutException;
import org.apache.cassandra.thrift.UnavailableException;
import org.apache.log4j.Logger;import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
public class SimpleWriteRead {
private static final Logger LOG = Logger.getLogger(SimpleWriteRead.class);
//set up some constants private static final String UTF8 = "UTF8";
private static final String HOST = "localhost";private static final int PORT = 9160;
private static final ConsistencyLevel CL = ConsistencyLevel.ONE;
//not paying attention to exceptions here
public static void main(String[] args) throws UnsupportedEncodingException,
InvalidRequestException, UnavailableException, TimedOutException, TException, NotFoundException {
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");
String cfName = "Standard1"; byte[] userIDKey = "1".getBytes(); //this is a row key
Clock clock = new Clock(System.currentTimeMillis());
//create a representation of the Name column
ColumnPath colPathName = new ColumnPath(cfName);
colPathName.setColumn("name".getBytes(UTF8));
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);
//insert the Age column
client.insert(userIDKey, cp, new Column("age".getBytes(UTF8), "69".getBytes(), clock), CL); LOG.debug("Row insert done.");
// read just the Name column
LOG.debug("Reading Name Column:");
Column col = client.get(userIDKey, colPathName, CL).getColumn();
LOG.debug("Column name: " + new String(col.name, UTF8));
LOG.debug("Column value: " + new String(col.value, UTF8));
LOG.debug("Column timestamp: " + col.clock.timestamp);
//create a slice predicate representing the columns to read
//start and finish are the range of columns--here, all
SlicePredicate predicate = new SlicePredicate();
SliceRange sliceRange = new SliceRange(); sliceRange.setStart(new byte[0]);
sliceRange.setFinish(new byte[0]);
predicate.setSlice_range(sliceRange);
LOG.debug("Complete Row:"); // read all columns in the row
ColumnParent parent = new ColumnParent(cfName);
List<ColumnOrSuperColumn> results = client.get_slice(userIDKey, parent, predicate, CL);
//loop over columns, outputting values
for (ColumnOrSuperColumn result : results) {
Column column = result.column;
LOG.debug(new String(column.name, UTF8) + " : "
+ new String(column.value, UTF8)); } tf.close();
LOG.debug("All done."); }
}
This will have output like this:
DEBUG 14:02:09,572 Inserting row for key 1
DEBUG 14:02:09,580 Row insert done.
DEBUG 14:02:09,580 Reading Name Column:
DEBUG 14:02:09,585 Column name: name
DEBUG 14:02:09,586 Column value: George Clinton
DEBUG 14:02:09,586 Column timestamp: 1284325329569
DEBUG 14:02:09,589 Complete Row:
DEBUG 14:02:09,594 age : 69
DEBUG 14:02:09,594 name : George Clinton
DEBUG 14:02:09,594 All done.
Posted by eben.hewitt on Oct 20, 2010 at 07:16 AM | Permalink | Comments (2)
Reblog (0) | | Digg This | Save to del.icio.us |
I've just finished putting together a rough first draft of an appendix for Cassandra: The Definitive Guide. It's a Glossary of Terms that covers everything from Anti-Entropy to Vector Clocks.
I wanted to post it here to help sort out the many tricky terms that one comes across when getting started with Cassandra. I'm writing the book in DocBook XML, so this is just a plain generated HTML page.
You can read it at http://io.typepad.com/glossary.html.
I hope that it proves useful. I'd be grateful for any feedback to help improve it.
I've got to say that the Cassandra community is honestly the greatest. Many thanks to the hard work of so many fantastic developers who created not only this database, but such a vibrant and positive atmosphere. Thanks in particular to Jonathan Ellis, Benjamin Black, Ran Tavory, Eric Evans, Gary Dusbabek, Paul Prescod, Stu Hood, Ryan King, and the many others that are helping Cassandra grow. You guys rock.
Posted by eben.hewitt on May 15, 2010 at 05:45 PM | Permalink | Comments (0)
Reblog (0) | | Digg This | Save to del.icio.us |
Set up this new blog to help people learn about Apache Cassandra and related NoSQL technologies.
I will add some files from the book as I get them prepared, and add other Cassandra-related information here, so check back later.
If you're looking for posts on Java, web services, recipes, or stuff on Java SOA Cookbook, then you can still access that blog at http://io.typepad.com/eben_hewitt_on_java
Happy reading!
Posted by eben.hewitt on May 15, 2010 at 05:13 PM | Permalink | Comments (0)
Reblog (0) | | Digg This | Save to del.icio.us |