FluentCassandra range selection problem

2019-05-03 08:19发布

问题:

I have a problem in getting some data out from Cassandra using c# and FluentCassandra. In my Cassandra keyspace i have the following super column family definition:

<ColumnFamily Name="MySCFName"
                    ColumnType="Super"
                    CompareWith="TimeUUIDType"

                CompareSubcolumnsWith="AsciiType"/>

What i would like to do is run a query on this supercolumnfamily similar to the following in sql:

select "something" from MyTable where "timestamp" between "2011-01-01 00:00:00.000" and "2011-03-01 00:00:00.000"

Following one tutorial i found i can get some data out of Cassandra with the following command:

family.Get("238028210009775").Fetch(DateTime.Parse("2011-01-01 00:00:00.000")).FirstOrDefault();

but this is equivalent in sql "timestamp" > "2011-01-01 00:00:00.000"

and so far i cannot figure out how to retrieve data from a range of values.

Any hints or help will be appreciated :) Thanks in advance, Nicola

回答1:

You should be able to do it with the following:

using (var db = new CassandraContext(keyspace: "keyspace_name", host: "localhost"))
{
    var fromDate = DateTime.Now.Subtract(new TimeSpan(0, 30, 0));
    var toDate = DateTime.Now;
    var family = db.GetColumnFamily<TimeUUIDType, AsciiType>("family_name");
    var results = family.Get("row_key")
                        .Fetch(fromDate)
                        .TakeUntil(toDate)
                        .FirstOrDefault();
}