【问题标题】:Python Cassandra cql: INSERT timestamp and blobPython Cassandra cql:插入时间戳和 blob
【发布时间】:2014-05-10 16:33:27
【问题描述】:

我正在编写一些 python 代码,它们会随着时间的推移收集数据。我需要将它存储在 Cassandra 中。 我花了整整一天的时间,但找不到有用的东西。

CREATE TABLE timearchive 
  (name_yymmddhh text, name text, ip text, time_current timestamp, data blob, 
   PRIMARY KEY (name_yymmddhh, time_current));

我可以创建表,但在插入各种数据(time_current 时间戳、数据块)时遇到问题。我无法正确格式化。我计划按小时分行(数据大小在我的用例中应该没问题)和每个数据条目的列(2-3/分钟)。

这是我要插入的代码。如果我将时间戳/blob 的格式更改为 int/text,它将起作用。

query = """INSERT INTO timearchive
            (name_yymmddhh, name, ip, time_current, data)
            VALUES (:name_yymmddhh, :name, :ip, :time_current, :data)"""
values = {'name_yymmddhh':rowkey,
          'name': dcname,
          'ip': ip,
          'time_current': timenow,
          'data': my_blob}
cursor.execute(query, values)

问题:

1) 如何在 python 中创建 cql 时间戳:timenow?
这没有帮助(对于我的 Cassandra 级别来说太复杂了): Cassandra 1.2 inserting/updating a blob column type using Python and the cql library

2) 我的数据是一个字典。这将是一个大字典和其他数据。 (我发现了各种讨论,但没有任何效果。似乎大约 6 个月前有一些更新,但没有简单的例子:https://github.com/datastax/python-driver/pull/39
我该怎么写:

my_dict = {'one': 1, 'two': 2, 'three': 3}  
 ...  
my_blob = ???

【问题讨论】:

  • 对于您的第一个问题,在快速浏览了源代码后,您似乎无法在 Python 的客户端生成 timeuuid。但是,您可以将 :time_current 替换为直接的 CQL 函数调用 now(),这将导致在执行您的语句时在服务器上生成当前 timeuuid。
  • 这是如何通过 Python 代码完成的?
  • 只需使用VALUES (:name_yymmddhh, :name, :ip, now(), :data)"""而不是你已经拥有的VALUES (:name_yymmddhh, :name, :ip, :time_current, :data)""",并在分配values时删除设置time_current的部分。

标签: python cassandra timestamp blob cql


【解决方案1】:

解决了。 需要为 blob 使用最新的 Datastax 驱动程序,以及上面的 INSERT 方法(不是字符串转换的),以及适当的 pickle 和 bytearray。

【讨论】:

    猜你喜欢
    • 2013-05-22
    • 2014-05-15
    • 2017-03-06
    • 2018-10-06
    • 2012-10-24
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多