哪种C#类型等效于Datastax Cassandra C#驱动程序中的timeuuid?
我正在编写一个简单的用户跟踪服务,并希望访问最新的用户历史记录。我正在尝试创建一个与此create语句等效的表:
CREATE TABLE IF NOT EXISTS user_history (
user_id text,
event_type text,
create_date timeuuid,
item_id text,
PRIMARY KEY ((user_id, event_type), create_date)
);
我已经完成了以下课程:
[AllowFiltering]
[Table("user_history")]
public class UserHistory
{
[PartitionKey(1)]
[Column("user_id")]
public string UserID;
[PartitionKey(2)]
[Column("event_type")]
public string EventType;
[ClusteringKey(1)]
[Column("create_date")]
public DateTime CreateDate { get; set; }
[Column("item_id")]
public string ItemID;
}
我正在使用以下语句在Cassandra中创建表:
var table = Session.GetTable<UserHistory>();
table.CreateIfNotExists();
但这给了我下表:
CREATE TABLE user_history (
user_id text,
event_type text,
create_date timestamp,
item_id text,
PRIMARY KEY ((user_id, event_type), create_date)
)
正如你所看到的类型,create_date
是timestamp
不是timeuuid
。
我尝试Guid
代替DateTime
,但是给我一个uuid
打电话的时间.CreateIfNotExists()
。
我应该使用Guid
,而不是DateTime
用于CreateDate
创造使用原始CQL3表明确?我想这将使我能够与timeuuid
Cassandra进行读写(使用GuidGenerator
FluentCassandra项目中的内容)?(回想一下:我正在使用Datastax驱动程序)
Timeuuid基本上是一个GUID,因此您应该使用GUID,以下代码摘自此处:创建一个时间Uuid-GUID-in-net,它是FluentCassandra项目的一部分
“下面是在.NET中生成时间UUID或基于时间的Guid对象所需的所有代码。”
public static Guid GenerateTimeBasedGuid(DateTime dateTime)
{
long ticks = dateTime.Ticks - GregorianCalendarStart.Ticks;
byte[] guid = new byte[ByteArraySize];
byte[] clockSequenceBytes = BitConverter.GetBytes(Convert.ToInt16(Environment.TickCount % Int16.MaxValue));
byte[] timestamp = BitConverter.GetBytes(ticks);
// copy node
Array.Copy(Node, 0, guid, NodeByte, Node.Length);
// copy clock sequence
Array.Copy(clockSequenceBytes, 0, guid, GuidClockSequenceByte, clockSequenceBytes.Length);
// copy timestamp
Array.Copy(timestamp, 0, guid, 0, timestamp.Length);
// set the variant
guid[VariantByte] &= (byte)VariantByteMask;
guid[VariantByte] |= (byte)VariantByteShift;
// set the version
guid[VersionByte] &= (byte)VersionByteMask;
guid[VersionByte] |= (byte)((int)GuidVersion.TimeBased << VersionByteShift);
return new Guid(guid);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句