How to keep 2 Cassandra tables within same partition
Asked Answered
S

1

8

I tried reading up on datastax blogs and documentation but could not find any specific on this

Is there a way to keep 2 tables in Cassandra to belong to same partition? For example:

CREATE TYPE addr (
  street_address1 text,
  city text,
  state text,
  country text,
  zip_code text,
);

CREATE TABLE foo (
  account_id timeuuid,
  data text,
  site_id int,
  PRIMARY KEY (account_id)
};

CREATE TABLE bar (
  account_id timeuuid,
  address_id int,
  address frozen<addr>,
  PRIMARY KEY (account_id, address_id)
);

Here I need to ensure that both of these tables/CF will live on same partition that way for the same account_id both of these set of data can be fetched from the same node

Any pointers are highly appreciated.

Also, if someone has some experience in using UDT (User Defined Types), I would like to understand how the backward compatibility would work. If I modify "addr" UDT to have a couple of more attributes (say for example zip_code2 int, and name text), how does the older rows that does have these attribute work? Is it even compatible?

Thanks

Saltire answered 15/12, 2015 at 16:46 Comment(0)
L
11

If two table share the same replication strategy and same partition key they will colocate their partitions. So as long as the two tables are in the same keyspace AND their partition keys match

PRIMARY KEY (account_id) == PRIMARY KEY (account_id, address_id)

Any given account_id will be on (and replicated to) the same machines.

Lucilelucilia answered 15/12, 2015 at 17:20 Comment(7)
Excellent! +1 for quick and clean response. Can you please help answer the other part of the question on UDT?Saltire
It's best to separate unrelated questions into different SO questions. This makes the service better in the future for folks looking for similar answers.Lucilelucilia
Got it. I have posted another question: #34300641Saltire
I'm confused by your answer here: #36701359Adiathermancy
Maybe you should comment about what you don't understand on that answer then? @VishalSharmaLucilelucilia
In the linked question, you've written that different tables have different partitions and here you are saying that they can, in certain conditions, reside on the same nodes(partitions). This seems to me contradictory.Adiathermancy
It would be if nodes and partitions were the same but they are different. Every table has a set of partitions. Different tables can have different partitions with the same key. Partitions with the same key reside on the same node (given they are in the same replication pattern) even if they are in different tables.Lucilelucilia

© 2022 - 2024 — McMap. All rights reserved.