Skip to content

Instantly share code, notes, and snippets.

@christineyen
Created November 27, 2012 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christineyen/4153497 to your computer and use it in GitHub Desktop.
Save christineyen/4153497 to your computer and use it in GitHub Desktop.
cassandra experimentation
## via shell:
## fetches + runs the correct version of Cassandra for you
# CASSANDRA_VERSION=1.1 cassandra_helper cassandra
## via CLI:
# create keyspace Parse with placement_strategy='org.apache.cassandra.locator.SimpleStrategy' AND strategy_options = [{ replication_factor: 1 }];
# use Parse;
# via irb:
require 'rubygems'
require 'cassandra/1.1'
store = Cassandra.new('Parse')
cf_def = CassandraThrift::CfDef.new(:keyspace => 'Parse', :name => 'ApiStats', :default_validation_class => 'CounterColumnType', :comparator_type => 'AsciiType', :key_validation_class => 'UTF8Type')
store.add_column_family(cf_def)
# column names are a stringified representation of time; a numeric representation of minutes may be better, or a composite (hour, minute) column name
store.add('ApiStats', 'app-event-20121124', 1, '18:10')
store.add('ApiStats', 'app-event-20121125', 1, '06:45')
store.add('ApiStats', 'app-event-20121120', 1, '08:01')
store.add('ApiStats', 'app-event-20121128', 1, '11:09')
store.add('ApiStats', 'app-event-20121125', 1, '07:15')
store.add('ApiStats', 'app-event-20121126', 1, '13:01')
# doesn't really work -- order is order of insertion?
store.get_range_keys('ApiStats', :start_key => 'app-event-2', :finish_key => 'app-event-2012')
# isn't smart enough to return the correct row keys; see email re: range queries on row keys.
# Theoretically, filtering like this should be possible, but I haven't been able to get it to work...
store.get_range_keys('ApiStats', :start_key => 'app-event-20121125', :finish_key => 'app-event-20121126')
cf_def = cf_def = CassandraThrift::CfDef.new(:keyspace => 'Parse', :name => 'StatsIndex', :comparator_type => 'UTF8Type')
store.add_column_family(cf_def)
# Example of dynamic column names used to store data
store.insert(:StatsIndex, 'app-event', { 'app-event-20121120' => '' })
store.insert(:StatsIndex, 'app-event', { 'app-event-20121124' => '' })
store.insert(:StatsIndex, 'app-event', { 'app-event-20121125' => '' })
store.insert(:StatsIndex, 'app-event', { 'app-event-20121126' => '' })
store.insert(:StatsIndex, 'app-event', { 'app-event-20121128' => '' })
# Columns, however, are ordered on disk + support range queries. Note that when we initialized the Column Family,
# we set the default comparator_type to 'UTF8Type'.
hsh = store.get_range('StatsIndex', :start => 'app-event-20121125', :finish => 'app-event-20121128')
hsh['app-event'].keys
=> ["app-event-20121125", "app-event-20121126", "app-event-20121128"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment