0
votes

Hi I'm new in Apache Cassandra and I found article about Basic Rules of Cassandra Data Modeling. In example 1 are created 2 tables

CREATE TABLE users_by_username (
    username text PRIMARY KEY,
    email text,
    age int
)

CREATE TABLE users_by_email (
    email text PRIMARY KEY,
    username text,
    age int
)

This tables contains same data (username, email and age). Here I don't understand how to insert data into two tables. I think, that I have to execute two separate inserts. One for table users_by_username and one for table users_by_email. But how to maintain data consistency between tables. For example when I insert data into first table and I forgot to insert data to second table ... or the other way

1

1 Answers

2
votes

It's your job as developer to make sure that data is in sync. Although, you can use things like materialized views to generate another "table" with slightly different primary key (there are some rules on what could be changed). For your case, for example, you can have following:

CREATE TABLE users_by_username (username text PRIMARY KEY, 
   email text, age int);

create MATERIALIZED VIEW users_by_email as SELECT * from 
   users_by_username where email is not null and 
   username is not null primary key (email, username);

and if you insert data as

insert into users_by_username (username, email, age) 
   values ('test', '[email protected]', 30);

you can query the materialized view for data in addition to query by username

SELECT * from users_by_username  where username = 'test' ;

 username | age | email
----------+-----+-----------------
     test |  30 | [email protected]

SELECT * from users_by_email where email = '[email protected]';

 email           | username | age
-----------------+----------+-----
 [email protected] |     test |  30