ASSIGNMENT – 01
TASKS 1:Export and Import data
Part 1: Setting up the Database
1. Create a Keyspace retail_store with Replication Strategy SimpleStrategy and
Replication Factor 1
CREATE KEYSPACE retail_store
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};
2. Create a Table customer_transactions with the Required Attributes
USE retail_store;
CREATE TABLE customer_transactions (
transaction_id UUID PRIMARY KEY,
customer_name TEXT,
product_name TEXT,
quantity INT,
price DECIMAL,
purchase_date TIMESTAMP
);
3. Insert 5 Records into the customer_transactions Table
INSERT INTO customer_transactions (transaction_id, customer_name, product_name,
quantity, price, purchase_date)
VALUES (uuid(), 'John Doe', 'Laptop', 1, 1200.50, '2025-03-01 10:00:00');
INSERT INTO customer_transactions (transaction_id, customer_name, product_name,
quantity, price, purchase_date)
VALUES (uuid(), 'Jane Smith', 'Phone', 2, 800.75, '2025-03-02 14:30:00');
INSERT INTO customer_transactions (transaction_id, customer_name, product_name,
quantity, price, purchase_date)
VALUES (uuid(), 'Sam Green', 'Tablet', 1, 500.00, '2025-03-03 16:45:00');
INSERT INTO customer_transactions (transaction_id, customer_name, product_name,
quantity, price, purchase_date)
VALUES (uuid(), 'Anna White', 'Monitor', 3, 300.99, '2025-03-04 09:20:00');
INSERT INTO customer_transactions (transaction_id, customer_name, product_name,
quantity, price, purchase_date)
VALUES (uuid(), 'Tom Black', 'Keyboard', 5, 60.25, '2025-03-05 13:10:00');
Part 2: Exporting Data from Cassandra
1. Export Data from customer_transactions Table to a CSV File
COPY retail_store.customer_transactions TO 'customer_transactions.csv';
2. Verify the Data in the CSV File
Once the above command is executed, you will have a customer_transactions.csv file
generated in your current working directory. Open the file to verify that it contains the
correct data.
Part 3: Importing Data into Another Cassandra Instance
1. Create a New Keyspace analytics_store in Another Cassandra Cluster (or same
instance)
CREATE KEYSPACE analytics_store
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};
2. Create a Similar Table Structure in analytics_store
USE analytics_store;
CREATE TABLE customer_transactions (
transaction_id UUID PRIMARY KEY,
customer_name TEXT,
product_name TEXT,
quantity INT,
price DECIMAL,
purchase_date TIMESTAMP
);
3. Import the Exported CSV File into the New Table Using the COPY Command
COPY analytics_store.customer_transactions FROM 'customer_transactions.csv';
4. Verify the Imported Data Using the SELECT Statement
SELECT * FROM analytics_store.customer_transactions;
Tasks 2: Data Definition Commands
1. Create a Keyspace university_db with SimpleStrategy Replication and
Replication Factor 2
CREATE KEYSPACE university_db
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 2};
Show the keyspace creation output: After executing the above command, it will
confirm the creation of the keyspace in the terminal.
2. Set university_db as the Active Keyspace for All Subsequent Operations
USE university_db;
3. Create a students Table with the Given Fields
CREATE TABLE students (
student_id UUID PRIMARY KEY,
name TEXT,
age INT,
email TEXT,
course TEXT
);
4. Modify the students Table by Adding a New Column phone_number
ALTER TABLE students ADD phone_number TEXT;
5. Rename the Column course to department
ALTER TABLE students RENAME course TO department;
6. Remove the Column phone_number from the students Table
ALTER TABLE students DROP phone_number;
7. Create an Index on the email Column to Improve Search Performance
CREATE INDEX email_index ON students (email);
8. Drop the email_index from the students Table
DROP INDEX email_index;
9. Drop the students Table
DROP TABLE students;
10. Drop the university_db Keyspace
DROP KEYSPACE university_db;