存储riak对象时Ruby脚本挂起

smdnsysy  于 2023-03-01  发布在  Ruby
关注(0)|答案(3)|浏览(160)

我正在尝试执行《Seven Databases in Seven Weeks》一书中的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端口,现在我得到以下调用存储时:
/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>'
c0vxltue

c0vxltue1#

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

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

ffvjumwh2#

这里有一个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
yr9zkbsy

yr9zkbsy3#

尝试自己修改这段代码。它对我很有效。Linux fedora,ruby版本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

相关问题