Guest User

Untitled

a guest
Oct 29th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. # Creating a KEYSPACE with SimpleStrategy and a Replication Factor of 3.
  2. CREATE KEYSPACE shoutapp WITH REPLICATION = ('class' : 'SimpleStrategy', 'replication_factor' : 3);
  3. # Show keyspaces in cassandra.
  4. DESCRIBE KEYSPACES ;
  5. # Show a specified keyspace.
  6. DESCRIBE KEYSPACE shoutapp;
  7. # use a specific keyspace.
  8. USE shoutapp;
  9.  
  10. # Lets create Tables in SQL.
  11. CREATE TABLE users(username text PRIMARY KEY, password text, email text, name text);
  12.  
  13. # Gives all informations about a specific table.
  14. DESCRIBE TABLE users;
  15.  
  16. # Create the remaining Tables for the twitter similar app.
  17. CREATE TABLE following(username text PRIMARY KEY, followed text);
  18.  
  19. CREATE TABLE followers(username text PRIMARY KEY, following text);
  20.  
  21. CREATE TABLE shouts(shout_id uuid PRIMARY KEY, username text, body text);
  22.  
  23. CREATE TABLE usershouts(username text, shout_id timeuuid, body text, PRIMARY KEY (username, shout_id));
  24.  
  25. CREATE TABLE shoutwall (username text, shout_id timeuuid, posted_by text, body text, PRIMARY KEY (username,shout_id));
  26.  
  27. # See availabe tables.
  28. DESCRIBE TABLES;
  29.  
  30. # Inserting Data (A note here, password should be encrypted if its used for a real app ).
  31. INSERT INTO users (username, password) VALUES ('devuser1','pass1')
  32.  
  33. # Verifying Data.
  34. SELECT * FROM users;
  35. # We didn't specify mail and name, but we will add them later. Let's use a Batch Command.
  36.  
  37. BEGIN BATCH
  38. INSERT INTO users (username,email, name, password) VALUES ('devuser2','usr2@m.com','user2','pass2')
  39. INSERT INTO users (username,email, name, password) VALUES ('devuser3','usr3@m.com','user3','pass3')
  40. INSERT INTO users (username,email, name, password) VALUES ('devuser4','usr4@m.com','user4','pass4')
  41. APPLY BATCH;
  42.  
  43. # Verifying Data.
  44. SELECT * FROM users;
  45.  
  46. INSERT INTO following (username, followed) VALUES ('devuser1','devuser3');
  47.  
  48. # Show who a user is following.
  49. SELECT followed FROM following WHERE username = 'devuser1';
  50.  
  51. INSERT INTO followers (username, following) VALUES ('devuser3','devuser1');
  52.  
  53. # Show who is following a user.
  54. SELECT following FROM followers WHERE username = 'devuser3';
  55.  
  56. # now() gives a uuid based on TIMESTAMP to uniquely identefy a shout.
  57. INSERT INTO shouts (shout_id,username, body) VALUES (now(),'devuser1','Hello Cassandra !');
  58.  
  59. # Show shouts.
  60. SELECT * FROM shouts;
  61.  
  62. # DELETE a record.
  63. DELETE FROM shouts WHERE shout_id='...' ;
  64.  
  65. # Add multiple records to different tables.
  66. BEGIN BATCH
  67. INSERT INTO shouts (shout_id,username, body) VALUES (now(),'devuser2','Random Text')
  68. INSERT INTO usershouts (username,shout_id, body) VALUES ('devuser2',now(),'Random Text')
  69. INSERT INTO shoutwall (username,shout_id,posted_by body) VALUES ('devuser2',now(),'devuser1','Random')
  70. APPLY BATCH;
  71.  
  72. # Show shoutwall.
  73. SELECT * FROM shoutwall;
  74.  
  75. SELECT * FROM usershouts WHERE username = 'devuser2';
  76.  
  77. SELECT dateOf(shout_id), posted_by, body FROM shoutwall WHERE username = 'devuser1' AND shout_id > minTimeuuid(2013-01-01) ORDER BY shout_id DESC;
  78.  
  79. # UPDATING DATA.
  80.  
  81. UPDATE users SET email = 'dev1@m.com' WHERE username = 'devuser1';
  82. UPDATE users SET name = 'user1' WHERE username = 'devuser1';
  83.  
  84. # Check
  85. SELECT * FROM users WHERE username = 'devuser1';
  86.  
  87. # Adding Fields.
  88. ALTER TABLE users ADD age int;
  89.  
  90. UPDATE users SET age = 32 ZHERE username = 'devuser1';
  91. # Check
  92. SELECT * FROM users WHERE username = 'devuser1';
  93.  
  94. # Deleting Fields.
  95. ALTER TABLE users DROP age;
  96.  
  97. # this throws an error because we didn''t specify that password field is a primary key.
  98. SELECT * FROM users WHERE password = 'pass1';
  99.  
  100. # a fix for the previous problem.
  101. CREATE INDEX ON users(password);
  102.  
  103. SELECT * FROM users WHERE password = 'pass1';
Add Comment
Please, Sign In to add comment