1

这是我在论坛上的第一个问题,如果我的理解有误,请纠正。

我正在从 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)"
>>>

我在下面尝试但失败了:

  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

但我想知道这个?

4

1 回答 1

1

你是对的,对uuid()函数的调用是你的问题:

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

uuid()函数生成一个 type-4 UUID,而该video_id列被定义为一个 type-1 UUID(又名 a TIMEUUID)。

相反,调用该now()函数来获取TIMEUUID

INSERT INTO videos_by_tag (tag,added_date,video_id,title)
VALUES ('cassandra', '2013-01-10', now(), 'Cassandra Is My Friend');
于 2020-06-22T04:12:20.147 回答