0% found this document useful (0 votes)
12 views25 pages

Lecture 8 - Wide Column Stores - Part 2

The document is a lecture on Wide Column Stores, specifically focusing on Apache Cassandra, covering its data model and query language. It details various DML statements such as SELECT, INSERT, UPDATE, and DELETE, along with their clauses and usage examples. The lecture also emphasizes the structure and limitations of queries in Cassandra, including filtering, grouping, and ordering of results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views25 pages

Lecture 8 - Wide Column Stores - Part 2

The document is a lecture on Wide Column Stores, specifically focusing on Apache Cassandra, covering its data model and query language. It details various DML statements such as SELECT, INSERT, UPDATE, and DELETE, along with their clauses and usage examples. The lecture also emphasizes the structure and limitations of queries in Cassandra, including filtering, grouping, and ordering of results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

NIE‐PDB: Advanced Database Systems

http://www.ksi.mff.cuni.cz/~svoboda/courses/241‐NIE‐PDB/

Lecture 9

Wide Column Stores: Cassandra


Martin Svoboda
[email protected]

19. 11. 2024

Charles University, Faculty of Mathematics and Physics


Czech Technical University in Prague, Faculty of Information Technology
Lecture Outline
Wide column stores
• Introduction
Apache Cassandra
• Data model
• Cassandra query language
DDL statements
DML statements

NIE‐PDB: Advanced Database Systems | Lecture 9: Wide Column Stores: Cassandra | 19. 11. 2024 2
DML Statements
Selection
SELECT statement
• Selects matching rows from a single table

SELECT clause FROM clause


WHERE clause

GROUP BY clause ORDER BY clause LIMIT clause

ALLOW FILTERING

NIE‐PDB: Advanced Database Systems | Lecture 9: Wide Column Stores: Cassandra | 19. 11. 2024 32
Selection
Clauses of SELECT statements
• SELECT – columns or values to appear in the result from single
table
• FROM – single table to be queried
• WHERE – filtering conditions to be applied on table rows
• GROUP BY – columns to be used for grouping of rows
• ORDER BY – criteria defining the order of rows in the result
• LIMIT – number of rows to be included in the result Example

SELECT id, title, actors


FROM movies
WHERE year = 2000 AND genres CONTAINS 'comedy'

NIE‐PDB: Advanced Database Systems | Lecture 9: Wide Column Stores: Cassandra | 19. 11. 2024 33
Selection
FROM clause
• Defines a single table to be queried
From the current / selected keyspace
• I.e. joining of multiple tables is not possible

FROM table name


keyspace name .

NIE‐PDB: Advanced Database Systems | Lecture 9: Wide Column Stores: Cassandra | 19. 11. 2024 34
Selection
WHERE clause
• One or more relations a row must satisfy
in order to be included in the query result
WHERE relation

AND

• Only simple conditions can be expressed and


not all relations are allowed, e.g.:
only primary key columns can be involved
unless secondary index structures exist
non‐equal relations on partition keys are not supported

NIE‐PDB: Advanced Database Systems | Lecture 9: Wide Column Stores: Cassandra | 19. 11. 2024 35
Selection
WHERE clause: relations
column name = term

( column name ) !=

, <

<=

=>

>

IN ( term )

CONTAINS term

CONTAINS KEY

NIE‐PDB: Advanced Database Systems | Lecture 9: Wide Column Stores: Cassandra | 19. 11. 2024 36
Selection
WHERE clause: relations
• Comparisons
=, !=, <, <=, =>, >
• IN
Returns true when the actual value is one of the enumerated
• CONTAINS
May only be used on collections (lists, sets, and maps)
Returns true when a collection contains a given element
• CONTAINS KEY
May only be used on maps
Returns true when a map contains a given key

NIE‐PDB: Advanced Database Systems | Lecture 9: Wide Column Stores: Cassandra | 19. 11. 2024 37
Selection
SELECT clause
• Defines columns or values to be included in the result
* = all the table columns
Aliases can be defined using AS

SELECT *
DISTINCT
selector
AS identifier

• DISTINCT – duplicate rows are removed

NIE‐PDB: Advanced Database Systems | Lecture 9: Wide Column Stores: Cassandra | 19. 11. 2024 38
Selection
SELECT clause: selectors
column name

term

COUNT ( * )

WRITETIME ( column name )

TTL ( column name )

• COUNT(*)
Number of all the rows in a group (see aggregation)
• WRITETIME and TTL
Selects modification timestamp / remaining time‐to‐live
of a given column
Cannot be used on collections and their elements
Cannot be used in other clauses (e.g. WHERE)

NIE‐PDB: Advanced Database Systems | Lecture 9: Wide Column Stores: Cassandra | 19. 11. 2024 39
Selection
ORDER BY clause
• Defines the order of rows returned in the query result
• Only orderings induced by clustering columns are allowed!

ORDER BY column name


ASC

DESC

LIMIT clause
• Limits the number of rows returned in the query result
LIMIT integer

NIE‐PDB: Advanced Database Systems | Lecture 9: Wide Column Stores: Cassandra | 19. 11. 2024 40
Selection
GROUP BY clause
• Groups rows of a table according to certain columns
• Only groupings induced by primary key columns are allowed!

GROUP BY column name

• When a non‐grouping column would be accessed directly in


the SELECT clause (i.e. without being wrapped by an aggregate
function), the first value encountered will always be returned

NIE‐PDB: Advanced Database Systems | Lecture 9: Wide Column Stores: Cassandra | 19. 11. 2024 41
Selection
GROUP BY clause: aggregates
• Native aggregates
COUNT(column)
– Number of all the values in a given column
– null values are ignored
MIN(column), MAX(column)
– Minimal / maximal value in a given column
SUM(column)
– Sum of all the values in a given column
AVG(column)
– Average of all the values in a given column
• User‐defined aggregates

NIE‐PDB: Advanced Database Systems | Lecture 9: Wide Column Stores: Cassandra | 19. 11. 2024 42
Selection
ALLOW FILTERING modifier
• By default, only non‐filtering queries are allowed
I.e. queries where
the number of rows read ∼ the number of rows returned
Such queries have predictable performance
– They will execute in a time that is proportional
to the amount of data returned
• ALLOW FILTERING enables (some) filtering queries

NIE‐PDB: Advanced Database Systems | Lecture 9: Wide Column Stores: Cassandra | 19. 11. 2024 43
Insertions
INSERT statement
• Inserts a new row into a given table
When a row with a given primary key already exists,
it is updated
• Values of at least primary key columns must be set
• Names of columns must always be explicitly enumerated
INSERT INTO table name
keyspace name .

( column name ) VALUES ( term )

, ,

IF NOT EXISTS USING update parameters

NIE‐PDB: Advanced Database Systems | Lecture 9: Wide Column Stores: Cassandra | 19. 11. 2024 44
Insertions
Example
INSERT INTO movies (id, title, director, year, actors, genres)
VALUES (
'stesti',
'Štěstí',
('Bohdan', 'Sláma'),
2005,
{ 'vilhelmova': 'Monika', 'liska': 'Toník' },
[ 'comedy', 'drama' ]
)
USING TTL 86400

NIE‐PDB: Advanced Database Systems | Lecture 9: Wide Column Stores: Cassandra | 19. 11. 2024 45
Updates
UPDATE statement
• Updates existing rows within a given table
When a row with a given primary key does not yet exist,
it is inserted
• At least all primary key columns must be specified
in the WHERE clause
UPDATE table name
keyspace name .

SET assignment
USING update parameters
,

WHERE clause

NIE‐PDB: Advanced Database Systems | Lecture 9: Wide Column Stores: Cassandra | 19. 11. 2024 46
Updates
UPDATE statement: assignments
• Describe modifications to be applied
• Allowed assignments:
Value of a whole column is replaced
Value of a list or map element is replaced
– Items of lists are numbered starting with 0
Value of a user‐defined type field is replaced

column name = term

column name [ term ]

column name . field name

NIE‐PDB: Advanced Database Systems | Lecture 9: Wide Column Stores: Cassandra | 19. 11. 2024 47
Updates
Examples
UPDATE movies
SET
year = 2006,
director = ('Jan', 'Svěrák'),
actors = { 'machacek': 'Robert Landa', 'sverak': 'Josef Tkaloun' },
genres = [ 'comedy' ],
countries = { 'CZ' }
WHERE id = 'vratnelahve'

UPDATE movies
SET
actors['vilhelmova'] = 'Helenka',
genres[1] = 'comedy',
properties.length = 99
WHERE id = 'vratnelahve'

NIE‐PDB: Advanced Database Systems | Lecture 9: Wide Column Stores: Cassandra | 19. 11. 2024 48
Updates
Examples: modification of collection elements
UPDATE movies
SET
actors = actors + { 'vilhelmova': 'Helenka' },
genres = [ 'drama' ] + genres,
countries = countries + { 'SK' }
WHERE id = 'vratnelahve'

UPDATE movies
SET
actors = actors - { 'vilhelmova', 'landovsky' },
genres = genres - [ 'drama', 'sci-fi' ],
countries = countries - { 'SK' }
WHERE id = 'vratnelahve'

NIE‐PDB: Advanced Database Systems | Lecture 9: Wide Column Stores: Cassandra | 19. 11. 2024 49
Insertions and Updates
Update parameters
• TTL: time‐to‐live
0, null or simply missing for persistent values
• TIMESTAMP: writetime
TIMESTAMP integer

TTL

AND

• Only newly inserted / updated values are really affected

NIE‐PDB: Advanced Database Systems | Lecture 9: Wide Column Stores: Cassandra | 19. 11. 2024 50
Deletions
DELETE statement
• Removes the matching rows /
Preserves these rows but removes the selected columns /
Preserves these columns but removes elements of collections
or fields of UDT values
DELETE
column name

column name [ term ]

column name . field name

FROM clause WHERE clause

NIE‐PDB: Advanced Database Systems | Lecture 9: Wide Column Stores: Cassandra | 19. 11. 2024 51
Lecture Conclusion
Cassandra
• Wide column store
Cassandra query language
• DDL statements
• DML statements
SELECT, INSERT, UPDATE, DELETE

NIE‐PDB: Advanced Database Systems | Lecture 9: Wide Column Stores: Cassandra | 19. 11. 2024 53

You might also like