【问题标题】:Cassandra timeuuid in Datastax C# driverDatastax C# 驱动程序中的 Cassandra timeuuid
【发布时间】:2014-03-27 23:10:23
【问题描述】:

Datastax Cassandra C#驱动中的timeuuid是什么C#类型?

我正在编写一个简单的用户跟踪服务,并希望访问最新的用户历史记录。我正在尝试创建一个与此创建语句等效的表:

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

我应该使用Guid 而不是DateTime 作为CreateDate 并使用原始CQL3 显式创建表吗?我想这将允许我从/向 Cassandra 读写 timeuuid(使用 FluentCassandra 项目中的 GuidGenerator)?! (回想一下:我正在使用 Datastax 驱动程序)

【问题讨论】:

  • 你感受到对 Cassandra 的爱了吗?
  • 是的。到目前为止,我喜欢数据模型。我已将 CreateDate 更改为 Guid,并使用原始 CQL 而不是 table.CreateIfNotExists() 创建了表。工作正常。

标签: c# cassandra driver datastax


【解决方案1】:

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

“以下是在 .NET 中生成 Time UUID 或 Time-Based 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);
}

【讨论】:

  • 在 DataStax 中有内置的方法:Cassandra.TimeUuid.NewId(dateTime)
  • 确实,Luke 在下面的回答中这么说......事实上,虽然 OP 没有指定他们使用的 DataStax 版本,即它仅在 2.1.3 中可用......因此,如果他们使用的是较低版本,他们可以按照上面的代码手动执行...无论如何,这要感谢所有非评论反对票的人...我应该删除这个标记为答案的答案吗?...即唯一当时回答,这对提问者有帮助吗?
  • @Stas Berkov Cassandra.TimeUuid.NewId(dateTime) 对我来说工作正常。我正在使用版本 [cqlsh 5.0.1 |卡桑德拉 3.5.0 | CQL 规范 3.4.0 |原生协议 v4]
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2014-05-13
    • 1970-01-01
    • 2013-10-10
    • 2016-02-27
    • 2017-01-20
    • 2017-08-04
    • 1970-01-01
    • 2018-02-15
    • 2013-12-23
    相关资源
    最近更新 更多