简体   繁体   中英

cassandra data consistency issue

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

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', 'test@domain.com', 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 | test@domain.com

SELECT * from users_by_email where email = 'test@domain.com';

 email           | username | age
-----------------+----------+-----
 test@domain.com |     test |  30

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM