Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cassandra create table won't keep column order

Tags:

cassandra

I am creating a column family in Cassandra and I expect the column order to match the one I am specifying in the create clause.

This

CREATE TABLE cf.mycf (
    timestamp timestamp,
    id text,
    score int,
    type text,
    publisher_id text,
    embed_url text,
    PRIMARY KEY (timestamp, id, score)
) WITH bloom_filter_fp_chance = 0.01
AND comment = ''
AND dclocal_read_repair_chance = 0.1
AND default_time_to_live = 0
AND gc_grace_seconds = 864000
AND max_index_interval = 2048
AND memtable_flush_period_in_ms = 0
AND min_index_interval = 128
AND read_repair_chance = 0.0
AND speculative_retry = '99.0PERCENTILE'
AND caching = {
    'keys' : 'ALL',
    'rows_per_partition' : 'NONE'
}
AND compression = {
    'chunk_length_kb' : 64,
    'crc_check_chance' : 1.0,
    'sstable_compression' : 'LZ4Compressor'
}
AND compaction = {
    'base_time_seconds' : 60,
    'class' : 'DateTieredCompactionStrategy',
    'enabled' : true,
    'max_sstable_age_days' : 365,
    'max_threshold' : 32,
    'min_threshold' : 4,
    'timestamp_resolution' : 'MICROSECONDS',
    'tombstone_compaction_interval' : 86400,
    'tombstone_threshold' : 0.2,
    'unchecked_tombstone_compaction' : false
};

Should create a table like : timestamp ,id ,score , type, id ,embed_url

Instead I am getting this:

timestamp timestamp,
    id text,
    score int,
    embed_url text,
    publisher_id text,
    type text,

I've created quite a few tables in the same way and this never happened so any help would be appreciated.

I put the id and score as keys to show that these keep their respective position. while the actual scheme I am looking for is only the timestamp to be the primary key.

like image 600
raam86 Avatar asked Dec 03 '15 17:12

raam86


2 Answers

Looks like there is no such thing as fields order in cassandra.

The others columns are displayed in alphabetical order by Cassandra. 

http://docs.datastax.com/en/cql/3.1/cql/ddl/ddl_compound_keys_c.html

like image 121
Alex Dvoretsky Avatar answered Oct 23 '22 11:10

Alex Dvoretsky


You should make a clear distinction on how you want the data to be presented and how it is effectively presented to you. Moreover, you should not rely on the ordinal position of the fields but only on their names.

In order to be efficient, and against your will (you specified an order to the columns when you modeled your schema), Cassandra needs to store the columns in a particular order, and for simplicity this reflects on how it (the CQL interface or the driver) will give back your data.

I suggest you to have a deep insight on how Cassandra stores data (column names included!) in Understanding How CQL3 Maps to Cassandra’s Internal Data Structure.

By the way, if you absolutely need to keep your order at application level (and are too lazy to specify all the fields in the SELECT instead of using SELECT *), you need to create an abstraction interface on your own, something like creating an ordered "field names" array (your order):

String myorder[] = { "timestamp", "id", "score", "type", "publisher_id", "embed_url"};

and then use this as a map in loops using ordinal values.

like image 42
xmas79 Avatar answered Oct 23 '22 09:10

xmas79