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

标签 java sql cassandra cassandra-3.0

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

我的示例程序是:

测试.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;
    }


}

TestDao.java

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

Cassandra 架构:

CREATE TABLE tokenizer.test (
    id text PRIMARY KEY,
    testdate date
) ;

最佳答案

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

自定义编解码器示例:

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

关于java - 如何使用映射管理器将 java.sql.Date 存储在 cassandra 日期字段中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45803848/

相关文章:

java - 如何从缓冲图像中获取子图像

mysql - 如何修复mysql中的 "right syntax to use near ' as begin“错误?

MySQL 无法正确获取 PROCEDURE 中的参数值

cassandra - JanusGraph gremlin 如何知道它应该使用 Cassandra 而不是 Hadoop?

java - 使用链表从 Cassandra 接收结果

java - Play 1.2.4 : Rendering XML in template

java - 需要连接的 hibernate 路径,但路径已设置

java - 嵌入式 jetty 中的异步 servlet 似乎在高负载时写入错误响应

mysql - 在日期范围和特定时间范围之间选择

Cassandra ArrayIndexOutOfBoundsException 异常