2014-04-02 44 views
4

如何在使用Datastax C#驱动程序的timeuuid数据类型的CQL查询中创建“大于”或“小于”where-conditions?“大于”where-condition on timeuuid使用Datastax C#Cassandra驱动程序

我有卡桑德拉一个表,用于存储时间戳归类为timeuuid cookie的历史:

CREATE TABLE cookie_history (
    cookie_id text, 
    create_date timeuuid, 
    item_id text, 
    PRIMARY KEY ((cookie_id), create_date) 
); 

该表是用C#类映射使用Datastax C#卡桑德拉司机询问:

[Table("cookie_history")] 
public class CookieHistoryDataEntry 
{ 
    [PartitionKey(1)] 
    [Column("cookie_id")] 
    public string CookieID; 

    [ClusteringKey(1)] 
    [Column("create_date")] 
    public Guid CreateDate; 

    [Column("item_id")] 
    public string ItemID; 
} 

对于给定的cookie,我希望给定时间戳后的所有项目。

 var myTimeUuid = new Guid("5812e74d-ba49-11e3-8d27-27303e6a4831"); 
     var table = session.GetTable<CookieHistoryDataEntry>(); 
     var query = table.Where(x => x.CookieID == myCookieId 
            && x.CreateDate > myTimeUuid); 

但这(x.CreateDate> myTimeUuid)给了我一个编译时错误:

Operator '>' cannot be applied to operands of type 'System.Guid' and 'System.Guid' 

回答

2

在原始CQL中,可以在timeuuid上使用“大于”。所以,一个解决方案是从驾驶员执行原CQL:

session.Execute(@"select * 
    from cookie_history 
    where cookie_id = 1242a96c-4bd4-8505-1bea-803784f80c18 
    and create_date > 5812e74d-ba49-11e3-8d27-27303e6a4831;"); 
0

是否有您想要尝试代表您的日期作为一个GUID的理由,而不是实际的日期类型?关于Guid的概念没有大于或小于Guid的概念,除非这是我不知道的一些边缘情况。日期A可以大于日期B,从我见过的所有事情都不是Guid's的事实。

+0

我使用的日期作为我的专栏的关键。我使用日期的Guid表示来避免发生的事件与列键的冲突。为了解决这个“大于”不支持的问题,我将查询最新的X条目和内存中的日期过滤器。 – user628904

+0

我认为此线程中的讨论与您的情况非常相关:http://stackoverflow.com/questions/5086192/sql-server-using-datetime-as-primary-key –

+0

链接讨论的要点是当我们也将用户特定的密钥视为标识符的一部分时,我们只有相同时间戳的条目碰撞。但我不能假定这是因为我的一些条目在时间戳记中没有“时间部分”(但仅包含“日期部分”),所以我需要使用timeuuid来避免列键中的冲突。 – user628904

3

如果你使用的CompareTo(),您可以使用LINQ:

var query = table.Where(x => x.CookieID == myCookieId && 
         x.CreateDate.CompareTo(myTimeUuid) > 0; 

Here's the code that handles the CompareTo.

相关:如果您需要比较需要使用Cassandra token()方法的分区键,可以使用CqlToken.Create完成:

var query = table.Where(x => 
    CqlToken.Create(x.PartitionKeyProperty) >= CqlToken.Create(3); 
+1

这应该是被接受的答案。 –

0

其实还可以用QueryBuilder做的不仅仅是为原料CQL:

select.where(QueryBuilder.eq('cookie_id', cookie_id). 
    and(QueryBuilder.gt("create_date", 
     QueryBuilder.fcall("maxTimeuuid", 
      QueryBuilder.fcall("unixTimestampOf", 
       QueryBuilder.raw(timeuuid_as_string)))); 
相关问题