【问题标题】:DS201: How to get value of "type timeuuid" while inserting into cassandra using Python Driver?DS201:如何在使用 Python 驱动程序插入 cassandra 时获取“type timeuuid”的值?
【发布时间】:2020-10-11 16:00:03
【问题描述】:

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

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

但我想知道这个?

【问题讨论】:

    标签: cassandra datastax cql cassandra-python-driver


    【解决方案1】:

    您是正确的,对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');
    

    【讨论】:

      猜你喜欢
      • 2016-11-17
      • 2013-10-10
      • 2014-03-27
      • 2017-11-05
      • 2016-06-28
      • 2023-04-07
      • 2014-05-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多