存储riak对象时,Ruby脚本会挂起

时间:2015-10-16 12:44:19

标签: 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 => 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>'

2 个答案:

答案 0 :(得分:5)

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

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

答案 1 :(得分:0)

仅供参考,这是riak-client-2.2.1和ruby 1.9.3的工作示例

angular  
  .module('test', [])
  .service('DataLayerService', function($q, $http) {
      var self = this;
  
      self.loadData = function() {
        var configs = { cache: false };
        
        configs.params = {};
        
        return $http
          .get('http://myapp.com/api/v1/datalayer/...', configs)
          .then(function(response) {
            return response.data;
          });
      };
  });