【问题标题】:C# cassandra composite primary keyC# cassandra 复合主键
【发布时间】:2016-04-08 03:56:35
【问题描述】:

重新发送对 Cassandra 表的更改后,我在 C# 中遇到了问题。原始主键由两部分组成((person_id),car_id),不足以识别单行,因此键被扩展((person_id),car_id,purchase_date)。

person_id(bigint), car_id(大整数), 购买日期(timeuuid)

在此更改之后,我遇到了一些 java 错误

2016-01-04 14:24:42.2935|ERROR|MyModule|工作时出错 java.lang.RuntimeException:无法在 org.apache.cassandra.db.marshal.CompositeType(org. apache.cassandra.db.marshal.ReversedType(org.apache.cassandra.db.marshal.TimeUUIDType),org.apache.cassandra.db.marshal.UTF8Type)。这可能是由于架构和读取的数据不匹配

我正在使用 datastax 2.7.0.0 并通过我的查询检索一个人的所有记录:

this.storage.GetEntityList<MyEntity>(r => r.PersonId== personid);

public virtual IEnumerable<T> GetEntityList<T>(Expression<Func<T, bool>> predicate)
        {
            return new Table<T>(this.Session)
                .Where(predicate)
                .Execute();
        }


public class MyEntity
{
    [PartitionKey]
    [Column("person_id")]
    public long PersonId{ get; set; }

    [ClusteringKey]
    [Column("car_id")]
    public long CarId{ get; set; }

    [ClusteringKey(1)]
    [Column("purchase_date")]
    public TimeUuid PurchaseDate{ get; set; }

Ofc 如果我使用带有 SimpleStatement 的 cql 查询数据库,它的工作原理就像一个魅力(ironi),但我不在乎。我想知道为什么我的解决方案失败了。

【问题讨论】:

    标签: c# cassandra datastax


    【解决方案1】:

    GCassandra TimeUUIDType 的 C# 等效项是 System.Guid。见CQL data types to C# types

    并且 ClusteringKey 属性构造函数可以采用 SortOrder 参数。你应该试试:

    [ClusteringKey(1, SortOrder.Descending)]
    [Column("purchase_date")]
    public Guid PurchaseDate{ get; set; }
    

    【讨论】:

    • 我发现很难将您的回答与我的问题联系起来,这只会对回答进行排序。
    • 我不确定我的回答能否解决您的问题。但我知道的是,同一个分区中的条目是自然排序的。在 Java 异常中提到了 ReversedType。我想这意味着 purchase_date 是按降序排列的。
    • 另外,我修复了 PurchaseDate 属性的类型。
    • 日期在db中是按desc排序的,我尝试在代码中设置,没有影响问题。
    猜你喜欢
    • 2023-03-31
    • 2014-04-04
    • 1970-01-01
    • 2019-10-14
    • 2023-03-24
    • 2018-08-31
    • 2012-10-08
    • 2013-09-04
    • 1970-01-01
    相关资源
    最近更新 更多