3

Datastax Cassandra C# ドライバーの 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;
}

そして、このステートメントを使用して、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_datetimestampではなく is ですtimeuuid

Guidの代わりに試してみましたが、呼び出しDateTimeているuuidときに が表示されます.CreateIfNotExists()

for のGuid代わりに使用し、未加工の CQL3 を使用して明示的にテーブルを作成する必要がありますか? これにより、Cassandraとの間で読み書きできるようになると思います( FluentCassandra プロジェクトで見つかったものを使用)。(リコール: Datastax ドライバーを使用しています)DateTimeCreateDatetimeuuidGuidGenerator

4

2 に答える 2

5

Timeuuid は基本的に GUID であるため、GUID を使用する必要があります。次のコードはここから取得されます: creating-a-time-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);
}
于 2014-02-25T15:57:47.373 に答える