Search code examples
ruby-on-railsrubycassandrathriftirb

Cassandra and creating an int column name from Ruby client


I am attempting to create dynamic columns with a comparator/validator that is a 32 bit signed integer. This obviously will save on storage space amongst other advantages. Currently, this works great if I have a UTF8Type validator (Using Twitter's cassandra client for Ruby):

db.insert(:foo, 'mykey', {'mycol' => 'myval'})

This is where the problem occurs:

db.insert(:foo, 'mykey', {5 => 'myval'})

I think this is more of a Ruby issue than Cassandra issue. Using Rails console, I get the following thrown out at me:

TypeError: no implicit conversion of Fixnum into String

Further clarification, I can't simply do:

db.insert(:foo, 'mykey', {'5' => 'myval'})

This will trigger a validation fail which is expecting an integer for the column and not a string.

Is there a way to make this reasonably work in Ruby so that I don't have to use UTF8Type column names and can stick to int based ones for my Cassandra 1.2 based app?


Solution

  • The twitter Cassandra library leaves it to you the developer to serialize/deserialize all values. It requires everything that you give it to be in binary string representation. So if you want to use ints as your comparator you need to pack them before inserting and unpack them when fetching them out of Cassandra. Your insert needs to look like this:

    db.insert(:foo, 'mykey', {[5].pack('N*') => 'myval'})