本文介绍了使用cql从python插入cassandra的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我计划在CF中插入具有复合键的数据。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...);但是我不能使用cql从python插入数据到这个CF。 (http://code.google.com/a/apache-extras.org/p/cassandra-dbapi2/)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 cqlconnection = 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获得以下跟踪回调: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'我做错了什么?推荐答案看起来您正试图遵循以下示例: http://pypi.python.org/pypi/cql/1.4.0 It looks like you are trying to follow the example in:http://pypi.python.org/pypi/cql/1.4.0import cqlcon = 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.此外,由于您使用的是复合键,请确保使用CQL3 http://www.datastax.com/dev/blog/whats-new-in -cql-3-0 Also, since you are using composite keys, make sure you use CQL3http://www.datastax.com/dev/blog/whats-new-in-cql-3-0connection = cql.connect('localhost:9160', cql_version='3.0.0')以下代码应该可以正常工作p> The following code should work (just adapt it to localhost if needed):import cqlcon = 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) 这篇关于使用cql从python插入cassandra的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-23 07:56