ds201:如何在使用python驱动程序插入cassandra时获取“typetimeuuid”的值?

2w3rbyxf  于 2021-06-13  发布在  Cassandra
关注(0)|答案(1)|浏览(366)

这是我在论坛上的第一个问题,如果我的理解有误,请更正。
我正在从ds201执行应用程序驱动程序连接。
下表如下:

cqlsh:killrvideo> SELECT * FROM videos_by_tag ;

       tag | added_date                      | video_id                             | title
-----------+---------------------------------+--------------------------------------+------------------------------  
  datastax | 2013-10-16 00:00:00.000000+0000 | 4845ed97-14bd-11e5-8a40-8338255b7e33 | DataStax Studio

现在我想做一个任务“python代码插入一个新的视频到数据库”根据实验室。
我尝试了此代码并得到错误:

>>> session.execute(
... "INSERT INTO videos_by_tag (tag, added_date, video_id, title)" +
... "VALUES ('cassandra', '2013-01-10', uuid(), 'Cassandra Is My Friend')")

Traceback (most recent call last):
File "<stdin>", line 3, in <module>
File "cassandra/cluster.py", line 2618, in cassandra.cluster.Session.execute
File "cassandra/cluster.py", line 4877, in cassandra.cluster.ResponseFuture.result
cassandra.InvalidRequest: Error from server: code=2200 [Invalid query] message="Type error: cannot assign result of function system.uuid (type uuid) to video_id (type timeuuid)"
>>>

我在下面试过,但失败了:
uuids.timebased()
错误:

cassandra.InvalidRequest: Error from server: code=2200 [Invalid query] message="Unknown function uuids.timebased called"

cassandra.util.uuid来自\u时间
错误:

cassandra.protocol.SyntaxException: <Error from server: code=2000 [Syntax error in CQL query] message="line 1:109 no viable alternative at input '.' (...)VALUES ('cassandra', '2013-01-10', [cassandra].util...)">

目前,我已经硬编码的价值。

session.execute(
... "INSERT INTO videos_by_tag (tag, added_date, video_id, title)" +
... "VALUES ('cassandra', '2013-01-10', 245e8024-14bd-11e5-9743-8238357b7e32, 'Cassandra Is My Friend')")

db成功:

cassandra | 2013-01-10 00:00:00.000000+0000 | 245e8024-14bd-11e5-9743-8238357b7e32 | Cassandra Is My Friend

但我想知道这个?

oyt4ldly

oyt4ldly1#

你说的对 uuid() 功能是你的问题:

INSERT INTO videos_by_tag (tag,added_date,video_id,title)
VALUES ('cassandra', '2013-01-10', uuid(), 'Cassandra Is My Friend');

这个 uuid() 函数生成类型4的uuid,而 video_id 列定义为类型1 uuid(也称为 TIMEUUID ).
相反,调用 now() 函数来获取 TIMEUUID :

INSERT INTO videos_by_tag (tag,added_date,video_id,title)
VALUES ('cassandra', '2013-01-10', now(), 'Cassandra Is My Friend');

相关问题