如何在 cassandra 查询语言中使用 DATE 数据类型编写查询?

How to write a query using DATE data type in cassandra query language?

我创建了 table

create table testdates2(dates date primary key,ate text);

insert into testdates2(2018-04,'nov');

我收到这个错误

SyntaxException: line 1:23 no viable alternative at input '2018' (insert into testdates2([2018]...)

格式是yyyy-mm-dd,这意味着你也必须总是玩一天。这是一个使用您的模式的示例,该模式将为 2018 年 4 月 1 日插入一行。我为 ate 列选择了一个随机值。

cqlsh> insert into testdates2 (dates, ate) VALUES ('2018-04-01', 'Soup');
cqlsh> select * from testdates2;

 dates      | ate
------------+------
 2018-04-01 | Soup

(1 rows)

您可以在 DataStax's documentation 中找到有关在 Cassandra 中使用日期值的更多详细文档。

根据关于时间戳与日期的评论编辑

timestamp 具有更高的分辨率,它可以指向事件发生的确切秒数,而 date 可以指向事件发生的日期。

比方说我想知道那汤到底是第几秒吃的,我可以使用时间戳类型而不是日期类型。

ALTER TABLE testdates2 ADD time_eaten timestamp;
insert into testdates2 (dates, ate, time_eaten) VALUES ('2018-04-01', 'Soup', 1522576800);

1522576800 和 2019-04-01 都代表 2018 年 4 月 1 日,但时间戳还指定事件发生在上午 10 点。