简体   繁体   English

如何使用Spring Data Cassandra将java.util.Date值插入Cassandra日期类型列中?

[英]How to insert java.util.Date values into Cassandra date type column using Spring Data Cassandra?

I have cassandra table with a date type column as follows: 我有带有日期类型列的cassandra表,如下所示:

create table people
(
   id int primary key, 
   name text, 
   email text, 
   dob date
);

I am using SpringBoot 1.5.2 + Spring Data Cassandra Starter. 我正在使用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: 我要插入新的Person,如下所示:

personRepository.save(new Person(1, "Siva","siva@gmail.com", new java.util.Date()));

It is throwing the following error: 它抛出以下错误:

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]

But if I make dob column type to timestamp then it is working fine. 但是,如果我将dob列类型设置为时间戳 ,则工作正常。 Is it possible to have date type column and use java.util.Date type properties? 是否可以具有日期类型列并使用java.util.Date类型属性?

Ps: Even if I use java.sql.Date I am getting the same error. 附:即使我使用java.sql.Date,我也会遇到同样的错误。

Use com.datastax.driver.core.LocalDate 使用com.datastax.driver.core.LocalDate

You can use any of these method to get LocalDate from java.util.Date 您可以使用任何这些方法从java.util.Date获取LocalDate

  • LocalDate.fromYearMonthDay(2017, 03, 28) LocalDate.fromYearMonthDay(2017,03,28)
  • LocalDate.fromMillisSinceEpoch(new Date().getTime()) LocalDate.fromMillisSinceEpoch(new Date()。getTime())

Or you could create your own codec that will allow you to insert java.util.Date into Cassandra date type. 或者,您可以创建自己的编解码器,以便将java.util.Date插入Cassandra日期类型。

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 : 创建connectin时,必须注册:

CodecRegistry codecRegistry = new CodecRegistry();
codecRegistry.register(new DateCodec(TypeCodec.date(), Date.class));
Cluster.builder().withCodecRegistry(codecRegistry).build();

For more : http://docs.datastax.com/en/developer/java-driver/3.1/manual/custom_codecs/ 有关更多信息: http : //docs.datastax.com/zh-CN/developer/java-driver/3.1/manual/custom_codecs/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM