Code Shop http://journlr.com A bit of Ruby, a bit of JavaScript, a bit of love... posterous.com Wed, 02 Jun 2010 13:13:36 -0700 JRuby Cassandra Thrift Example http://journlr.com/jruby-cassandra-thrift-example http://journlr.com/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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/68361/photo.jpg http://posterous.com/people/15Zzt2e7kgV Abhi Yerra abhi Abhi Yerra
Sun, 30 May 2010 10:36:17 -0700 Location of Rubinus Grammar Parser http://journlr.com/location-of-rubinus-grammar-parser http://journlr.com/location-of-rubinus-grammar-parser The Rubinius parser is located in lib/ext/melbourne of the source tree. I was a bit confused where this was initially.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/68361/photo.jpg http://posterous.com/people/15Zzt2e7kgV Abhi Yerra abhi Abhi Yerra
Fri, 28 May 2010 23:55:24 -0700 Setting up JRuby on Rails http://journlr.com/setting-up-jruby-on-rails http://journlr.com/setting-up-jruby-on-rails

I use JRuby a lot more than I use MRI Ruby. I just like the Java VM a lot more. Anyways, to setup a JRuby project you need to do a few different things. Gem install activerecord-jdbc-adapter and one of these based on the database you are going to be using.

1 activerecord-jdbcderby-adapter
2 activerecord-jdbch2-adapter
3 activerecord-jdbchsqldb-adapter
4 activerecord-jdbcmssql-adapter
5 activerecord-jdbcmysql-adapter
6 activerecord-jdbcpostgresql-adapter 
7 activerecord-jdbcsqlite3-adapter

Then run:

1 ./script/generate jdbc

Everything else should be the same.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/68361/photo.jpg http://posterous.com/people/15Zzt2e7kgV Abhi Yerra abhi Abhi Yerra
Sat, 11 Jul 2009 20:44:01 -0700 Ruby HTTP Multipart Post http://journlr.com/ruby-http-multipart-post http://journlr.com/ruby-http-multipart-post Posting multipart to Ruby is annoying to say the least because it isn't implemented in the native libraries. I have had difficulty getting this working correctly even while following the many methods that have been written about it. So I completely bypass Ruby and use curl to do the posting. It works reasonably well and gets the job done!
 

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/68361/photo.jpg http://posterous.com/people/15Zzt2e7kgV Abhi Yerra abhi Abhi Yerra
Fri, 10 Jul 2009 21:09:00 -0700 Command Line Flickr Uploader http://journlr.com/flickr-uploader http://journlr.com/flickr-uploader

I dislike GUI tools and posting to Flickr usually seems like a chore more than as an enjoyable means of posting new pictures. With that in mind I have written this command line Flickr uploader. It will be a constant work in progress where I will add features as I need to use them. So with that in mind download it from here.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/68361/photo.jpg http://posterous.com/people/15Zzt2e7kgV Abhi Yerra abhi Abhi Yerra