java 处理 Cassandra 时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36186093/
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-11-03 01:02:24  来源:igfitidea点击:

Dealing with Cassandra Timestamp

javacassandratimestampcqldatastax

提问by Shabarinath Volam

Recently I have started working on cassandra and I have some issues dealing with cassandra timestamp using cql and Java.

最近我开始研究 cassandra,但我在使用 cql 和 Java 处理 cassandra 时间戳时遇到了一些问题。

Below is my sample cassandra table schema.

下面是我的示例 cassandra 表架构。

CREATE TABLE emp (
    empid int,
    create_date timestamp,
    deptid int,
    PRIMARY KEY (empid, create_date)
)

Here are my questions below:

以下是我的问题:

1)Actually I need only date(I am not worried about time), In my table schema I have used timestamp datatype, Is there any datatype like date to store only date instead timestamp.

1)实际上我只需要日期(我不担心时间),在我的表模式中我使用了时间戳数据类型,是否有像日期这样的数据类型只存储日期而不是时间戳。

2)If there is no alternative to timestamp datatype below is how I am inserting record into table manually

2)如果下面的时间戳数据类型没有替代方案,我将如何手动将记录插入表中

Insert into emp(empId, deptId, create_date) values(1,2,'2016-03-15 00:00:00+0000');

When I am trying to query to retrieve record using cql as below

当我尝试使用 cql 查询检索记录时,如下所示

select * from emp where create_date='2016-03-15' and empid=1;

It's not returning any record so I have use below query

它没有返回任何记录,所以我使用了以下查询

 select * from emp where create_date='2016-03-15 00:00:00+0000' and empid=1;

above query returned record, So how to query so that I need to get records only with date.

上面的查询返回了记录,那么如何查询以便我只需要获取带有日期的记录。

3)I am using datastax 2.1 JAVA API to communicate to cassandra, Below is my code how I am retrieving timestamp column from Java.

3)我正在使用 datastax 2.1 JAVA API 与 cassandra 通信,下面是我如何从 Java 检索时间戳列的代码。

row.getDate("create_date")

getDate method is returning java.util.date (later versions returning com.datastax.driver.core.LocalDate)

getDate 方法返回 java.util.date (更高版本返回com.datastax.driver.core.LocalDate

When I try to get date from code its returning one day less suppose if record in my table is 2016-03-15 00:00:00+0000, but from my code it shows something like 2016-03-14 EDT(one day less is this because of timezone issue), If its timezone issue should I set timezone in my Java code while retrieving

当我尝试从代码中获取日期时,它会少返回一天,假设我的表中的记录是2016-03-15 00:00:00+0000,但从我的代码中它显示了类似2016-03-14 EDT(一天较少是因为时区问题),如果是时区问题,我应该在检索时在 Java 代码中设置时区

4)If I want to query and get record from my Java code based on empId and create_date how to make exact pattern like 2016-03-15 00:00:00+0000in Java code, As it is not retrieving if I provide only date in where condition or is there any better way in retrieving using Java code.

4)如果我想根据 empId 和 create_date 从我的 Java 代码中查询和获取记录,如何像2016-03-15 00:00:00+0000在 Java 代码中那样制作精确的模式,因为如果我只在 where 条件下提供日期,它就不会检索,或者有没有更好的检索方法使用 Java 代码。

采纳答案by Stefan Podkowinski

Getting back to your example:

回到你的例子:

Insert into emp(empId, deptId, create_date) values(1,2,'2016-03-15 00:00:00+0000');

插入 emp(empId, deptId, create_date) values(1,2,'2016-03-15 00:00:00+0000');

The statement above will create a timestamp value for 2016-03-15 00:00 UTC.

上面的语句将为 2016-03-15 00:00 UTC 创建一个时间戳值。

select * from emp where create_date='2016-03-15' and empid=1;

select * from emp where create_date='2016-03-15' and empid=1;

The select will match against a timestamp created from the given date in your local timezone. What you have to do here is to query specifically by UTC, using create_date='2016-03-15Z'.

选择将与从本地时区中的给定日期创建的时间戳匹配。您在这里要做的是使用 UTC 专门查询create_date='2016-03-15Z'

As you're also having timezone related issues in Java the easiest option would be to simply use a string representation of the date.

由于您在 Java 中也遇到与时区相关的问题,因此最简单的选择是简单地使用日期的字符串表示形式。