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.

    require 'java'
    
    # All the files from cassandra/lib
    require 'lib/antlr-3.1.3.jar'
    require 'lib/apache-cassandra-0.6.1.jar'
    require 'lib/avro-1.2.0-dev.jar'
    require 'lib/clhm-production.jar'
    require 'lib/commons-cli-1.1.jar'
    require 'lib/commons-codec-1.2.jar'
    require 'lib/commons-collections-3.2.1.jar'
    require 'lib/commons-lang-2.4.jar'
    require 'lib/google-collections-1.0.jar'
    require 'lib/hadoop-core-0.20.1.jar'
    require 'lib/high-scale-lib.jar'
    require 'lib/ivy-2.1.0.jar'
    require 'lib/jackson-core-asl-1.4.0.jar'
    require 'lib/jackson-mapper-asl-1.4.0.jar'
    require 'lib/jline-0.9.94.jar'
    require 'lib/json-simple-1.1.jar'
    require 'lib/libthrift-r917130.jar'
    require 'lib/log4j-1.2.14.jar'
    require 'lib/slf4j-api-1.5.8.jar'
    require 'lib/slf4j-log4j12-1.5.8.jar'
    
    java_import org.apache.cassandra.thrift.Cassandra
    java_import org.apache.cassandra.thrift.ColumnOrSuperColumn
    java_import org.apache.cassandra.thrift.ColumnPath
    java_import org.apache.cassandra.thrift.Column
    java_import org.apache.cassandra.thrift.ColumnParent
    java_import org.apache.cassandra.thrift.ConsistencyLevel
    java_import org.apache.cassandra.thrift.InvalidRequestException
    java_import org.apache.cassandra.thrift.NotFoundException
    java_import org.apache.cassandra.thrift.TimedOutException
    java_import org.apache.cassandra.thrift.UnavailableException
    java_import org.apache.thrift.TException
    java_import org.apache.thrift.protocol.TBinaryProtocol
    java_import org.apache.thrift.protocol.TProtocol
    java_import org.apache.thrift.transport.TSocket
    java_import org.apache.thrift.transport.TTransport
    java_import org.apache.thrift.transport.TTransportException
    
    tr = TSocket.new("10.0.0.151", 9160)
    proto = TBinaryProtocol.new(tr)
    client = Cassandra::Client.new(proto)
    tr.open
    
    puts client.describe_cluster_name
    puts client.describe_keyspaces
    
    timestamp = java.lang.System.currentTimeMillis()
    path = ColumnPath.new("CrawlData")
    path.setColumn(java.lang.String.new("url").getBytes("utf-8"))
    
    client.insert("Crawl", "abc", path, java.lang.String.new("http://google.com").getBytes("utf8"), timestamp, ConsistencyLevel::ONE)
    
    tr.close
    
    #cassandra> get Crawl.CrawlData['abc']
    #=> (column=url, value=http://google.com, timestamp=1275509007789)
    #Returned 1 results.
    #cassandra> get Crawl.CrawlData['abc']['url']
    #=> (column=url, value=http://google.com, timestamp=1275509007789)
    #cassandra> get Crawl.CrawlData['abc']
    #=> (column=url, value=http://google.com, timestamp=1275509007789)
    #Returned 1 results.

Loading JARs into the Classpath

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