Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

Assignment 4b

Bscs20064

cluster = Cluster(['127.0.0.1'], port=9042) This line creates a Cluster object,


which represents a connection to a Cassandra cluster. The Cluster constructor
takes a list of contact points (in this case, ['127.0.0.1']) and port number (in this
case, 9042). Session = cluster.connect(): This line creates a Session object,
which represents a connection to a keyspace within the Cassandra cluster. The
connect() method is called on the Cluster object to create the Session. And also
used try and catch method for exception handling if error is occur during
connection.
DESCRIBE keyspaces

In NoSQL databases, keyspaces are the top-level namespace or container that


holds one or more tables or column families.

SELECT * FROM system_schema.keyspaces


Query returns all keyspaces in a Cassandra cluster.

CREATE KEYSPACE IF NOT EXISTS demokeyspace WITH REPLICATION = { 'class' :


'SimpleStrategy', 'replication_factor' : 1 };

This CQL statement creates a new keyspace named 'demokeyspace' with


SimpleStrategy replication strategy and replication factor of 1, if it doesn't
already exist.

SELECT * FROM system_schema.keyspaces

Query returns all keyspaces in a Cassandra cluster.


ALTER KEYSPACE demokeyspace WITH REPLICATION = { 'class' :
'SimpleStrategy', 'replication_factor' : 2 };

This CQL statement modifies the replication settings of the 'demokeyspace'


keyspace, changing the replication factor to 2.

SELECT * FROM system_schema.keyspaces

Query returns all keyspaces in a Cassandra cluster.


USE demokeyspace
This CQL statement switches to the 'demokeyspace' keyspace, allowing
subsequent queries to operate on tables within that keyspace.

CREATE TABLE IF NOT EXISTS music_library (year int, artist_name text,


album_name text, PRIMARY KEY (year,artist_name));

This CQL statement creates a new table named 'music_library' in the current
keyspace, with columns for 'year', 'artist_name', and 'album_name', and a
composite primary key consisting of 'year' and 'artist_name'. It only creates the
table if it doesn't already exist.

select * from music_library WHERE YEAR=1970

This CQL query selects all rows from the 'music_library' table where the 'year' column equals
1970.

CREATE TABLE IF NOT EXISTS music_library (year int, artist_name text,


album_name text, PRIMARY KEY ((year,artist_name), album_name)) WITH
clustering ORDER BY(album_name DESC);
This CQL statement creates a new table named 'music_library' in the current
keyspace, with columns for 'year', 'artist_name', and 'album_name', and a
composite primary key consisting of 'year' and 'artist_name', and clustering
key of 'album_name'. The table is created only if it doesn't already exist and
the albums are sorted in descending order.

drop table if exists music_library;

Droping the table name music_library using drop

INSERT INTO music_library (year, artist_name, album_name)


VALUES (%s, %s, %s)

Inserting new record or value in the table.


SELECT * FROM music_library

Showing all table value table name music_library

ALTER TABLE music_library ADD sales int

Changing table by adding new column in table.

SELECT * FROM music_library


INSERT INTO music_library (year, artist_name, album_name, sales) VALUES
(?,?, ?, ?)
SELECT * FROM music_library

Adding value and showing table

select * from music_library WHERE year=1970 ALLOW FILTERING;


select * from music_library WHERE year=1970

Filtering table by taking year 1970 from table name music_library

select * from music_library WHERE artist_name='A' ALLOW FILTERING

select * from music_library WHERE artist_name='C' ALLOW FILTERING


Getting value of Artist name ‘C’ from table.

select * from music_library WHERE album_name='B'


Getting value of Artist name ‘B’ from table.

select * from music_library WHERE YEAR=1970 and artist_name='Sheharyar'

Getting value of Artist name ‘Sheharyar’ and year 1970 from table.

select * from music_library WHERE YEAR=2002 and artist_name='C' and


album_name = 'D'

Getting value of Artist name ‘C’ and ‘D’ and year 2002 from table.

CREATE INDEX indexname ON music_library (album_name);


This CQL statement creates a new index named 'indexname' on the
'album_name' column of the 'music_library' table. The index can speed up
queries that filter or sort on the 'album_name' column.

SELECT * FROM music_library ORDER BY YEAR


This CQL query selects all rows from the 'music_library' table and sorts them in
ascending order based on the 'year' column.

SELECT * FROM music_library WHERE YEAR IN (2001,2002)

This CQL query selects all rows from the 'music_library' table where the 'year'
column equals 2001 or 2002.

SELECT * FROM music_library WHERE YEAR IN (2001,2002) ORDER BY YEAR


DESC
This CQL query selects all rows from the 'music_library' table where the 'year'
column equals 2001 or 2002, and sorts them in descending order based on the
'year' column.

SELECT * FROM music_library WHERE YEAR IN (2001,2002) ORDER BY


ARTIST_NAME DESC

This CQL query selects all rows from the 'music_library' table where the
'Artist_name' column equals 2001 or 2002, and sorts them in descending order
based on the 'year' column.
SELECT * FROM music_library ORDER BY ALBUM_NAME
This CQL query selects all rows from the 'music_library' table and sorts them in
ascending order based on the 'album_name' column.

SELECT * FROM music_library WHERE YEAR = 2001 ORDER BY ALBUM_NAME


This CQL query selects all rows from the 'music_library' table and sorts them in
ascending order based on the 'album_name' column and year = 2001.

SELECT * FROM music_library WHERE YEAR = 2001 ORDER BY ARTIST_NAME


This CQL query selects all rows from the 'music_library' table and sorts them in
ascending order based on the 'album_name' column and year = 2001.

SELECT max(year) FROM music_library

Getting only max year and showing.

SELECT min(year) FROM music_library


Getting only min year and showing.
SELECT sum(sales) FROM music_library

Getting sum of sales column and showing.

SELECT avg(sales) FROM music_library


Getting avg of sales column and showing.

SELECT avg(sales) FROM music_library where year = 2001


Getting avg of sales column which have year is 2001 and showing.

SELECT avg(sales) FROM music_library where year in (2001,2002)ALLOW


FILTERING;

Getting avg of sales column which have year between 2001 and 2002 and showing.

UPDATE music_library SET sales=10000 WHERE year = 1970

Updating the sales value of year equal to 1970 by using update and set
command.

UPDATE music_library SET sales=10000 WHERE year = 1970 and artist_name


= 'The Beatles'
SELECT * FROM music_library ;

Showing updated table.

drop table music_library


Dropping table using Drop command.

"DROP KEYSPACE demokeyspace"

This CQL statement deletes the 'demokeyspace' keyspace and all tables, data,
and other objects within it. Use with caution as it deletes all the data in the
keyspace.

DESCRIBE keyspaces

This CQL statement lists all the keyspaces in the current Cassandra cluster.

-------------------------------END------------------------------------

You might also like