如何使用映射管理器在cassandra日期字段中存储java.sql.Date?

问题描述 投票:1回答:2

有人可以帮助我使用Java以yyyy-mm-dd格式将当前系统日期存储在cassandra日期列中吗?我在使用java.sql.Date保存MappingManager时遇到异常。

我的示例程序是:

test.Java

import com.datastax.driver.mapping.annotations.Table;
import java.sql.Date;
@Table(keyspace = "testing", name = "test")
public class Test {

    private String uid;
    private Date rece;

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public Date getRece() {
        return rece;
    }

    public void setRece(Date rece) {
        this.rece = rece;
    }


}

test DAO.Java

Mapper mapper = new MappingManager(session).mapper(Test.class);
mapper.save(test);

卡桑德拉时间表:

CREATE TABLE tokenizer.test (
    id text PRIMARY KEY,
    testdate date
) ;
java sql cassandra cassandra-3.0
2个回答
1
投票

要么将rece的数据类型声明为com.datastax.driver.core.LocalDate,要么编写自定义编解码器来编码java.sql.Date并将其注册到集群。

自定义编解码器示例:

import com.datastax.driver.core.LocalDate;
import com.datastax.driver.core.ProtocolVersion;
import com.datastax.driver.core.TypeCodec;
import com.datastax.driver.core.exceptions.InvalidTypeException;

import java.nio.ByteBuffer;
import java.sql.Date;


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

如何使用它 ?

示例:

CodecRegistry codecRegistry = new CodecRegistry();
codecRegistry.register(new DateCodec(TypeCodec.date(), Date.class));

try (Cluster cluster = Cluster.builder().withCodecRegistry(codecRegistry).addContactPoint("127.0.0.1").withCredentials("cassandra", "cassandra").build(); Session session = cluster.connect("test")) {
    Mapper mapper = new MappingManager(session).mapper(Test.class);
    mapper.save(test);
}

0
投票

我有类似的问题,根据Ashraful的回答,我在我的模型中添加了以下代码来解决问题。

Meta Data Model:

public class MetaData {
private String receive_date;
private String receive_hour;
**private com.datastax.driver.core.LocalDate error_date;**
}

In my service class, added below code.
final MetaData metaData = MetaData.builder()
                    .receive_date(row.getString("receive_date"))
                    .receive_hour(row.getString("receive_hour"))
                    .error_date(row.getDate("error_date"))
                    .build();
© www.soinside.com 2019 - 2024. All rights reserved.