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

时间:2019-11-06 11:09:04

标签: cassandra cql cqlsh

我已经创建了表

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

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

我收到此错误

  

SyntaxException:行1:23输入'2018'处没有可行的选择   (插入testdates2([2018] ...)

1 个答案:

答案 0 :(得分:1)

格式为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点。

相关问题