Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert to cassandra from python using cql

I'm planning to insert data to bellow CF that has compound keys.

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

But I can't insert data to this CF from python using cql. (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({}) )

I get the following 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'

What am I doing wrong?

like image 704
tuelabel Avatar asked Nov 04 '12 09:11

tuelabel


People also ask

How do you use CQL in Python?

To create a table, use session object to execute CQL query for creating a table. The keyspace so created can be further used to insert rows. The CQL version of INSERT query is similar to SQL Insert statement. Following code inserts a row in students table.

What is CQL in Cassandra?

Query languages are computer languages used to make queries in databases and information systems. Cassandra Query Language is the primary query language for communicating with the Apache Cassandra database.

How do I create a Cassandra table in Python?

Cassandra query language create table command. CREATE TABLE command in Cassandra CQL allows adding a new table in a keyspace. To create a table inside the keyspace using CQL use table-name, Column-names, column-datatypes, and one column from a table with the primary key.


2 Answers

It looks like you are trying to follow the example in: 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...'))

However, if you only need to insert one row (like in your question), just drop the empty dict() parameter.

Also, since you are using composite keys, make sure you use CQL3 http://www.datastax.com/dev/blog/whats-new-in-cql-3-0

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

The following code should work (just adapt it to localhost if needed):

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)
like image 173
Oren Avatar answered Oct 18 '22 22:10

Oren


For python 2.7, 3.3, 3.4, 3.5, and 3.6 for installation you can use

     $ pip install cassandra-driver

And in python:

     import cassandra

Documentation can be found under https://datastax.github.io/python-driver/getting_started.html#passing-parameters-to-cql-queries

like image 21
B.abba Avatar answered Oct 19 '22 00:10

B.abba