Skip to content

Instantly share code, notes, and snippets.

@cchandler
Created September 29, 2010 20:19
Show Gist options
  • Select an option

  • Save cchandler/603460 to your computer and use it in GitHub Desktop.

Select an option

Save cchandler/603460 to your computer and use it in GitHub Desktop.
Basic Cassandra 0.7 examples
require 'cassandra/0.7'
client = Cassandra.new("Sample", "127.0.0.1:9160") # Sample is the Keyspace
# The client tries to discover all nodes the hard way. This will prevent timeouts
# from their IPs not being publicly available.
client.disable_node_auto_discovery!
### Inserting data
# The hash is for {column => value}
client.insert(:Test, "moo", {"values" => "are always strings", "other" => "thingy"})
# Inserting data with quorum consistency
client.insert(:Test, "moo", {"consistency_levels" => "weee!"}, {:consistency => Cassandra::Consistency::QUORUM})
# Insert with arbitrary client-side timestamps
client.insert(:Test, "moo_timestamp", {"value" => "more fun"}, {:timestamp => (Time.now - 1000).to_i})
### Getting data
# ALL columns under a row in a ColumnFamily, result is OrderedHash
result = client.get(:Test, "moo")
# Only the "values" column
result = client.get(:Test, "moo", {:start => "values", :finish => "values"})
# Shorter version of "values" column
result = client.get(:Test, "moo", "values")
# Read 50 columns starting from values
result = client.get(:Test, "moo", {:start => "values", :count => 50})
# Read only the values column with quorum consistency
result = client.get(:Test, "moo", "values", {:consistency => Cassandra::Consistency::QUORUM})
# Read only the values column with ALL consistency
result = client.get(:Test, "moo", "values", {:consistency => Cassandra::Consistency::ALL})
#result['moo']['value'] == "more fun"
#result['moo'].timestamps == map of last update for all retrieved columns
#Multiget different keys
result = client.multi_get(:Test, ["moo","some other key"])
# Read a range of keys, requires OrderPreservingPartitioner
# FYI: Unlike the get and multi_get, this returns an array of Thrift objects
# not the much more convenient OrderedHash object
result = client.get_range(:Test, {:start => "moo", :finish => "other"})
### Removing data
# Deleting data
client.remove(:Test, "moo_timestamp")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment