abhi's logbook

Adventures in coding and coding for adventures.

JRuby

Cassandra Thrift Example

Here is how to connect to Cassandra using JRuby using the Java Thrift API.
I was a bit annoyed that the Ruby libraries were using native libraries so I wrote it using JRuby.

 1    require 'java'
 2    
 3    # All the files from cassandra/lib
 4    require 'lib/antlr-3.1.3.jar'
 5    require 'lib/apache-cassandra-0.6.1.jar'
 6    require 'lib/avro-1.2.0-dev.jar'
 7    require 'lib/clhm-production.jar'
 8    require 'lib/commons-cli-1.1.jar'
 9    require 'lib/commons-codec-1.2.jar'
10    require 'lib/commons-collections-3.2.1.jar'
11    require 'lib/commons-lang-2.4.jar'
12    require 'lib/google-collections-1.0.jar'
13    require 'lib/hadoop-core-0.20.1.jar'
14    require 'lib/high-scale-lib.jar'
15    require 'lib/ivy-2.1.0.jar'
16    require 'lib/jackson-core-asl-1.4.0.jar'
17    require 'lib/jackson-mapper-asl-1.4.0.jar'
18    require 'lib/jline-0.9.94.jar'
19    require 'lib/json-simple-1.1.jar'
20    require 'lib/libthrift-r917130.jar'
21    require 'lib/log4j-1.2.14.jar'
22    require 'lib/slf4j-api-1.5.8.jar'
23    require 'lib/slf4j-log4j12-1.5.8.jar'
24    
25    java_import org.apache.cassandra.thrift.Cassandra
26    java_import org.apache.cassandra.thrift.ColumnOrSuperColumn
27    java_import org.apache.cassandra.thrift.ColumnPath
28    java_import org.apache.cassandra.thrift.Column
29    java_import org.apache.cassandra.thrift.ColumnParent
30    java_import org.apache.cassandra.thrift.ConsistencyLevel
31    java_import org.apache.cassandra.thrift.InvalidRequestException
32    java_import org.apache.cassandra.thrift.NotFoundException
33    java_import org.apache.cassandra.thrift.TimedOutException
34    java_import org.apache.cassandra.thrift.UnavailableException
35    java_import org.apache.thrift.TException
36    java_import org.apache.thrift.protocol.TBinaryProtocol
37    java_import org.apache.thrift.protocol.TProtocol
38    java_import org.apache.thrift.transport.TSocket
39    java_import org.apache.thrift.transport.TTransport
40    java_import org.apache.thrift.transport.TTransportException
41    
42    tr = TSocket.new("10.0.0.151", 9160)
43    proto = TBinaryProtocol.new(tr)
44    client = Cassandra::Client.new(proto)
45    tr.open
46    
47    puts client.describe_cluster_name
48    puts client.describe_keyspaces
49    
50    timestamp = java.lang.System.currentTimeMillis()
51    path = ColumnPath.new("CrawlData")
52    path.setColumn(java.lang.String.new("url").getBytes("utf-8"))
53    
54    client.insert("Crawl", "abc", path, java.lang.String.new("http://google.com").getBytes("utf8"), timestamp, ConsistencyLevel::ONE)
55    
56    tr.close
57    
58    #cassandra> get Crawl.CrawlData['abc']
59    #=> (column=url, value=http://google.com, timestamp=1275509007789)
60    #Returned 1 results.
61    #cassandra> get Crawl.CrawlData['abc']['url']
62    #=> (column=url, value=http://google.com, timestamp=1275509007789)
63    #cassandra> get Crawl.CrawlData['abc']
64    #=> (column=url, value=http://google.com, timestamp=1275509007789)
65    #Returned 1 results.

Loading JARs into the Classpath

Dir.glob(Rails.root.join("java/*.jar")).each { |dir| $CLASSPATH << dir }