0

我在我的 os x 机器上运行了 4 节点 riak 设置。我有以下程序 -

require 'riak'




class RiakClient < Riak::Client
    #attr_accessor :bucket

    def initialize(hosts="")
        return Riak::Client.new(:nodes => [{:http_port => 8091},{:http_port =>8092},{:http_port=>8093},{:http_port =>8094}])
    end

    def get_me(bucket, key)
        obj = self.bucket(bucket).get(key)
        puts obj.data
    end

    def put_me(bucket, key, data, content_type)
        obj=self.bucket(bucket).get_or_new(key)
        puts obj.class
        obj.content_type=content_type
        obj.raw_data=data
        obj.store
    end
end



if __FILE__ == $0
    my_client=RiakClient.new
    my_client.put_me("doc", "index.html", "some data goes here", "text/html")
    hash=my_client.get_me("doc", "index.html")
end

我收到以下错误

NilClass
riak_client.rb:32:in `put_me': undefined method `content_type=' for nil:NilClass (NoMethodError)
    from riak_client.rb:42:in `<main>'

我必须导入 RiakBucket 和 RiakObject 类吗?好像这里不能访问 RiakBucket 方法?

4

1 回答 1

1

这里的实际问题是:为什么get_or_new返回 nil?

这可能是因为您的initialize()方法返回了一个新的 Riak::Client,但作为您的父对象的 Riak::Client 永远不会被初始化。尝试调用 super 而不是return Riak::Client.newin initialize

继承 Riak::Client 在这里有点可疑。我倾向于委托给它。

于 2012-06-25T06:19:22.310 回答