如何使用 Spring Data Cassandra 将 java.util.Date 值插入到 Cassandra 日期类型列中? [英] How to insert java.util.Date values into Cassandra date type column using Spring Data Cassandra?
问题描述
我有一个带有日期类型列的 cassandra 表,如下所示:
I have cassandra table with a date type column as follows:
create table people
(
id int primary key,
name text,
email text,
dob date
);
我使用的是 SpringBoot 1.5.2 + Spring Data Cassandra Starter.
I am using SpringBoot 1.5.2 + Spring Data Cassandra Starter.
@Table("people")
public class Person {
@PrimaryKey
Integer id;
private String name;
private String email;
private java.util.Date dob;
//setters and getters
}
public interface PersonRepository extends CrudRepository<Person, Integer>{
}
我按如下方式插入新人:
I am inserting new Person as follows:
personRepository.save(new Person(1, "Siva","siva@gmail.com", new java.util.Date()));
它抛出以下错误:
Caused by: com.datastax.driver.core.exceptions.InvalidQueryException: Expected 4 byte long for date (8)
at com.datastax.driver.core.Responses$Error.asException(Responses.java:136) ~[cassandra-driver-core-3.1.4.jar:na]
at com.datastax.driver.core.DefaultResultSetFuture.onSet(DefaultResultSetFuture.java:179) ~[cassandra-driver-core-3.1.4.jar:na]
at com.datastax.driver.core.RequestHandler.setFinalResult(RequestHandler.java:177) ~[cassandra-driver-core-3.1.4.jar:na]
at com.datastax.driver.core.RequestHandler.access$2500(RequestHandler.java:46) ~[cassandra-driver-core-3.1.4.jar:na]
但是如果我将 dob 列类型设为 timestamp 那么它就可以正常工作.是否可以有 date 类型的列并使用 java.util.Date 类型的属性?
But if I make dob column type to timestamp then it is working fine. Is it possible to have date type column and use java.util.Date type properties?
P.s:即使我使用 java.sql.Date 我也遇到同样的错误.
P.s: Even if I use java.sql.Date I am getting the same error.
推荐答案
使用com.datastax.driver.core.LocalDate
您可以使用这些方法中的任何一种从 java.util.Date
You can use any of these method to get LocalDate
from java.util.Date
- LocalDate.fromYearMonthDay(2017, 03, 28)
- LocalDate.fromMillisSinceEpoch(new Date().getTime())
或者您可以创建自己的编解码器,允许您将 java.util.Date
插入到 Cassandra 日期类型中.
Or you could create your own codec that will allow you to insert java.util.Date
into Cassandra date type.
你可以像下面这样开始:
You can start like the below one :
public class DateCodec extends TypeCodec<Date> {
private final TypeCodec<LocalDate> innerCodec;
public DateCodec(TypeCodec<LocalDate> codec, Class<Date> javaClass) {
super(codec.getCqlType(), javaClass);
innerCodec = codec;
}
@Override
public ByteBuffer serialize(Date value, ProtocolVersion protocolVersion) throws InvalidTypeException {
return innerCodec.serialize(LocalDate.fromMillisSinceEpoch(value.getTime()), protocolVersion);
}
@Override
public Date deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException {
return new Date(innerCodec.deserialize(bytes, protocolVersion).getMillisSinceEpoch());
}
@Override
public Date parse(String value) throws InvalidTypeException {
return new Date(innerCodec.parse(value).getMillisSinceEpoch());
}
@Override
public String format(Date value) throws InvalidTypeException {
return value.toString();
}
}
创建连接时,您必须注册:
When creating connectin you have to register :
CodecRegistry codecRegistry = new CodecRegistry();
codecRegistry.register(new DateCodec(TypeCodec.date(), Date.class));
Cluster.builder().withCodecRegistry(codecRegistry).build();
更多信息:http://docs.datastax.com/en/developer/java-driver/3.1/manual/custom_codecs/
这篇关于如何使用 Spring Data Cassandra 将 java.util.Date 值插入到 Cassandra 日期类型列中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!