2012-11-04 13 views
13

Mam zamiar wstawić dane do poniżej CF, który ma złożone klucze.Wstaw do kassandra z python za pomocą cql

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

Ale nie mogę wstawić dane do tej CF z pytona użyciu CQI. (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({})) 

otrzymuję następujący traceback:

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' 

Czym jestem robić źle?

Odpowiedz

15

Wygląda na to, że starają się naśladować przykład w: 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...')) 

Jednakże, jeśli tylko trzeba wstawić jeden wiersz (jak w pytaniu), po prostu upuścić pusty dict() parametr .

Ponadto, ponieważ używasz klawiszy kompozytowe, upewnij się, że używasz CQL3 http://www.datastax.com/dev/blog/whats-new-in-cql-3-0

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

Poniższy kod powinien działać (tylko dostosować go do localhost w razie potrzeby):

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

Thanks Oren . Próbowałem edytować kod i wykonać, więc zadziałało! – tuelabel

+0

Czy istnieje sposób, aby zbiorczo przesłać dane za pomocą tego samego? –

Powiązane problemy