ABP W5-W6 Big Data Analytics Lab-CASSANDRA
ABP W5-W6 Big Data Analytics Lab-CASSANDRA
(A7902) (VCE-R21)
Week-5 Cassandra
Output: cqlsh:students>
Note: Cassandra converted the Students keyspace to lowercase as quotation marks were
not used.
Note: The output is a list of CQL commands with the help of which the table “student_info”
can be recreated.
2. INSERT: To insert data into the column family “student_info”. An insert writes one or
more columns to a record in Cassandra table atomically. An insert statement does not
return an output.
cqlsh:students> BEGIN BATCH
INSERT INTO student_info (RollNo,StudName,DateofJoining,LastExamPercent)
VALUES (1,'Michael Storm','2012-03-29', 69.6)
INSERT INTO student_info (RollNo,StudName,DateofJoining,LastExamPercent)
VALUES (2,'Stephen Fox','2013-02-27', 72.5)
INSERT INTO student_info (RollNo,StudName,DateofJoining,LastExamPercent)
VALUES (3,'David Flemming','2014-04-12', 81.7)
INSERT INTO student_info (RollNo,StudName,DateofJoining,LastExamPercent)
VALUES (4,'Ian String','2012-05-11', 73.4)
APPLY BATCH;
The above select statement retrieves data from the “student_info” table.
3.2. To view or display information of only those students records where the RollNo
column either has a value 1 or 2 or 3.
cqlsh:students> SELECT * FROM student_info WHERE RollNo IN (1,2,3);
3.3. To execute the query using the index defined on “studname” column.
cqlsh:students> CREATE INDEX ON student_info(studname)
cqlsh:students> SELECT * FROM student_info WHERE studname= 'Stephen Fox';
4.UPDATE: An update updates one or more column values for a given row to the Cassandra
table. It does not return anything.
4.1. To updatethe value held in the “StudName” column of the “student_info” column family
to “David Sheen” for the record where the RollNo column has value = 2.
cqlsh:students> UPDATE student_info SET StudName = 'David Sheen'
WHERE RollNo = 2;
cqlsh:students> SELECT * FROM student_info WHERE RollNo = 2;
5. DELETE: Delete statement removes one or more columns from one or more rows of a
Cassandra table or removes entire rows if no columns are specified.
5.1. To delete the column “LastExamPercent” from the “student_info” table for the record
where the RollNo = 2.
cqlsh:students> DELETE LastExamPercent FROM student_info WHERE RollNo= 2;
cqlsh:students> SELECT * FROM student_info WHERE RollNo = 2;
Week-6 Cassandra
Problem statement: Create a table “users” with columns “user_id” (as primary key),
“first_name”, “last_name” and “emails” column as set-collection. Insert the given values to
the table: 'AB', 'Albert', 'Baggins', {'[email protected]', '[email protected]'}. Add an element
to the emails set {'[email protected]'}. Retrieve all email addresses. Now, remove an
element from the set and view the record. Then remove all elements from the set and
display the table.
1.1. To create a table “users” with an “emails” column. The type of this column “emails” is
“set” collection.
cqlsh:students> CREATE TABLE users (
user_id text PRIMARY KEY,
first_name text,
last_name text,
emails set<text> );
1.2. To insert values into the “emails” column of the “users” table.
Note: Set values must be unique.
cqlsh:students> INSERT INTO users (user_id, first_name, last_name, emails)
VALUES ('AB', 'Albert', 'Baggins', {'[email protected]', '[email protected]'});
1.3 Add an element to a set using the UPDATE command and the addition (+) operator.
cqlsh:students> UPDATE users SET emails = emails + {'[email protected]'}
WHERE user_id = 'AB';
1.5. To remove an element from a set using the subtraction (-) operator.
cqlsh:students> UPDATE users SET emails = emails - {'[email protected]'}
WHERE user_id = 'AB';
cqlsh:students> SELECT * FROM users
1.6. To remove all elements from a set by using the UPDATE or DELETE statement.
cqlsh:students> UPDATE users SET emails = {} WHERE user_id = 'AB';
(OR)
cqlsh:students> DELETE emails FROM users WHERE user_id = 'AB';
cqlsh:students> SELECT * FROM users
6.2.2. Update the list column “top_places” with values ['Lonavla', 'Khandala'] in the “users”
table for user_id = ‘AB’.
cqlsh:students> UPDATE users SET top_places = ['Lonavla', 'Khandala']
WHERE user_id = 'AB';
cqlsh:students> SELECT * FROM users WHERE user_id = 'AB';
6.2.4. To append an element to the list by switching the order of the new element data and
the list name in the update command.
cqlsh:students> UPDATE users SET top_places = top_places + ['Tapola']
WHERE user_id = ‘AB’
cqlsh:students> SELECT * FROM users;
6.2.6.To remove an element from a list using the DELETE command and the list index
position in square brackets.
cqlsh:students> DELETE top_places[3] FROM users WHERE user_id = 'AB';
cqlsh:students> SELECT * FROM users;
6.3. MAP Collection: As the name implies, a map is used to map one thing to another. A
map is a pair of typed values. It is used to store timestamp related information. Each
element of the map is stored as a Cassandra column. Each element can be individually
queried, modified, and deleted. Objective:
6.3.2. To update “todo” values for the record for user (user_id = ‘AB’) in the “users” table.
cqlsh:students> UPDATE users SET todo = {‘2024-09-24’ : ‘Cassandra Session’ ,
‘2024-10-02’ : ‘MangoDB Session’ ,}
WHERE user_id = 'AB';
cqlsh:students> SELECT user_id, top_places FROM users WHERE user_id = 'AB';
6.3.3. To delete an element from the map using the DELETE command and enclosing the
timestamp of the element in square brackets.
cqlsh:students> DELETE todo['2024-09-24'] FROM users WHERE user_id = 'AB';
cqlsh:students> SELECT user_id, todo FROM users WHERE user_id = 'AB';
6.b) ALTER commands: to bring about changes to the structure of the table/column
family.
1. Create a table “sample” with columns “sample_id” and “sample_name”.
cqlsh:students> CREATE TABLE sample(
sample_id text, sample_name text,
PRIMARY KEY (sample_id) );
2. After the data type of the column “sample_id” is changed from text to integer, try
inserting a record as follows and observe the error message:
cqlsh:students> INSERT INTO sample(sample_id, sample_name)
VALUES( 'S102', 'Big Data');
4. Alter the data type of the “sample_id” column to varchar from integer.
cqlsh:students> ALTER TABLE sample ALTER sample_id TYPE varchar;
5. Check the records after the data type of “sample_id” has been changed to varchar from
integer.
cqlsh:students> SELECT * FROM sample;
Note: The request to drop the “sample_id” column from the table “sample” does not
succeed as it is the primary key column.
The above request succeeds. The table/column family no longer exists in the keyspace.