use*_*904 3 c# driver cassandra datastax
什么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)
);
Run Code Online (Sandbox Code Playgroud)
我做了以下课程:
[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;
}
Run Code Online (Sandbox Code Playgroud)
我正在使用此语句在Cassandra中创建表:
var table = Session.GetTable<UserHistory>();
table.CreateIfNotExists();
Run Code Online (Sandbox Code Playgroud)
但是这给了我下面的表格:
CREATE TABLE user_history (
user_id text,
event_type text,
create_date timestamp,
item_id text,
PRIMARY KEY ((user_id, event_type), create_date)
)
Run Code Online (Sandbox Code Playgroud)
正如你所看到的类型,create_date是timestamp不是timeuuid.
我试过Guid而不是DateTime,但这给了我一个uuid什么时候我打电话.CreateIfNotExists().
我应该使用Guid,而不是DateTime用于CreateDate创造使用原始CQL3表明确?我想这将允许我timeuuid从/向Cassandra 读取和写入(使用GuidGeneratorFluentCassandra项目中的发现)?!(回想一下:我正在使用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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3869 次 |
| 最近记录: |