使用命令行时如何使用 CQL 获取当前时间戳? [英] How to get current timestamp with CQL while using Command Line?
问题描述
我正在尝试从命令行插入到我的 CQL 表中.我能够插入所有内容.但我想知道我是否有时间戳列,那么如何从命令行插入时间戳列?基本上,每当我插入 CQL 表时,我都想插入当前时间戳 -
I am trying to insert into my CQL table from the command line. I am able to insert everything. But I am wondering if I have a timestamp column, then how can I insert into timestamp column from the command line? Basically, I want to insert current timestamp whenever I am inserting into my CQL table -
目前,每当我插入下面的 CQL 表时,我都会对时间戳进行硬编码 -
Currently, I am hardcoding the timestamp whenever I am inserting into my below CQL table -
CREATE TABLE TEST (ID TEXT, NAME TEXT, VALUE TEXT, LAST_MODIFIED_DATE TIMESTAMP, PRIMARY KEY (ID));
INSERT INTO TEST (ID, NAME, VALUE, LAST_MODIFIED_DATE) VALUES ('1', 'elephant', 'SOME_VALUE', 1382655211694);
有什么方法可以使用 CQL 中的一些预定义函数获取当前时间戳,以便在插入上表时,我可以使用该方法获取当前时间戳,然后插入到上表中?
Is there any way to get the current timestamp using some predefined functions in CQL so that while inserting into above table, I can use that method to get the current timestamp and then insert into above table?
推荐答案
您可以使用 timeuuid 函数 now()
和 dateof()
(或在更高版本的Cassandra,toTimestamp()
),例如,
You can use the timeuuid functions now()
and dateof()
(or in later versions of Cassandra, toTimestamp()
), e.g.,
INSERT INTO TEST (ID, NAME, VALUE, LAST_MODIFIED_DATE)
VALUES ('2', 'elephant', 'SOME_VALUE', dateof(now()));
now
函数不接受任何参数并生成一个新的唯一 timeuuid(在执行使用它的语句时).dateOf
函数接受一个 timeuuid 参数并提取嵌入的时间戳.(取自关于 timeuuid 函数的 CQL 文档).
The now
function takes no arguments and generates a new unique timeuuid (at the time where the statement using it is executed). The dateOf
function takes a timeuuid argument and extracts the embedded timestamp. (Taken from the CQL documentation on timeuuid functions).
dateof()
在 Cassandra 2.2.0-rc2 中已弃用.对于更高版本,您应该将其使用替换为 toTimestamp()
,如下所示:
dateof()
was deprecated in Cassandra 2.2.0-rc2. For later versions you should replace its use with toTimestamp()
, as follows:
INSERT INTO TEST (ID, NAME, VALUE, LAST_MODIFIED_DATE)
VALUES ('2', 'elephant', 'SOME_VALUE', toTimestamp(now()));
这篇关于使用命令行时如何使用 CQL 获取当前时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!