2012-11-04 81 views
13

我打算將數據插入到具有複合鍵的波紋管CF中。使用cql從python插入cassandra

CREATE TABLE event_attend (
    event_id int, 
    event_type varchar, 
    event_user_id int, 
    PRIMARY KEY (event_id, event_type) #compound keys... 
); 

但我不能從python使用cql插入數據到這個CF。 (http://code.google.com/a/apache-extras.org/p/cassandra-dbapi2/)

import cql 
connection = cql.connect(host, port, keyspace) 
cursor = connection.cursor() 
cursor.execute("INSERT INTO event_attend (event_id, event_type, event_user_id) VALUES (1, 'test', 2)", dict({})) 

我得到以下回溯:

Traceback (most recent call last): 
File "./v2_initial.py", line 153, in <module> 
    db2cass.execute() 
File "./v2_initial.py", line 134, in execute 
    cscursor.execute("insert into event_attend (event_id, event_type, event_user_id) values (1, 'test', 2)", dict({})) 
File "/usr/local/pythonbrew/pythons/Python-2.7.2/lib/python2.7/site-packages/cql-1.4.0-py2.7.egg/cql/cursor.py", line 80, in execute 
    response = self.get_response(prepared_q, cl) 
File "/usr/local/pythonbrew/pythons/Python-2.7.2/lib/python2.7/site-packages/cql-1.4.0-py2.7.egg/cql/thrifteries.py", line 80, in get_response 
    return self.handle_cql_execution_errors(doquery, compressed_q, compress) 
File "/usr/local/pythonbrew/pythons/Python-2.7.2/lib/python2.7/site-packages/cql-1.4.0-py2.7.egg/cql/thrifteries.py", line 98, in handle_cql_execution_errors 
    raise cql.ProgrammingError("Bad Request: %s" % ire.why) 
cql.apivalues.ProgrammingError: Bad Request: unable to make int from 'event_user_id' 

我是什麼做錯了?

回答

15

它看起來像你試圖遵循例如: http://pypi.python.org/pypi/cql/1.4.0

import cql 
con = cql.connect(host, port, keyspace) 
cursor = con.cursor() 
cursor.execute("CQL QUERY", dict(kw='Foo', kw2='Bar', kwn='etc...')) 

但是,如果你只需要(在你的問題等)插入一個一行,只是下降的空字典()參數。

而且,由於使用的是組合鍵,請確保您使用CQL3 http://www.datastax.com/dev/blog/whats-new-in-cql-3-0

connection = cql.connect('localhost:9160', cql_version='3.0.0') 

下面的代碼應該工作(只是適應它到本地主機如果需要的話):

import cql 
con = cql.connect('172.24.24.24', 9160, keyspace, cql_version='3.0.0') 
print ("Connected!") 
cursor = con.cursor() 
CQLString = "INSERT INTO event_attend (event_id, event_type, event_user_id) VALUES (131, 'Party', 3156);" 
cursor.execute(CQLString) 
+0

感謝奧倫。 我試圖編輯我的代碼並執行,所以它工作! – tuelabel

+0

有沒有辦法批量上傳數據? –