1
我有代码如何从python cassandra驱动程序传递cassandra函数?
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()')
尝试它给出错误的验证。
我试图覆盖列,并且从validate
函数返回now()
。
但它卡在插入记录。
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)
但是,这也给了同样的错误:(。
有没有办法从python
驾驶员通过卡桑德拉功能default
?
有没有任何方法可以使用'cassandra python'驱动程序ORM而不需要直接的'INSERT'查询? – Nilesh
@Lafada我发现了几个例子,但其中一个是代码中的客户端'datetime.now()',另一个是'uuid.uuid1()',但看起来你已经尝试过了。稍后我会搜索并查看我能找到的内容,但还有其他方法可以检查这些文档,看看您是否可以找到任何内容:https://datastax.github.io/python-driver/object_mapper.html https:// datastax .github.io/python-driver/getting_started.html – Aaron
一般来说,ORM'column'属性设置的值将在服务器端执行,在我们的例子中,我们想在服务器端执行'now()'函数。我尝试在'cassandra驱动程序'中找到相同的属性,但是,还没有运气:( – Nilesh