Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep 2 Cassandra tables within same partition

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

like image 514
im2kul Avatar asked Dec 15 '15 16:12

im2kul


People also ask

Can we have multiple partition keys in Cassandra?

Cassandra allows you to use multiple columns as the partition key for a table with a composite partition key. Unlike a simple partition key, a composite partition key is used when the data stored is too large to reside in a single partition and determines where data will reside with multiple columns.

How is data partitioned in Cassandra?

As we learned earlier, Cassandra uses a consistent hashing technique to generate the hash value of the partition key (app_name) and assign the row data to a partition range inside a node.

Is partition key unique in Cassandra?

The partition key has a special use in Apache Cassandra beyond showing the uniqueness of the record in the database.. Please note that there will not be any error if you insert same partition key again and again as there is no constraint check.

How does Cassandra partition key work?

A partition key can have a partition key defined with multiple table columns which determines which node stores the data. For a table with a composite partition key, Cassandra uses multiple columns as the partition key. These columns form logical sets inside a partition to facilitate retrieval.


1 Answers

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.

like image 140
RussS Avatar answered Sep 23 '22 10:09

RussS