DS201:如何在使用 Python 驱动程序插入 cassandra 时获取 "type timeuuid" 的值?

DS201: How to get value of "type timeuuid" while inserting into cassandra using Python Driver?

这是我第一次在论坛上提问,如果我的理解有误,请指正。

我正在从 DS201 执行应用程序驱动程序连接练习。

Table如下:

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)"
>>>

我尝试了以下但失败了:

  1. UUIDs.timeBased()

错误:

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

错误:

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')")

数据库成功:

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

但我想知道这个?

您是正确的,因为调用 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');