这是我的列族定义:
CREATE TABLE columnfamily (column1 int PRIMARY KEY, column2 text);
我正在使用python cassandra-dbapi2 1.4.0连接到Cassandra 1.1.6,使用CQL 3语法。我想在下面的查询中使用查询替换,但它似乎不适用于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(...,...)语句中进行查询替换的正确方法是什么?
谢谢!
答案 0 :(得分:3)
Cassandra是准备好的查询中的插值边界值,在这里,它不支持绑定一系列值,所以答案很简单,“你不能那样做(还)”。
如果你想留意那里的情况,请参见https://issues.apache.org/jira/browse/CASSANDRA-4210。
可以在普通execute()
调用中的python-cql驱动程序中支持这种结构,其中查询插值是在Python端完成的,但你不会获得准备好的查询的任何好处,并不仅仅是
cursor.execute('SELECT * FROM a WHERE b IN (%s)'
% ', '.join(map(cql.query.cql_quote, your_list_of_values)))