2012-11-29 35 views
4

這是我列族的定義:蟒蛇CQL查詢取代 「SELECT * FROM CF凡在列(.. .. ..)」

CREATE TABLE columnfamily (column1 int PRIMARY KEY, column2 text); 

我使用python卡桑德拉 - dbapi2 1.4。 0使用CQL 3語法連接到Cassandra 1.1.6。我想在下面的查詢使用查詢替換,但似乎沒有工作WHERE ... IN(...,...):

cql = "SELECT * FROM columnfamily WHERE column1 IN (:x) and column2 = :y;" 
q = cursor.prepare_query(cql) 

cursor.execute_prepared(q, {'x':(1,2,3), 'y':'abc'} 

我在嘗試上述後得到了以下錯誤:

Traceback (most recent call last): 
    File "test_cassandra.py", line 17, in <module> 
    cursor.execute_prepared(q, params) 
    File "/usr/local/lib/python2.7/dist-packages/cql/cursor.py", line 89, in execute_prepared 
    response = self.get_response_prepared(prepared_query, params, cl) 
    File "/usr/local/lib/python2.7/dist-packages/cql/thrifteries.py", line 83, in get_response_prepared 
    paramvals = prepared_query.encode_params(params) 
    File "/usr/local/lib/python2.7/dist-packages/cql/query.py", line 60, in encode_params 
    return [t.to_binary(t.validate(params[n])) for (n, t) in zip(self.paramnames, self.vartypes)] 
    File "/usr/local/lib/python2.7/dist-packages/cql/cqltypes.py", line 225, in to_binary 
    return cls.serialize(val) 
struct.error: cannot convert argument to integer 

我也曾嘗試使用以下命令:

cursor.execute_prepared(q, {'x':','.join([str(i) for i in (1,2,3)]), 'y':'abc'} 

同樣的錯誤發生。有誰知道在WHERE ... IN(...,...)語句中進行查詢替換的正確方法是什麼?

謝謝!

回答

3

Cassandra是在預先準備好的查詢中插入邊界值的塊,在這裏,它不支持綁定一系列值,所以答案非常多,「你不能這樣做(但)」。

請參閱https://issues.apache.org/jira/browse/CASSANDRA-4210如果您想要關注那裏的情況。

可以支持這種在平原​​呼叫,其中此查詢插在Python端所做的蟒蛇,CQL驅動結構的,但你不會得到任何的好處準備的查詢,並且它不會超過僅僅是

cursor.execute('SELECT * FROM a WHERE b IN (%s)' 
       % ', '.join(map(cql.query.cql_quote, your_list_of_values)))