Cassandra 用户 table 数据建模
Cassandra user table data modelling
我正在学习卡桑德拉。我必须做基本的用户 table 建模。这是我做过的。
CREATE KEYSPACE IF NOT EXISTS users
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 3};
CREATE TYPE IF NOT EXISTS users.date_type(
month SMALLINT,
day SMALLINT,
year INT
);
CREATE TYPE IF NOT EXISTS users.phone_type(
code INT,
phone TEXT,
);
-- create user
CREATE TABLE IF NOT EXISTS users.user(
userid TIMEUUID,
name TEXT,
dob FROZEN<date_type>,
username TEXT,
email TEXT,
phone FROZEN<phone_type>,
email_verified BOOLEAN,
phone_verified BOOLEAN,
created_on TIMESTAMP,
PRIMARY KEY (userid)
);
-- get user by username
CREATE TABLE IF NOT EXISTS users.user_by_username(
userid TIMEUUID,
username TEXT,
password TEXT,
PRIMARY KEY (username)
);
-- get user by user phone number
-- can have same number for more than one account
CREATE TABLE IF NOT EXISTS users.user_by_phone(
userid TIMEUUID,
phone FROZEN<phone_type>,
password TEXT,
PRIMARY KEY (phone, userid)
);
-- get user by email
-- can have same email for more than one account
CREATE TABLE IF NOT EXISTS users.user_by_email(
userid TIMEUUID,
email TEXT,
password TEXT,
PRIMARY KEY (email, userid)
);
-- user history
CREATE TABLE IF NOT EXISTS users.user_history(
userid TIMEUUID,
name TEXT,
dob FROZEN<date_type>,
username TEXT,
phone FROZEN<phone_type>,
email TEXT,
phone_verified BOOLEAN,
email_verified BOOLEAN,
action TEXT,
date TIMESTAMP,
PRIMARY KEY (userid, date, action)
);
- 根据 cassandra 数据建模,以上 table 设计是否有效?
- 为什么因为如果我想创建一个用户,我必须插入 5 tables
- 如果我想更新电子邮件、phone 或密码,首先我必须删除 table 中的条目,然后插入更新后的值
Is above table design valid as per cassandra data modeling?
是的,根据 Cassandra 数据建模table 设计是有效的。
Why because if I want to create an user, I have to insert in 5 tables
这就是你在 Cassandra 中的做法,首先你有你想要满足的查询,然后根据它们设计你的 tables。
And if I want to update email, phone or password, first I have to
delete entry in table then insert with updated values
只需更新值,无需先删除它们。但请确保更新每个 table.
中的相应条目
我正在学习卡桑德拉。我必须做基本的用户 table 建模。这是我做过的。
CREATE KEYSPACE IF NOT EXISTS users
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 3};
CREATE TYPE IF NOT EXISTS users.date_type(
month SMALLINT,
day SMALLINT,
year INT
);
CREATE TYPE IF NOT EXISTS users.phone_type(
code INT,
phone TEXT,
);
-- create user
CREATE TABLE IF NOT EXISTS users.user(
userid TIMEUUID,
name TEXT,
dob FROZEN<date_type>,
username TEXT,
email TEXT,
phone FROZEN<phone_type>,
email_verified BOOLEAN,
phone_verified BOOLEAN,
created_on TIMESTAMP,
PRIMARY KEY (userid)
);
-- get user by username
CREATE TABLE IF NOT EXISTS users.user_by_username(
userid TIMEUUID,
username TEXT,
password TEXT,
PRIMARY KEY (username)
);
-- get user by user phone number
-- can have same number for more than one account
CREATE TABLE IF NOT EXISTS users.user_by_phone(
userid TIMEUUID,
phone FROZEN<phone_type>,
password TEXT,
PRIMARY KEY (phone, userid)
);
-- get user by email
-- can have same email for more than one account
CREATE TABLE IF NOT EXISTS users.user_by_email(
userid TIMEUUID,
email TEXT,
password TEXT,
PRIMARY KEY (email, userid)
);
-- user history
CREATE TABLE IF NOT EXISTS users.user_history(
userid TIMEUUID,
name TEXT,
dob FROZEN<date_type>,
username TEXT,
phone FROZEN<phone_type>,
email TEXT,
phone_verified BOOLEAN,
email_verified BOOLEAN,
action TEXT,
date TIMESTAMP,
PRIMARY KEY (userid, date, action)
);
- 根据 cassandra 数据建模,以上 table 设计是否有效?
- 为什么因为如果我想创建一个用户,我必须插入 5 tables
- 如果我想更新电子邮件、phone 或密码,首先我必须删除 table 中的条目,然后插入更新后的值
Is above table design valid as per cassandra data modeling?
是的,根据 Cassandra 数据建模table 设计是有效的。
Why because if I want to create an user, I have to insert in 5 tables
这就是你在 Cassandra 中的做法,首先你有你想要满足的查询,然后根据它们设计你的 tables。
And if I want to update email, phone or password, first I have to delete entry in table then insert with updated values
只需更新值,无需先删除它们。但请确保更新每个 table.
中的相应条目