Christian
Christian

Reputation: 4593

Ruby script hangs when storing riak object

I'm trying to execute the script hotel.rb from the book "Seven Databases in Seven Weeks". To make it work with riak 2.1.1, I had to change the creation of the client, apart from that it's the same script as can be downloaded from the books website:

require 'rubygems'
require 'riak'
STYLES = %w{single double queen king suite}
client = Riak::Client.new(:nodes => [
  {:host => 'localhost', :pb_port => 10017},
  {:host => 'localhost', :pb_port => 10027},
  {:host => 'localhost', :pb_port => 10037}
])

bucket = client.bucket('rooms')
# Create 100 floors to the building
for floor in 1..100
  current_rooms_block = floor * 100
  puts "Making rooms #{current_rooms_block} - #{current_rooms_block + 100}"
  # Put 100 rooms on each floor (huge hotel!)
  for room in 1...100
    # Create a unique room number as the key
    ro = Riak::RObject.new(bucket, (current_rooms_block + room))
    # Randomly grab a room style, and make up a capacity
    style = STYLES[rand(STYLES.length)]
    capacity = rand(8) + 1
    # Store the room information as a JSON value
    ro.content_type = "application/json"
    ro.data = {'style' => style, 'capacity' => capacity}
    puts "before storing"
    ro.store # Line 42
    puts "after storing"
  end
end

This is the output I'm getting:

chris@desktop:~/Downloads$ ruby hotel.rb  
Making rooms 100 - 200 
before storing

It looks like the script hangs when calling the store method on ro. The book used riak version 1.0.2, I'm using riak 2.1.1.

Update: Tried with Ruby 1.9.3 and Ruby 2.0.0. I'm using ubuntu 14.04.

Update 2: I used the http ports instead of pb ports, now I'm getting the following when calling store:

/var/lib/gems/2.0.0/gems/riak-client-2.2.1/lib/riak/client/beefcake/object_methods.rb:105:in `dup': can't dup Fixnum (TypeError)
    from /var/lib/gems/2.0.0/gems/riak-client-2.2.1/lib/riak/client/beefcake/object_methods.rb:105:in `maybe_encode'
    from /var/lib/gems/2.0.0/gems/riak-client-2.2.1/lib/riak/client/beefcake/object_methods.rb:18:in `dump_object'
    from /var/lib/gems/2.0.0/gems/riak-client-2.2.1/lib/riak/client/beefcake_protobuffs_backend.rb:136:in `store_object'
    from /var/lib/gems/2.0.0/gems/riak-client-2.2.1/lib/riak/client.rb:412:in `block in store_object'
    from /var/lib/gems/2.0.0/gems/riak-client-2.2.1/lib/riak/client.rb:357:in `block in recover_from'
    from /var/lib/gems/2.0.0/gems/innertube-1.0.2/lib/innertube.rb:127:in `take'
    from /var/lib/gems/2.0.0/gems/riak-client-2.2.1/lib/riak/client.rb:355:in `recover_from'
    from /var/lib/gems/2.0.0/gems/riak-client-2.2.1/lib/riak/client.rb:327:in `protobuffs'
    from /var/lib/gems/2.0.0/gems/riak-client-2.2.1/lib/riak/client.rb:411:in `store_object'
    from /var/lib/gems/2.0.0/gems/riak-client-2.2.1/lib/riak/robject.rb:144:in `store'
    from ../7dbs-code/code/riak/hotel.rb:42:in `block (2 levels) in <main>'
    from ../7dbs-code/code/riak/hotel.rb:28:in `each'
    from ../7dbs-code/code/riak/hotel.rb:28:in `block in <main>'
    from ../7dbs-code/code/riak/hotel.rb:24:in `each'
    from ../7dbs-code/code/riak/hotel.rb:24:in `<main>'

Upvotes: 3

Views: 277

Answers (3)

Alexander
Alexander

Reputation: 1

Try modifying this code for yourself. It works for me. Linux fedora, ruby version 3.1.3, riak-client 2.2.1

require 'riak'

STYLES = %w{single double queen king suite}

# Starting Client
client = Riak::Client.new(pb_port: 10017)

bucket = client.bucket('Rooms')

for floor in 1..100
  for room in 1..99
    newEntity = {
      style: STYLES[rand(STYLES.length)] ,
      capacity: rand(8) + 1
    }
    cr = bucket.new((floor * 100 + room).to_s)
    cr.data = newEntity
    puts "store #{newEntity}"
    cr.store
  end
end

Upvotes: 0

sakisk
sakisk

Reputation: 1859

Just for reference, here's a working example for riak-client-2.2.1 and ruby 1.9.3

require 'rubygems'
require 'riak'
STYLES = %w{single double queen king suite}
client = Riak::Client.new(:nodes => [
 {:host => 'localhost', :http_port => 8098}
])

bucket = client.bucket('rooms')
# Create 100 floors to the building
for floor in 1..100
  current_rooms_block = floor * 100
  puts "Making rooms #{current_rooms_block} - #{current_rooms_block + 100}"
  # Put 100 rooms on each floor (huge hotel!)
  for room in 1...100
    # Create a unique room number as the key
    ro = Riak::RObject.new(bucket, "#{current_rooms_block + room}")
    # Randomly grab a room style, and make up a capacity
    style = STYLES[rand(STYLES.length)]
    capacity = rand(8) + 1
    # Store the room information as a JSON value
    ro.content_type = "application/json"
    ro.data = {'style' => style, 'capacity' => capacity}
    ro.store
 end
end

Upvotes: 0

Joe
Joe

Reputation: 28366

The newer client requires that keynames be string.

ro = Riak::RObject.new(bucket, "#{current_rooms_block + room}")

Upvotes: 5

Related Questions