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

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

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

javaspringspring-bootcassandraspring-data-cassandra

提问by K. Siva Prasad Reddy

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:

我插入新的人如下:

personRepository.save(new Person(1, "Siva","[email protected]", 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.access00(RequestHandler.java:46) ~[cassandra-driver-core-3.1.4.jar:na]

But if I make dobcolumn type to timestampthen it is working fine. Is it possible to have datetype column and use java.util.Datetype properties?

但是如果我将dob列类型设置为时间戳,那么它就可以正常工作。是否可以有日期类型列并使用java.util.Date类型属性?

P.s: Even if I use java.sql.Date I am getting the same error.

Ps:即使我使用 java.sql.Date 我也遇到同样的错误。

回答by Ashraful Islam

Use com.datastax.driver.core.LocalDate

利用 com.datastax.driver.core.LocalDate

You can use any of these method to get LocalDatefrom java.util.Date

您可以使用这些方法来获得LocalDatejava.util.Date

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

Or you could create your own codec that will allow you to insert java.util.Dateinto Cassandra date type.

或者您可以创建自己的编解码器,以允许您插入java.util.DateCassandra 日期类型。

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();

For more : http://docs.datastax.com/en/developer/java-driver/3.1/manual/custom_codecs/

更多信息:http: //docs.datastax.com/en/developer/java-driver/3.1/manual/custom_codecs/