0

我是红宝石新手。但由于一些问题,我必须处理代码,因为我们的 ruby​​ 开发人员不可用。我们正在使用 cassandra 数据库从 Ruby (Sinatra) Web 服务中获取值并将其放入 Cassandra 键空间。但由于某些问题,数据无法插入。

在以下代码中,partners_daily、partner_monthly 等是统计键空间(数据库)中的列族(表)。

if params and !partner_id.nil? and !activity_type.nil?
            {
                :partners_daily    => "#{partner_id}_#{activity_type}_#{success == 1 ? 'sucess' : "failure:#{failure_code}"}_#{time.year}_#{time.month}_#{time.day}",
                :partners_monthly  => "#{partner_id}_#{activity_type}_#{success == 1 ? 'sucess' : "failure:#{failure_code}"}_#{time.year}_#{time.month}",
                :partners_alltime  => "#{partner_id}_#{activity_type}_#{success == 1 ? 'sucess' : "failure:#{failure_code}"}",

                :channels_daily    => "#{channel_id}_#{activity_type}_#{success == 1 ? 'sucess' : "failure:#{failure_code}"}_#{time.year}_#{time.month}_#{time.day}",
                :channels_monthly  => "#{channel_id}_#{activity_type}_#{success == 1 ? 'sucess' : "failure:#{failure_code}"}_#{time.year}_#{time.month}",
                :channels_alltime  => "#{channel_id}_#{activity_type}_#{success == 1 ? 'sucess' : "failure:#{failure_code}"}",

                :countries_daily    => "#{country}_#{activity_type}_#{success == 1 ? 'sucess' : "failure:#{failure_code}"}_#{time.year}_#{time.month}_#{time.day}",
                :countries_monthly  => "#{country}_#{activity_type}_#{success == 1 ? 'sucess' : "failure:#{failure_code}"}_#{time.year}_#{time.month}",
                :countries_alltime  => "#{country}_#{activity_type}_#{success == 1 ? 'sucess' : "failure:#{failure_code}"}"
            }.each do |k,v| 
                stats.add(k, v, 1, 'count')
            end


            return "Activity stored in stats"
        end
    else
        return "Error: client headers missing"
    end
end

def count(table, key)

    require 'cassandra-cql' # requiring this at the top was leading to error: unconfigured columnfamily
    cqldb = CassandraCQL::Database.new('127.0.0.1:9160', {:keyspace => 'plystats'})

    query = "update partners_daily set count = ? where key = ?"#"update #{table} set count = count+1 where key = ?;"

    #return cqldb.execute(query, 0, 'sonia').inspect
    return query    

end

我想知道其中的数据插入逻辑是如何执行的,在哪里?在stats.add(k, v, 1, 'count')吗?

插入部分是否有任何错误,因为它失败了。

4

1 回答 1

0

我想知道其中的数据插入逻辑是如何执行的,在哪里?它在 stats.add(k, v, 1, 'count') 中吗?

是的,这就是它应该发生的地方。之间{}是字典/哈希值:

{
   :partners_daily => # …
}.each do |k,v|

以该方法开始一个循环,将each每个条目分解并放入kand v,其中的 key ink和 in 的值v。例如,哈希中的第一条记录是:

:partners_daily    => "#{partner_id}_#{activity_type}_#{success == 1 ? 'sucess' : "failure:#{failure_code}"}_#{time.year}_#{time.month}_#{time.day}",

然后,这将在each循环内分解为:

k = :partners_daily
v =  # The result of
     "#{partner_id}_#{activity_type}_#{success == 1 ? 'sucess' : "failure:#{failure_code}"}_#{time.year}_#{time.month}_#{time.day}",

我现在不知道这些值是什么partner_id等,但是编造一些看起来像"123_sales_sucess_2013_6_01"

请注意,那里的单词success有错字。

由于多个双引号和大括号,这有点令人困惑,所以我将其更改为:

[partner_id, activity_type, (success == 1 ? 'success' : "failure:#{failure_code}"), time.year, time.month, time.day].join("_")

但是请注意那里有很多重复,所以我将整个哈希更改为(至少):

success_string = success == 1 ?
                   'success' : 
                   "failure:#{failure_code}"

data = {
  :partners_daily    => [partner_id, activity_type,success_string,time.year,time.month,time.day].join("_"),
  :partners_monthly  => [partner_id,activity_type,success_string,time.year,time.month].join("_"),
  :partners_alltime  => [partner_id,activity_type,success_string].join("_"),

  :channels_daily    => [channel_id,activity_type,success_string,time.year,time.month,time.day].join("_"),
  :channels_monthly  => [channel_id,activity_type,success_string,time.year,time.month].join("_"),
  :channels_alltime  => [channel_id,activity_type,success_string].join("_"),

  :countries_daily    => [country,activity_type,success_string,time.year,time.month,time.day].join("_"),
  :countries_monthly  => [country,activity_type,success_string,time.year,time.month].join("_"),
  :countries_alltime  => [country,activity_type,success_string].join("_")
}

data.each do |k,v|
  # more code…

它开始更容易阅读和查看逻辑。此外,通过将散列放入data变量而不是立即处理它,它可以让您更轻松地检查它,例如

warn "data = #{data.inspect}"

会将数据的表示形式输出到控制台,因此至少您可以了解脚本试图放入的内容。在此代码的顶部,您还可以添加warn "script = #{script.inspect}"以检查script对象的外观。

如果script对象是一个 Cassandra 实例,即有类似script = Cassandra.new "blah", "blahblah"的设置它,那么add方法就是这个

给出的签名是add(column_family, key, value, *columns_and_options),但这似乎与您的呼叫不匹配:

stats.add(k, v, 1, 'count')

应该(可能)是:

stats.add('count', k, v, 1)

事实上,我什至不确定data哈希中的连接是否应该发生,也许所有这些都应该传递给add,但这是你的数据,所以我不能确定。

请随时在下面发表评论,我会更新此内容。


在 IRB 中尝试检查语法错误:

success = 1
# => 1
partner_id = 123
# => 123
activity_type = "something"
# => "something"
time = Time.now
# => 2013-06-05 11:17:50 0100
channel_id = 456
# => 456
country = "UK"
# => "UK"
success_string = success == 1 ?
                     'success' : 
                     "failure:#{failure_code}"
# => "success"

  data = {
      :partners_daily    => [partner_id, activity_type,success_string,time.year,time.month,time.day].join("_"),
      :partners_monthly  => [partner_id,activity_type,success_string,time.year,time.month].join("_"),
      :partners_alltime  => [partner_id,activity_type,success_string].join("_"),

      :channels_daily    => [channel_id,activity_type,success_string,time.year,time.month,time.day].join("_"),
      :channels_monthly  => [channel_id,activity_type,success_string,time.year,time.month].join("_"),
      :channels_alltime  => [channel_id,activity_type,success_string].join("_"),

      :countries_daily    => [country,activity_type,success_string,time.year,time.month,time.day].join("_"),
      :countries_monthly  => [country,activity_type,success_string,time.year,time.month].join("_"),
      :countries_alltime  => [country,activity_type,success_string].join("_")
  }
# => {:partners_daily=>"123_something_success_2013_6_5", :partners_monthly=>"123_something_success_2013_6", :partners_alltime=>"123_something_success", :channels_daily=>"456_something_success_2013_6_5", :channels_monthly=>"456_something_success_2013_6", :channels_alltime=>"456_something_success", :countries_daily=>"UK_something_success_2013_6_5", :countries_monthly=>"UK_something_success_2013_6", :countries_alltime=>"UK_something_success"}
于 2013-06-03T15:06:22.933 回答