In this article, you will learn Cassandra commands with CQL examples-
The Cassandra insert statement writes data in Cassandra columns in row form. Cassandra insert query will store only those columns that are given by the user. You have to necessarily specify just the primary key column.
It will not take any space for not given values. No results are returned after insertion.
Insert into KeyspaceName.TableName(ColumnName1, ColumnName2, ColumnName3 . . . .) values (Column1Value, Column2Value, Column3Value . . . .)
Here is the snapshot of the executed Cassandra Insert into table query that will insert one record in Cassandra table ‘Student’.
Insert into University.Student(RollNo,Name,dept,Semester) values(2,'Michael','CS', 2);
After successful execution of the command Insert into Cassandra, one row will be inserted in the Cassandra table Student with RollNo 2, Name Michael, dept CS and Semester 2.
Here is the snapshot of the current database state.
Cassandra does upsert. Upsert means that Cassandra will insert a row if a primary key does not exist already otherwise if primary key already exists, it will update that row.
The Cassandra Update query is used to update the data in the Cassandra table. If no results are returned after updating data, it means data is successfully updated otherwise an error will be returned. Column values are changed in ‘Set’ clause while data is filtered with ‘Where’ clause.
Update KeyspaceName.TableName Set ColumnName1=new Column1Value, ColumnName2=new Column2Value, ColumnName3=new Column3Value, . . . Where ColumnName=ColumnValue
Here is the screenshot that shows the database state before updating data.
Here is the snapshot of the executed Cassandra Update command that updates the record in the Student table.
Update University.Student Set name='Hayden' Where rollno=1;
After successful execution of the update query in Cassandra ‘Update Student’, student name will be changed from ‘Clark’ to ‘Hayden’ that has rollno 1.
Here is the screenshot that shows the database state after updating data.
Command ‘Delete’ removes an entire row or some columns from the table Student. When data is deleted, it is not deleted from the table immediately. Instead deleted data is marked with a tombstone and are removed after compaction.
Delete from KeyspaceName.TableName Where ColumnName1=ColumnValue
The above Cassandra delete row syntax will delete one or more rows depend upon data filtration in where clause.
Delete ColumnNames from KeyspaceName.TableName Where ColumnName1=ColumnValue
The above syntax will delete some columns from the table.
Here is the snapshot that shows the current database state before deleting data.
Here is the snapshot of the command that will remove one row from the table Student.
Delete from University.Student where rollno=1;
After successful execution of the CQL Delete command, one rows will be deleted from the table Student where rollno value is 1.
Here is the snapshot that shows the database state after deleting data.
There are following limitations in Cassandra query language (CQL).
- CQL does not support aggregation queries like max, min, avg
- CQL does not support group by, having queries.
- CQL does not support joins.
- CQL does not support OR queries.
- CQL does not support wildcard queries.
- CQL does not support Union, Intersection queries.
- Table columns cannot be filtered without creating the index.
- Greater than (>) and less than (<) query is only supported on clustering column.
Cassandra query language is not suitable for analytics purposes because it has so many limitations.
In Cassandra, data retrieval is a sensitive issue. The column is filtered in Cassandra by creating an index on non-primary key columns.
Select ColumnNames from KeyspaceName.TableName Where ColumnName1=Column1Value AND ColumnName2=Column2Value AND . . .
- Here is the snapshot that shows the data retrieval from Student table without data filtration.
select * from University.Student;
Two records are retrieved from Student table.
- Here is the snapshot that shows the data retrieval from Student with data filtration. One record is retrieved.
Data is filtered by name column. All the records are retrieved that has name equal to Guru99.
select * from University.Student where name='Guru99';