Created
July 3, 2014 16:39
-
-
Save beccam/c1957bd866d5f9f59b3e to your computer and use it in GitHub Desktop.
Getting Started with Apache Cassandra and Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'cql' | |
# connect to the cluster | |
client = Cql::Client.connect(hosts: ['127.0.0.1']) | |
client.use('demo') | |
# insert a user | |
client.execute("INSERT INTO users (lastname, age, city, email, firstname) VALUES ('Jones', 35, 'Austin', '[email protected]', 'Bob')") | |
# Use select to get the user we just entered | |
rows = client.execute("SELECT firstname, age FROM users WHERE lastname='Jones'") | |
rows.each do |row| | |
row.each do |key, value| | |
puts "#{key} = #{value}" | |
end | |
end | |
# Update the same user with a new age | |
client.execute("UPDATE users SET age = 36 WHERE lastname = 'Jones'") | |
# Select and show the change | |
rows = client.execute("SELECT firstname, age FROM users WHERE lastname='Jones'") | |
rows.each do |row| | |
row.each do |key, value| | |
puts "#{key} = #{value}" | |
end | |
end | |
# Delete the user from the users table | |
client.execute("DELETE FROM users WHERE lastname = 'Jones'") | |
# Show that the user is gone | |
rows = client.execute("SELECT * FROM users") | |
rows.each do |row| | |
row.each do |key, value| | |
puts "#{key} = #{value}" | |
end | |
end | |
# Close the connection | |
client.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment