Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't my Cassandra update work?

Tags:

cassandra

I have the following Cassandra table:

create table start_stop (id text, start text, end text, price double, PRIMARY KEY (id, start));

I do an insert like this:

insert into start_stop (id, start) values ('123', 'w');

Now I want to do an update:

update start_stop set end = 'z' where id = '123';

and I get an error: InvalidRequest: code=2200 [Invalid query] message="Some clustering keys are missing: start"

How can I fix this, aside from doing a query to look up the start value before doing an update?

like image 755
jcm Avatar asked Apr 03 '16 01:04

jcm


People also ask

Is insert and update same in Cassandra?

Insert, Update, and Upsert Because Cassandra uses an append model, there is no fundamental difference between the insert and update operations. If you insert a row that has the same primary key as an existing row, the row is replaced. If you update a row and the primary key does not exist, Cassandra creates it.

Can we update partition key in Cassandra?

You simply cannot alter the primary key of a Cassandra table. You need to create another table with your new schema and perform a data migration. I would suggest that you use Spark for that since it is really easy to do a migration between two tables with only a few lines of code.

Is primary key mandatory in Cassandra?

You can't create a table in Cassandra without a primary key, But still if you want to save your data you can add an additional column to your table (let say "pk") with data type UUID.

What is Upsert in Cassandra?

Upsert means that Cassandra will insert a row if a primary key does not exist already otherwise if primary key already exists, it will update that row.


1 Answers

Your primary key is incomplete, and cassandra can only update rows if there is a primary key match.

If you look at your table structure, (id, start) combined makes primary key, out of which id is partition key.

In cassandra there is no difference between update and insert, but in either case, you need to have complete primary key, for cassandra to find specific row.

You need to use either of below to update.

update start_stop set end = 'z' where id = '123' and start='w';

insert into start_stop (id, start, end) values ('123', 'w', 'z');
like image 134
Abhishek Anand Avatar answered Sep 28 '22 07:09

Abhishek Anand