2013-09-24 83 views
0

我有以下的列族在卡桑德拉存儲在少數非常「寬」行的時間序列數據「IN」的關係:複合柱和卡桑德拉

CREATE TABLE data_bucket (
    day_of_year int, 
    minute_of_day int, 
    event_id int, 
    data ascii, 
    PRIMARY KEY (data_of_year, minute_of_day, event_id) 
) 

在CQL殼,我能夠運行的查詢如此:

select * from data_bucket where day_of_year = 266 and minute_of_day = 244 
    and event_id in (4, 7, 11, 1990, 3433) 

實質上,我修復複合列名(minute_of_day)的第一個分量的值和要選擇基於所述不同值的非連續的一組列的的第二個組件(event_id)。由於「IN」關係被解釋爲平等關係,所以這可以很好地工作。

現在我的問題是,我將如何以編程方式完成相同類型的組合列切片,而不使用CQL。到目前爲止,我已經嘗試過Python客戶端pycassa和Java客戶端Astyanax,但沒有任何成功。

任何想法都會受到歡迎。

編輯:

我加入柱族的描述輸出通過卡桑德拉-CLI看到。由於我正在尋找基於Thrift的解決方案,所以這可能會有所幫助。

ColumnFamily: data_bucket 
    Key Validation Class: org.apache.cassandra.db.marshal.Int32Type 
    Default column value validator: org.apache.cassandra.db.marshal.AsciiType 
    Cells sorted by: org.apache.cassandra.db.marshal.CompositeType(org.apache.cassandra.db.marshal.Int32Type,org.apache.cassandra.db.marshal.Int32Type) 
    GC grace seconds: 864000 
    Compaction min/max thresholds: 4/32 
    Read repair chance: 0.1 
    DC Local Read repair chance: 0.0 
    Populate IO Cache on flush: false 
    Replicate on write: true 
    Caching: KEYS_ONLY 
    Bloom Filter FP chance: default 
    Built indexes: [] 
    Compaction Strategy: org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy 
    Compression Options: 
    sstable_compression: org.apache.cassandra.io.compress.SnappyCompressor 
+0

你確定這個查詢在卡桑德拉cqlsh作品?在卡桑德拉IN操作只支持分區鍵,在你的情況event_id僅僅是聚類列。 – abhi

+0

@abhi它當然可以工作。我的理解是,只要所有先前的組件指定了嚴格的平等關係,組合列的最後一個組件就可以指定一個範圍/一列的列。看看這裏:http://www.datastax.com/dev/blog/introduction-to-composite-columns-part-1 – Nikhil

+1

@abhi在這裏:http://cassandra.apache.org/doc/cql3/ CQL.html#selectStmt – Nikhil

回答

1

Thrift API中沒有「IN」類型的查詢。您可以爲每個組合列值執行一系列get查詢(day_of_yearminute_of_day,event_id)。

如果您event_id小號是連續的(和你的問題說,他們是不是),你可以執行單一get_slice查詢,傳遞範圍(例如,day_of_yearminute_of_day和範圍的event_id S)。您可以用這種方式抓取它們,並以編程方式自己過濾響應(例如,使用4-3433之間的事件ID獲取日期的所有數據)。更多的數據傳輸,更多的客戶端處理,所以不是一個很好的選擇,除非你真的在尋找一個範圍。

因此,如果您想在Cassandra中使用「IN」,您需要切換到基於CQL的解決方案。如果您正在考慮在python中使用CQL,另一個選項是cassandra-dbapi2。這爲我工作:

import cql 

# Replace settings as appropriate 
host = 'localhost' 
port = 9160 
keyspace = 'keyspace_name' 

# Connect 
connection = cql.connect(host, port, keyspace, cql_version='3.0.1') 
cursor = connection.cursor() 
print "connected!" 

# Execute CQL 
cursor.execute("select * from data_bucket where day_of_year = 266 and minute_of_day = 244 and event_id in (4, 7, 11, 1990, 3433)") 
for row in cursor: 
    print str(row) # Do something with your data 

# Shut the connection 
cursor.close() 
connection.close() 

(測試用卡珊德拉2.0.1。)

+0

它在CQL中有效,但正如我在我的問題中提到的,我正在尋找一個沒有它的解決方案。 – Nikhil

+1

如果您不想使用CQL,您是否在尋找基於Thrift或CLI的替代方案? [卡桑德拉自己的建議是使用基於CQL的客戶端](http://wiki.apache.org/cassandra/ClientOptions)。建議的解決方案將允許您構建任何基於CQL的查詢(傳遞給'cursor.execute'的字符串),並使用cassandra-dbapi2客戶端以編程方式執行它。 – lorcan

+0

我必須與使用Astyanax而不使用CQL的現有代碼集成。儘管我可能會推動CQL,但我很想知道如何通過Thrift在封面下工作。 – Nikhil