c#-将DateTime保存到Cassandra Date列
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#-将DateTime保存到Cassandra Date列,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4269字,纯文字阅读大概需要7分钟。
内容图文
Cassandra .NET驱动程序文档真是太糟糕了,我试图将一些功能拼凑在一起,但我浪费大量时间试图从发现的Java文档中更改代码.
我正在尝试使用Cassandra驱动程序将数据写入一个简单的表.该表已经存在,并且里面有日期.我创建了一个映射并添加了一些列.这是演示的简化版本:
For<Profile>().TableName("profiles")
.PartitionKey(p => p.IntegerId)
.Column(p => p.IntegerId, cm => cm.WithName("profileid"))
.Column(p => p.BirthDate, cm => cm.WithName("dateofbirth"))
列和表还有更多,但这是重要的部分.
然后通过一个简单的通用方法完成保存:
public async Task<bool> Add<T>(T item) where T : EntityBase, new()
{
await _mapper.InsertIfNotExistsAsync(item);
}
同样,这里还有更多代码,但是相关的部分在这里.重要的是,我正在使用InsertIfNotExists并使用与基本实体一起使用的通用方法.
Cassandra中的dateofbirth列的类型为Date.
当我运行Insert方法时,我得到一个例外,即Date的长度应该是4个字节而不是8个字节(我想我需要截断DateTime的时间部分).
我尝试在映射上使用WithType并创建一个类似于this question处描述的TypeSerializer,但是没有运气.
任何人都有可以将这种类型(可能还有其他类型)保存到Cassandra的有效代码吗?
这是从互联网改编的日期编解码器的代码,以及如何使用它,这可能是(非常)错误的:
public class DateCodec : TypeSerializer<DateTime>
{
private static TypeSerializer<LocalDate> _innerSerializer;
public DateCodec(TypeSerializer<LocalDate> serializer)
{
_innerSerializer = serializer;
TypeInfo = new CustomColumnInfo("LocalDate");
}
public override IColumnInfo TypeInfo { get; }
public override DateTime Deserialize(ushort protocolVersion, byte[] buffer, int offset, int length, IColumnInfo typeInfo)
{
var result = _innerSerializer.Deserialize(protocolVersion, buffer, offset, length, typeInfo);
return new DateTime(result.Year, result.Month, result.Day);
}
public override ColumnTypeCode CqlType { get; }
public override byte[] Serialize(ushort protocolVersion, DateTime value)
{
return _innerSerializer.Serialize(protocolVersion, new LocalDate(value.Year, value.Month, value.Day));
}
}
用法:
TypeSerializerDefinitions definitions = new TypeSerializerDefinitions();
definitions.Define(new DateCodec(TypeSerializer.PrimitiveLocalDateSerializer));
var cluster = Cluster.Builder()
.AddContactPoints(...)
.WithCredentials(...)
.WithTypeSerializers(definitions)
.Build();
解决方法:
C#驱动程序使用LocalDate类表示Cassandra中的日期,因此需要更改您的dateofbirth声明才能使用它,或者开发相应的编解码器.
您可以查看有关C#驱动程序的日期和时间表示形式的文档:https://docs.datastax.com/en/developer/csharp-driver/3.5/features/datatypes/datetime/
使用代码示例更新问题后进行更新:
定义表格和插入样本数据:
cqlsh> create table test.dt(id int primary key, d date);
cqlsh> insert into test.dt(id, d) values(1, '2018-05-17');
cqlsh> insert into test.dt(id, d) values(2, '2018-05-16');
cqlsh> insert into test.dt(id, d) values(3, '2018-05-15');
以下对我有用的转换作品:
public class DateCodec : TypeSerializer<DateTime>
{
private static readonly TypeSerializer<LocalDate> serializer =
TypeSerializer.PrimitiveLocalDateSerializer;
public override ColumnTypeCode CqlType
{
get { return ColumnTypeCode.Date; }
}
public DateCodec() { }
public override DateTime Deserialize(ushort protocolVersion, byte[] buffer,
int offset, int length, IColumnInfo typeInfo)
{
var result = serializer.Deserialize(protocolVersion, buffer,
offset, length, typeInfo);
return new DateTime(result.Year, result.Month, result.Day);
}
public override byte[] Serialize(ushort protocolVersion, DateTime value)
{
return serializer.Serialize(protocolVersion,
new LocalDate(value.Year, value.Month, value.Day));
}
}
主程序:
TypeSerializerDefinitions definitions = new TypeSerializerDefinitions();
definitions.Define(new DateCodec());
var cluster = Cluster.Builder()
.AddContactPoints("localhost")
.WithTypeSerializers(definitions)
.Build();
var session = cluster.Connect();
var rs = session.Execute("SELECT * FROM test.dt");
foreach (var row in rs)
{
var id = row.GetValue<int>("id");
var date = row.GetValue<DateTime>("d");
Console.WriteLine("id=" + id + ", date=" + date);
}
var pq = session.Prepare("insert into test.dt(id, d) values(?, ?);");
var bound = pq.Bind(10, new DateTime(2018, 04, 01));
session.Execute(bound);
结果如下:
id=1, date=5/17/18 12:00:00 AM
id=2, date=5/16/18 12:00:00 AM
id=3, date=5/15/18 12:00:00 AM
并从cqlsh检查:
cqlsh> SELECT * from test.dt ;
id | d
----+------------
10 | 2018-04-01
1 | 2018-05-17
2 | 2018-05-16
3 | 2018-05-15
内容总结
以上是互联网集市为您收集整理的c#-将DateTime保存到Cassandra Date列全部内容,希望文章能够帮你解决c#-将DateTime保存到Cassandra Date列所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。