Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Columns ordering in Cassandra

Tags:

cassandra

cql

When I create a table in CQL, is it necessary to be exact for the order of column that are NOT in the primary_key and NOT clustering columns :

CREATE TABLE user (
    a ascii,
    b ascii,
    c ascii,
    PRIMARY KEY (a)
);

Is it equivalent to ?

CREATE TABLE user (
    a ascii,
    c ascii, <-- switched
    b ascii, <-- switched
    PRIMARY KEY (a)
);

Thank you for your help

like image 795
Fundhor Avatar asked Jan 06 '16 14:01

Fundhor


People also ask

How do I ORDER BY Cassandra?

You can fine-tune the display order using the ORDER BY clause. The partition key must be defined in the WHERE clause and the ORDER BY clause defines the clustering column to use for ordering. cqlsh> CREATE TABLE cycling.

What is clustering order in Cassandra?

Ordering query results to make use of the on-disk sorting of columns. You can order query results to make use of the on-disk sorting of columns. You can order results in ascending or descending order. The ascending order will be more efficient than descending.

What are clustering columns in Cassandra?

Clustering columns order data within a partition. When a table has multiple clustering columns the data is stored in nested sort order. The database uses the clustering information to identify where the data is within the partition.


1 Answers

Both of those statements will fail, because of:

  • The extra comma.
  • You have not provided a primary key definition.

Assuming you had those fixed, then the answer is still "yes they are the same."

Cassandra applies its own order to your columns at table creation time. Consider this table as I have typed it:

CREATE TABLE testorder ( 
  acolumn text,
  jcolumn text,
  dcolumn text,
  bcolumn text,
  apkey text,
  bpkey text,
  ackey text,
  bckey text,
  PRIMARY KEY ((bpkey,apkey),bckey,ackey));

After creating it, I'll describe the table so you can see the order that Cassandra has applied to the columns.

aploetz@cqlsh:stackoverflow> desc table testorder ;

CREATE TABLE stackoverflow.testorder (
    bpkey text,
    apkey text,
    bckey text,
    ackey text,
    acolumn text,
    bcolumn text,
    dcolumn text,
    jcolumn text,
    PRIMARY KEY ((bpkey, apkey), bckey, ackey)
) WITH CLUSTERING ORDER BY (bckey ASC, ackey ASC)

Essentially, Cassandra will order the partition keys and the clustering keys (ordered by their precedence in the PRIMARY KEY definition), and then the columns follow in ascending order.

like image 52
Aaron Avatar answered Sep 21 '22 12:09

Aaron