|
Project Information
Featured
Downloads
Links
|
Jassandra is a Java Cassandra client.2.2.0 released, with support to Cassandra 1.0.8, and passed all existing test cases. Check it out! There are three downloads from this project:
It's designed to be simple and intuitive, esp. for the developers coming from JDBC background. Latest Updates
Quick Links
Documentation
Future Plan
Links
Sample CodeHere is a quick sample: public void testSample() throws JassandraException {
final String userName = "jsmith";
// 1. Gets a connection
Properties info = new Properties();
info.put(DriverManager.CONSISTENCY_LEVEL,
ConsistencyLevel.ONE.toString());
IConnection connection = DriverManager.getConnection(
"thrift://localhost:9160", info);
try {
// 2. Get a KeySpace by name
IKeySpace keySpace = connection.getKeySpace("Keyspace1");
// 3. Get a ColumnFamily by name
IColumnFamily cf = keySpace.getColumnFamily("Standard2");
// 4. Insert like this
long now = System.currentTimeMillis();
ByteArray nameFirst = ByteArray.ofASCII("first");
ByteArray nameLast = ByteArray.ofASCII("last");
ByteArray nameAge = ByteArray.ofASCII("age");
ByteArray valueLast = ByteArray.ofUTF8("Smith");
IColumn colFirst = new Column(nameFirst, ByteArray.ofUTF8("John"),
now);
cf.insert(userName, colFirst);
IColumn colLast = new Column(nameLast, valueLast, now);
cf.insert(userName, colLast);
IColumn colAge = new Column(nameAge, ByteArray.ofLong(42), now);
cf.insert(userName, colAge);
// 5. Select like this
ICriteria criteria = cf.createCriteria();
criteria.keyList(Lists.newArrayList(userName))
.columnRange(nameAge, nameLast, 10);
Map<String, List<IColumn>> map = criteria.select();
List<IColumn> list = map.get(userName);
Assert.assertEquals(3, list.size());
Assert.assertEquals(valueLast, list.get(2).getValue());
// 6. Delete like this
cf.delete(userName, colFirst);
map = criteria.select();
Assert.assertEquals(2, map.get(userName).size());
// 7. Get count like this
criteria = cf.createCriteria();
criteria.keyList(Lists.newArrayList(userName));
int count = criteria.count();
Assert.assertEquals(2, count);
} finally {
// 8. Don't forget to close the connection.
connection.close();
}
}
|