2014-02-25 129 views
3

什么C#类型等同于Datastax Cassandra C#驱动程序中的timeuuid?Datastax中的Cassandra timeuuid C#驱动程序

我正在写一个简单的用户跟踪服务,并且想要访问最新的用户历史记录。我想创建一个表相当于此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; 
} 

,我使用这个语句来创建卡桑德拉表:

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,但是当我打电话给.CreateIfNotExists()时,它给了我一个uuid

我是否应该使用Guid而不是DateTime作为CreateDate并使用原始CQL3创建表格?我想这将允许我读/写Cassandra的timeuuid(使用FluentCandandra项目中的GuidGenerator)? (回忆一下:我使用的是Datastax驱动程序)

+0

你是否感受到卡桑德拉的爱? –

+0

是的。目前为止,我喜欢数据模型。我已将CreateDate更改为Guid,并使用原始CQL而不是table.CreateIfNotExists()创建表。工作正常。 – user628904

回答

4

Timeuuid基本上是一个GUID,所以你应该使用GUID,the following code is taken from here: creating-a-time-uuid-guid-in-net and is part of the FluentCassandra project

“下面是你需要生成一个时间UUID或代码基于时间的Guid .NET中的对象“。

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

在DataStax中有内置的方法:Cassandra.TimeUuid.NewId(dateTime) –

+0

确实,Luke在他的回答中如此表达......事实上,OP没有指定他们使用哪个版本的DataStax ,即它只在2.1.3中可用......所以如果他们使用较低版本,他们可以按照上面的代码手动执行它...反正感谢所有非评论性的投票人...我应该删除这个答案标记为答案?...即当时唯一的答案,这有助于提问者? –

+0

@Stas Berkov Cassandra.TimeUuid.NewId(dateTime)对我来说工作正常。我正在使用版本[cqlsh 5.0.1 | Cassandra 3.5.0 | CQL规范3.4.0 |本机协议v4] –

5

听起来像您通过手动创建表来解决您的问题。

对于将来寻找这些信息的人来说,由于我们在BCL中没有用于表示TimeUUID的本机类型,所以2.1.3版本的DataStax .NET Driver引入了TimeUuid结构来帮助这种情况。你可以找到the source here。它有一个到/从System.Guid的隐式转换,并且应该与LINQ驱动程序的表创建方法一起使用,如果您将其用作属性的返回类型。

相关问题