2014-02-25 126 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驅動程序的表創建方法一起使用,如果您將其用作屬性的返回類型。

相關問題