Question

I'm pretty new to Cassandra, I have created a table like so:

CREATE TABLE IF NOT EXISTS notifications (
  id uuid,
  raised timeuuid,
  recorded timeuuid,
  customer varchar,
  region varchar,
  location varchar,
  operator varchar,
  till varchar,
  receipt_id varchar,
  value decimal,
  details text,
  is_refund boolean,
  has_promos boolean,
  utc_offset int,
  frame_count int,
  expecting_video boolean,
  PRIMARY KEY (id, raised)
) WITH CLUSTERING ORDER BY (raised desc);

And then inserted 1,000 rows from the DataStax .NET Cassandra adapter like so:

for (var i = 0; i < 1000; i++)
{
                    var id = Guid.NewGuid();
                    var now = DateTime.Now;

                    var insertNotif = session.Prepare(@"
                    INSERT INTO notifications 
                    (id,customer,region,location,operator,till,receipt_id,value,details,is_refund,has_promos,raised,recorded,utc_offset,frame_count,expecting_video)
                    VALUES (?,?,?,?,?,?,?,?,?,?,?,now(),now(),?,?,?)");
                    var insertNotifStatement = insertNotif.Bind(id, "cust1", "reg1", "loc1", "tomm", "london", i.ToString(), i % 10.99D, "DATA_HERE", false, true, (int)TimeZone.CurrentTimeZone.GetUtcOffset(now).TotalMinutes, 0, false);

                    session.Execute(insertNotifStatement);
                    Thread.Sleep(10);
}

What I'd expect to happen is that all the records will be stored in time descending order based on the raised column. When I use CQLSH to inspect the data, it appears to be in a random order:

cqlsh:my_keyspace> select dateOf(raised) from notifications limit 5;

 dateOf(raised)
--------------------------------------
 2014-06-03 11:12:45GMT Daylight Time
 2014-06-03 11:12:48GMT Daylight Time
 2014-06-03 11:12:56GMT Daylight Time
 2014-06-03 11:12:32GMT Daylight Time
 2014-06-03 11:12:34GMT Daylight Time

(5 rows)

Have I done something wrong? BTW, I have also tried to set raised to a timestamp column and expected the same behaviour (but I passed in DateTimes from .NET instead of now()), but it behaves in the exact same way.

How can I get my records store and retrieved in time descending order?

Was it helpful?

Solution

The clustering order refers to the ordering within the partition i.e. for the same ID. So if you inserted multiple rows with different raised values and queried, you would see them returned in descending order.

The order you are seeing is the order of IDs. As you observed, these are essentially random. This order is the partitioner order i.e. ordered by the hash of the ID.

You can't change this order, so you can't read all your partitions back in time order. You can only order within each partition.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top