Ruby脚本在存储riak对象时挂起

Ruby脚本在存储riak对象时挂起,ruby,riak,Ruby,Riak,我正在尝试执行《七周七个数据库》一书中的脚本hotel.rb。为了使它与riak 2.1.1兼容,我必须更改客户端的创建,除了它的脚本与可以从图书网站下载的脚本相同外: require 'rubygems' require 'riak' STYLES = %w{single double queen king suite} client = Riak::Client.new(:nodes => [ {:host => 'localhost', :pb_port => 100

我正在尝试执行《七周七个数据库》一书中的脚本hotel.rb。为了使它与riak 2.1.1兼容,我必须更改客户端的创建,除了它的脚本与可以从图书网站下载的脚本相同外:

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
这是我得到的输出:

chris@desktop:~/Downloads$ ruby hotel.rb  
Making rooms 100 - 200 
before storing
在ro上调用store方法时,脚本似乎挂起。 这本书使用了riak版本1.0.2,我使用的是riak 2.1.1

更新:尝试使用Ruby 1.9.3和Ruby 2.0.0。我使用的是ubuntu 14.04

更新2:我使用了http端口而不是pb端口,现在在调用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>'
/var/lib/gems/2.0.0/gems/riak-client-2.2.1/lib/riak/client/beefcake/object_methods.rb:105:在'dup'中:无法重复Fixnum(TypeError)
来自/var/lib/gems/2.0.0/gems/riak-client-2.2.1/lib/riak/client/beefcake/object_methods.rb:105:in'maybe_encode'
来自/var/lib/gems/2.0.0/gems/riak-client-2.2.1/lib/riak/client/beefcake/object_methods.rb:18:in'dump_object'
来自/var/lib/gems/2.0.0/gems/riak-client-2.2.1/lib/riak/client/beefcake\u protobuff\u backend.rb:136:in'store\u 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“protobuff”
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:在“商店”中
from../7dbs code/code/riak/hotel.rb:42:in“街区(2层)in”
from../7dbs code/code/riak/hotel.rb:28:in'each'
from../7dbs code/code/riak/hotel.rb:28:in'block in'
from../7dbs code/code/riak/hotel.rb:24:in'each'
from../7dbs code/code/riak/hotel.rb:24:in`'

较新的客户端要求键名为字符串

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

仅供参考,这里有一个riak-client-2.2.1和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

您是否尝试过将客户端中的节点一次减少到一个?也许其中一个端口有问题。您也可以尝试在pb_端口上转储数据包(ala tcpdump),可能服务器返回了一个客户端无法识别的错误。感谢您的提示,我使用了错误的端口。但是它仍然不起作用(参见更新2)。较新的客户端是否要求键名为字符串?这只是一个猜测,试试看:
ro=Riak::RObject.new(bucket,“{current\u rooms\u block+room}”)
你说得对,非常感谢!如果你想写答案,我很乐意接受。我运气不好。尝试了ruby 1.9.3和ruby 2.0,在这两种情况下,riak-client-2.2.1表现相同(存储部分中的块),即使键名是字符串。明白了!由于riak ruby client 2.0.0发行版-2014-09-05
http支持已从riak ruby客户端中移除,转而关注协议缓冲区,因此我还不得不使用
:http\u port
而不是
:pb\u port