2013-06-03 27 views
0

我是Ruby新手。但由於一些問題,我必須處理代碼,因爲我們的ruby開發人員不可用。我們使用cassandra數據庫從Ruby(Sinatra)Web服務獲取值並將其放入Cassandra密鑰空間。但由於某些問題,數據無法插入。瞭解cassandra數據庫的Ruby腳本

在以下代碼中,partners_daily,partner_monthly等是stats密鑰空間(數據庫)中的列族(表)。

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')

並且在插入部分有任何錯誤,因爲它的失敗。

+0

有腳本來沒有錯誤,它只是價值沒有得到插入到數據庫中。但是,當我右鍵單擊列系列並選擇顯示1000行時,會彈出一個'error:Null'消息。 –

+0

是的,我想..但取消註釋它沒有任何區別。 –

回答

0

I want to know how the data inserting logic in it is being performed, and where ? Is it in stats.add(k, v, 1, 'count') ?

是的,這就是它應該發生的地方。之間的{}是字典/散列值:

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

一個循環開始與each方法,每個條目被分解並投入kv,在k鑰匙,和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方法是this one

給出的簽名是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"} 
+0

嘿iain ..非常感謝好友。這非常有幫助。你真的很好地爲我簡化了一些事情。 –

+0

我認爲'stats.add(k,v,1,'count')'會保持這樣,因爲我們的db中的密鑰空間與partners_daily,partners_monthly等等同名。 + –

+0

@RyanMalhotra很高興我可以得到一些幫助。 – iain