Python 如何将日期时间插入到 Cassandra 1.2 时间戳列中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16532566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:56:18  来源:igfitidea点击:

How to insert a datetime into a Cassandra 1.2 timestamp column

pythoncassandracql

提问by Sergio Ayestarán

IMPORTANTIf you are dealing with this problem today, use the new cassandra-driver from datastax (i.e. import cassandra) since it solves most of this common problems and don't use the old cql driver anymore, it is obsolete! This question is old from before the new driver was even in development and we had to use an incomplete old library called cql (import cql <-- don't use this anymore, move to the new driver).

重要如果您今天正在处理这个问题,请使用来自 datastax 的新 cassandra 驱动程序(即导入 cassandra),因为它解决了大多数常见问题并且不再使用旧的 cql 驱动程序,它已经过时了!这个问题在新驱动程序开发之前就已经很老了,我们不得不使用一个名为 cql 的不完整的旧库(import cql <--不要再使用这个,转移到新驱动程序)。

IntroI'm using the python library cql to access a Cassandra 1.2 database. In the database I have a table with a timestamp column and in my Python code I have a datetime to be inserted in the column. Example as follows:

介绍我正在使用 python 库 cql 来访问 Cassandra 1.2 数据库。在数据库中,我有一个带有时间戳列的表,在我的 Python 代码中,我有一个要插入到该列中的日期时间。示例如下:

Table

桌子

CREATE TABLE test (
     id text PRIMARY KEY,
     last_sent timestamp
);

The code

编码

import cql
import datetime
...
cql_statement = "update test set last_sent = :last_sent where id =:id"
rename_dict = {}
rename_dict['id'] = 'someid'
rename_dict['last_sent'] = datetime.datetime.now()
cursor.execute (cql_statement, rename_dict)

The problem

问题

When I execute the code the actual cql statement executed is like this:

当我执行代码时,实际执行的 cql 语句是这样的:

update test set last_sent =2013-05-13 15:12:51 where id = 'someid'

Then it fails with an error

然后它失败并出现错误

 Bad Request: line 1:XX missing EOF at '-05'

The problem seems to be that the cql library is not escaping ('') or converting the datetime before running the query.

问题似乎是 cql 库在运行查询之前没有转义 ('') 或转换日期时间。

The questionWhat is the correct way of doing this without manually escaping the date and be able to store a full timestamp with more precision into a cassandra timestamp column?

问题在不手动转义日期的情况下执行此操作的正确方法是什么,并且能够更精确地将完整时间戳存储到 cassandra 时间戳列中?

Thanks in advance!

提前致谢!

采纳答案by Sergio Ayestarán

Has abhi already stated this can be done using the milliseconds since epoch as a long value from cqlsh, now we need to make it work in the Python code.

abhi 已经说过这可以使用自 epoch 以来的毫秒数作为 cqlsh 的 long 值来完成,现在我们需要让它在 Python 代码中工作。

When using the cql library this conversion (from datetime to milliseconds since epoch) is not happening so in order to make the update work and still have the precision you need to convert the datetime to milliseconds since epoch.

当使用 cql 库时,这种转换(从日期时间到纪元以来的毫秒数)不会发生,因此为了使更新工作并且仍然具有将日期时间转换为纪元以来的毫秒数所需的精度。

SourceUsing this useful question: Getting millis since epoch from datetime, in particular this functions(note the little change I made):

来源使用这个有用的问题:从 datetime 开始获取毫秒,特别是这个函数(注意我所做的小改动):

The solution

解决方案

import datetime

def unix_time(dt):
    epoch = datetime.datetime.utcfromtimestamp(0)
    delta = dt - epoch
    return delta.total_seconds()

def unix_time_millis(dt):
    return long(unix_time(dt) * 1000.0)

For this example the code would be:

对于此示例,代码将是:

cql_statement = "update test set last_sent = :last_sent where id =:id"
rename_dict = {}
rename_dict['id'] = 'someid'
rename_dict['last_sent'] = unix_time_millis(datetime.datetime.now())
cursor.execute (cql_statement, rename_dict)

You can convert the datetime to a long value containing the number of milliseconds since epoch and that's all, the update is transformed to an equivalent form using a long value for the timestamp.

您可以将日期时间转换为包含自纪元以来的毫秒数的 long 值,仅此而已,使用时间戳的 long 值将更新转换为等效形式。

Hope it helps somebody else

希望它可以帮助别人

回答by abhi

I can tell you how to do it in cqlsh. Try this

我可以告诉你如何在 cqlsh 中做到这一点。尝试这个

update test set last_sent =1368438171000 where id = 'someid'

Equivalent long value for date time 2013-05-13 15:12:51is 1368438171000

日期时间长等效值2013-05-13 15:12:511368438171000

回答by webjunkie

Well for me it works directly with

对我来说,它直接与

update test set last_sent = '2013-05-13 15:12:51' where id = 'someid'

No need to convert something. So in Python you can do it using the datetime value as a string:

无需转换某些东西。因此,在 Python 中,您可以使用日期时间值作为字符串来执行此操作:

cursor.execute("UPDATE test SET ts=:ts WHERE id=:id;",
    dict(ts=your_datetime.isoformat(), id=id))

回答by Patrick

Most of the solutions are valid, I just want to suggest a simpler one:

大多数解决方案都是有效的,我只想提出一个更简单的解决方案:

from datetime import datetime

my_date = int(float(datetime.now().strftime("%s.%f"))) * 1000

update test set last_sent = my_date where id = 'someid'

回答by zonked.zonda

I know it is a 2 year old question, but if anyone comes looking for an answer, use a datetime instance instead of using timestamp. Python driver should smartly handle an integer/float, though.

我知道这是一个 2 年前的问题,但是如果有人来寻找答案,请使用 datetime 实例而不是使用时间戳。不过,Python 驱动程序应该巧妙地处理整数/浮点数。