如何从python cassandra驱动程序传递cassandra函数?

时间:2017-01-16 19:46:31

标签: python orm cassandra default cassandra-python-driver

我有代码

from uuid import uuid4
from uuid import uuid1

from cassandra.cqlengine import columns, connection
from cassandra.cqlengine.models import Model
from cassandra.cqlengine.management import sync_table


class BaseModel(Model):
    __abstract__ = True

    id = columns.UUID(primary_key=True, default=uuid4)
    created_timestamp = columns.TimeUUID(primary_key=True,
                                         clustering_order='DESC',
                                         default=uuid1)
    deleted = columns.Boolean(required=True, default=False)

class OtherModel(BaseModel):
    __table_name__ = 'other_table'
    name = columns.Text(required=True, default='')



if __name__ == '__main__':
    connection.setup(hosts=['localhost'],
                     default_keyspace='test')
    sync_table(OtherModel)

now()中有cassandra个功能,可让current time创建记录。

我尝试使用INSERT查询创建记录。

cqlsh> INSERT INTO test.other_table ("id", "created_timestamp", "deleted", "name") VALUES (3c156369-6d71-40e2-933f-b48fdda7681f, now(), false, 'test');

但是当我尝试

created_timestamp = columns.TimeUUID(primary_key=True,
                                     clustering_order='DESC',
                                     default='now()')

验证时出错。

我尝试覆盖TimeUUID列,并从now()函数返回validate

但它插入了插入记录。

2017-01-16 12:20:06 [DEBUG] cassandra.cqlengine.connection: INSERT INTO  test.other_table ("deleted", "id", "created_timestamp", "name") VALUES (%(0)s, %(1)s, %(2)s, %(3)s)
...
...
    result = session.execute(query, params, timeout=timeout)
  File "cassandra/cluster.py", line 1710, in cassandra.cluster.Session.execute (cassandra/cluster.c:30976)
    return self.execute_async(query, parameters, trace, custom_payload, timeout).result()
  File "cassandra/cluster.py", line 3343, in cassandra.cluster.ResponseFuture.result (cassandra/cluster.c:68510)
    raise self._final_exception
InvalidRequest: Error from server: code=2200 [Invalid query] message="Invalid STRING constant (now()) for "created_timestamp" of type timeuuid"

我尝试使用直接插入查询。

from cassandra.cqlengine import connection

if __name__ == '__main__':

    connection.setup(hosts=['localhost'],
                     default_keyspace='test')

    session = connection.get_session()

    insert_query = """INSERT INTO test.other_table ("id", "created_timestamp", "deleted", "name") VALUES (%(0)s, %(1)s, %(2)s, %(3)s)"""

    params = {'0': '3c156369-6d71-40e2-933f-b48fdda7681f', '2': False, '3': 'name', '1': 'now()'}

    session.execute(insert_query, params)

但这也给出了同样的错误:(。

有没有办法从default驱动程序中传递python中的cassandra函数?

1 个答案:

答案 0 :(得分:1)

它不喜欢将now()作为参数传递。通过从now()中删除params并将其添加到VALUES子句中的查询字符串,我能够让您的(较低)代码正常工作:

insert_query = """INSERT INTO other_table 
    ("id", "created_timestamp", "deleted", "name") 
    VALUES (%(0)s, now(), %(1)s, %(2)s)"""

session.execute(insert_query, params)

...

aploetz@cqlsh:stackoverflow> SELECT * FROm other_table ;

 id                                   | created_timestamp                    | deleted | name
--------------------------------------+--------------------------------------+---------+------
 3c156369-6d71-40e2-933f-b48fdda7681f | da509470-dcc9-11e6-b047-2ff6499dee60 |   False | name

(1 rows)