Is there a way to make clustering order by data type and not string in Cassandra?

StackOverflow https://stackoverflow.com/questions/15259810

  •  18-03-2022
  •  | 
  •  

Question

I created a table in CQL3 in the cqlsh using the following CQL:

CREATE TABLE test (
    locationid int,
    pulseid int,
    name text, PRIMARY KEY(locationid, pulseid)
) WITH CLUSTERING ORDER BY (locationid ASC, pulseid DESC);

Note that locationid is an integer.

However, after I inserted data, and ran a select, I noticed that locationid's ascending sort seems to be based upon string, and not integer.

cqlsh:citypulse> select * from test;
 locationid | pulseid | name
------------+---------+------
          0 |       3 | test
          0 |       2 | test
          0 |       1 | test
          0 |       0 | test
         10 |       3 | test
          5 |       3 | test

Note the 0 10 5. Is there a way to make it sort via its actual data type?

Thanks, Allison

Était-ce utile?

La solution

In Cassandra, the first part of the primary key is the 'partition key'. That key is used to distribute data around the cluster. It does this in a random fashion to achieve an even distribution. This means that you can not order by the first part of your primary key.

What version of Cassandra are you on? In the most recent version of 1.2 (1.2.2), the create statement you have used an example is invalid.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top