Created
April 14, 2010 13:01
-
-
Save seancribbs/365791 to your computer and use it in GitHub Desktop.
Code samples from my demos at Boston.rb - "Introducing Riak and Ripple"
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 'riak' | |
client = Riak::Client.new | |
bucket = client['people'] | |
sean = bucket.new('sean') | |
sean.data = {"name" => "Sean Cribbs"} | |
puts "Storing Justin." | |
justin = bucket.new('justin') | |
justin.data = {"name" => "Justin Sheehy"} | |
justin.store | |
puts "Storing Rusty." | |
rusty = bucket.new('rusty') | |
rusty.data = {"name" => "Rusty Klophaus"} | |
rusty.store | |
puts "Adding links and storing Sean." | |
sean.links << justin.to_link("boss") | |
# </riak/people/justin>; riaktag="boss" | |
sean.links << rusty.to_link("coworker") | |
# </riak/people/rusty>; riaktag="coworker" | |
sean.store | |
# Link-walk to all people | |
puts "All people linked from Sean:" | |
puts sean.walk(:bucket => "people").first.map(&:data) # returns both | |
# Link-walk to coworkers only | |
puts "All coworkers linked from Sean:" | |
puts sean.walk(:tag => "coworker").first.map(&:data) # returns Rusty |
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 'csv' | |
require 'riak' | |
client = Riak::Client.new | |
bucket = client.bucket('goog', :keys => false) | |
CSV.foreach('goog.csv', | |
:headers => true) do |row| | |
puts row.first[1].to_s | |
obj = bucket.new(row.first[1].to_s) | |
obj.data = Hash[row.to_a] | |
obj.store | |
end |
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 'riak' | |
client = Riak::Client.new | |
puts "Map-Reduce all data" | |
gets | |
data = Riak::MapReduce.new(client). | |
add('goog'). | |
map("Riak.mapValuesJson", :keep => true).run | |
puts data | |
puts "M/R all the days where high was above $600." | |
gets | |
map_filter = <<-JAVASCRIPT | |
function(value, keyData, arg) { | |
var data = Riak.mapValuesJson(value)[0]; | |
if(data.High && parseFloat(data.High) > 600.00) | |
return [value.key]; | |
else | |
return []; | |
} | |
JAVASCRIPT | |
data = Riak::MapReduce.new(client). | |
add('goog'). | |
map(map_filter). | |
reduce("Riak.reduceSort", :keep => true). | |
run | |
puts data |
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 'ripple' | |
class Person | |
include Ripple::Document | |
property :name, String | |
validates_presence_of :name | |
end | |
puts "Loading all people." | |
people = Person.all | |
puts people.inspect | |
puts "Creating John." | |
johnm = Person.new(:name => "John Muellerleile") | |
johnm.key = "johnm" | |
johnm.save | |
puts "Reloading all people." | |
people = Person.all | |
puts people.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment