如何使用映射管理器将java.sql.Date存储在cassandra日期字段中?
有人能帮我用Java将当前系统日期以如何使用映射管理器将java.sql.Date存储在cassandra日期字段中?,java,sql,cassandra,cassandra-3.0,Java,Sql,Cassandra,Cassandra 3.0,有人能帮我用Java将当前系统日期以yyyy-mm-dd格式存储在cassandra日期列中吗?使用MappingManager保存java.sql.Date时出现异常 我的示例程序是: Test.java import com.datastax.driver.mapping.annotations.Table; import java.sql.Date; @Table(keyspace = "testing", name = "test") public class Test { p
yyyy-mm-dd
格式存储在cassandra日期列中吗?使用MappingManager
保存java.sql.Date
时出现异常
我的示例程序是:
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;
}
}
Mapper mapper = new MappingManager(session).mapper(Test.class);
mapper.save(test);
TestDao.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;
}
}
Mapper mapper = new MappingManager(session).mapper(Test.class);
mapper.save(test);
卡桑德拉模式:
向
com.datasax.driver.core.LocalDate
声明rece
的数据类型,或编写自定义编解码器对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();
}
}
我有类似的问题,根据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();
A日期
。你也可以回答这个问题并添加你得到的异常