3
votes

Cassandra docs and blog says that batches with conditional update statements work at the granularity of the partition, and partition is defined by first key in primary key So, I have following keyspaces:

CREATE TABLE SOCIAL_PROFILE (
soc_net_type text,
soc_net_user_id text,
user_prof_id text,
PRIMARY KEY (soc_net_type, soc_net_user_id)); 

CREATE TABLE SOCIAL_PROFILE_CONTACT (
soc_prof_soc_net_type text,
soc_prof_soc_net_user_id text,
soc_net_user_id text,
PRIMARY KEY (soc_prof_soc_net_type, soc_prof_soc_net_user_id, soc_net_user_id));

And insert statement:

BEGIN BATCH INSERT INTO social_profile (soc_net_type, soc_net_user_id, user_prof_id) VALUES ('vk', '1', '100') IF NOT EXISTS; INSERT INTO social_profile_contact (soc_prof_soc_net_type, soc_prof_soc_net_user_id, soc_net_user_id) VALUES ('vk', '1', '2'); INSERT INTO social_profile_contact (soc_prof_soc_net_type, soc_prof_soc_net_user_id, soc_net_user_id) VALUES ('vk', '1', '3'); INSERT INTO social_profile_contact (soc_prof_soc_net_type, soc_prof_soc_net_user_id, soc_net_user_id) VALUES ('vk', '1', '4'); APPLY BATCH;

And social_profile_contact.soc_prof_soc_net_type = social_profile.soc_net_type they are both have same values and should be in the same partition, but Cassandra throws: Bad Request: Batch with conditions cannot span multiple tables

I haven't found a word about tables in docs, what I'm doing wrong?

cqlsh 4.1.1 | Cassandra 2.0.11 | CQL spec 3.1.1 | Thrift protocol 19.39.0

2

2 Answers

5
votes

"Batch with conditions cannot span multiple tables"

Two different column families (tables) using the same primary key are still two different partitions.

1
votes

You need to move the statement with IF NOT EXISTS in the independent batch. INSERT INTO social_profile (soc_net_type, soc_net_user_id, user_prof_id) VALUES ('vk', '1', '100') IF NOT EXISTS;

The batch insert with conditionals IF NOT EXISTS could contain only single type.