Reputation: 119
I have some predefined columns in a Cassandra table/column family. I also get a another JSON /dictionary object which has key:value pair which are not predefined and hence were not included in the create table statement (I am using cql3 library on python). I want to add this data to my Cassandra table, I want a unique key in the json object to become a new column. If a key which has already been made a column is present in the json, Cassandra should just reject that query without throwing an exception. It throws an exception if I try to simply alter the table to add a pre existing column. Is there an alter table add column if column doesn't exists type query or should I handle it through exception handling?
Upvotes: 1
Views: 2035
Reputation: 14153
Currently there isn't a mechanism for doing this. The IF NOT EXISTS
functionality is currently only for CREATE and DROP modification statements (take a look at Conditional schema modifications).
I would remodel the table to allow what you call the key to be in a column (as in column of a table) and use a collection to store its various values ending up with something like:
key | date_added | values
--------+--------------------------------------+-------
1 | e64d8be0-4f2a-11e4-b409-138283d4b034 | ['item1', 'item2']
7 | e64d8be0-4f2a-11e4-b409-138283d4b034 | ['item1', 'item5']
1 | e64d8be0-4f2a-11e4-b409-138283d4b034 | ['item4']
On a side note, if you have a IF NOT EXISTS
alter statement every time you're carrying out a query you would incur a lot of overhead verifying that the row doesn't exist.
Upvotes: 1