【发布时间】:2020-07-26 03:02:36
【问题描述】:
我一直试图在 Cassandra 中将时间戳值存储到毫秒,但我只能通过 DataStax 驱动程序获取到 LocalDate,毫秒部分被它省略了。
这是我的课
package com.example.connectCass.Model;
import lombok.Data;
import org.springframework.data.cassandra.core.cql.PrimaryKeyType;
import org.springframework.data.cassandra.core.mapping.Column;
import org.springframework.data.cassandra.core.mapping.PrimaryKeyColumn;
import org.springframework.data.cassandra.core.mapping.Table;
import com.datastax.driver.core.LocalDate;
import java.util.Date;
@Data
@Table
public class DemoModel {
@PrimaryKeyColumn(name = "acctID", type = PrimaryKeyType.PARTITIONED)
private String acctID;
@PrimaryKeyColumn(name = "transactionDate", type = PrimaryKeyType.PARTITIONED)
private LocalDate transactionDate;
@PrimaryKeyColumn(name = "currentTime", type = PrimaryKeyType.CLUSTERED)
private LocalDate currentTime;
@Column
private String card;
@Column
private String amountCreditedFrom;
@Column
private double transactionAmount;
@Column
private double balance;
public void setTransactionDate(){
this.transactionDate = LocalDate.fromMillisSinceEpoch(new Date().getTime());
}
public void setCurrentTime(){
this.currentTime = LocalDate.fromMillisSinceEpoch(new Date().getTime());
}
}
在setCurrentTime 方法中,我想要时间到毫秒,但无法做到,只能到今天。直接试了java.util.Date,还是不行。
为什么 Cassandra 中没有毫秒?
【问题讨论】:
-
你说的时间到毫秒和到今天的日期是什么意思?请改写。
-
这意味着,toTimestamp(now()) 给出的值是 Cassandra 中的当前毫秒数,我想在 Java 中拥有相同的值并存储在 Cassandra 中,直到今天的日期意味着时间精度是直到今天日期不到毫秒。
-
我还是不明白你想表达什么。
new Date().getTime()在几秒前返回1586787833411,这是自纪元以来的毫秒数。您想要或期待什么价值? -
在 Java 中很好,我得到了价值,问题是将该值存储在 Cassandra 中,用 cassandra 时间戳数据类型存储在 mili 秒
-
在 Cassandra 中存储时,我只能以天精度存储 java 毫秒,小时、秒、毫秒已经消失,类似于 1586787800000,希望你现在得到它