· 7 years ago · Nov 05, 2018, 01:28 AM
1CREATE KEYSPACE IF NOT EXISTS instagram WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor' : 3}
2
3#composite partition key && clustering key
4
5CREATE TABLE IF NOT EXISTS instagram.user (
6 user_id uuid,
7 username text,
8 first_name text,
9 last_name text,
10 email text,
11 password text,
12 creation_date timestamp,
13 PRIMARY KEY (user_id)
14 );
15
16 # limit followers & subscription to prevent unbounded row growth
17
18 CREATE TABLE IF NOT EXISTS instagram.subscription (
19 user_id uuid,
20 tag text,
21 subscribe_date timestamp,
22 PRIMARY KEY (user_id)
23 ) WITH CLUSTERING ORDER BY (subscribe_date);
24
25 CREATE TABLE IF NOT EXISTS instagram.follows (
26 user_id uuid,
27 follows uuid,
28 their_username text, #could change
29 follow_date timestamp,
30 PRIMARY KEY (user_id)
31 ) WITH CLUSTERING ORDER BY (follow_date DESC)
32
33 CREATE TABLE IF NOT EXISTS instragram.followed_by (
34 user_id uuid,
35 followed_by uuid,
36 their_username text,
37 follow_date timestamp,
38 PRIMARY KEY (user_id)
39 ) WITH CLUSTERING ORDER BY (follow_date DESC)
40
41 CREATE TABLE IF NOT EXISTS instagram.user_post (
42 user_id uuid,
43 post_id timeuuid,
44 tag text, #joined with pipes "artsy|summer|test"
45 caption text,
46 PRIMARY KEY ((user_id), post_id)
47 ) WITH CLUSTERING ORDER BY (post_id DESC)
48
49CREATE TABLE IF NOT EXISTS instagram.user_post_tag (
50 user_id uuid,
51 post_id timeuuid,
52 tag text, # individual tag
53 caption text,
54 PRIMARY KEY((tag), post_id)
55 ) WITH CLUSTERING ORDER BY (post_id DESC)
56
57CREATE TABLE IF NOT EXISTS instagram.user_post_comment (
58 user_id uuid,
59 post_id timeuuid,
60 comment text,
61 PRIMARY KEY (post_id)
62) WITH CLUSTERING ORDER BY (post_id DESC)