Question

I need to insert new rows to Cassandra, to a table that has only primary key columns, e.g.:

CREATE TABLE users (
  user_id bigint,
  website_id bigint,
  PRIMARY KEY (user_id, website_id)
)

The obvious way to do it would be by INSERT:

 INSERT INTO users(user_id, website_id) VALUES(1,2);  

But I want to do it with use of Hadoop CqlOutputFormat and CqlRecordWriter only supports UPDATE statements. That's usually not a problem as UPDATE is in theory semantically the same as INSERT. (It will create rows if given primary key does not exist). But here... I don't know how to construct UPDATE statement - it seems that CQL just does not support my case, where there are non-primary key columns. See what I tried:

> update users set where user_id=3 and website_id=2 ;      
Bad Request: line 1:18 no viable alternative at input 'where'

> update users set website_id=2  where user_id=3;                                                                                                                              
Bad Request: PRIMARY KEY part website_id found in SET part

> update users set website_id=2  where user_id=3 and website_id=2;                                                                                                             
Bad Request: PRIMARY KEY part website_id found in SET part

> update users set website_id=2,user_id=1;
Bad Request: line 1:40 mismatched input ';' expecting K_WHERE

Some ideas on how to resolve it? Many thanks.

Was it helpful?

Solution

Not sure if you can do this with update like that. But why not just create a new dummy column that you never use for anything else? Then you could do

update users set dummy=1 where user_id=3 and website_id=2;

OTHER TIPS

You can't update primary key values in Cassandra as you have explained. As a solution you could also delete the row and insert a new one with the correct value in it. It's just a bit cleaner than creating two rows with one incorrect.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top