Lecture 8 - Wide Column Stores - Part 2
Lecture 8 - Wide Column Stores - Part 2
http://www.ksi.mff.cuni.cz/~svoboda/courses/241‐NIE‐PDB/
Lecture 9
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
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
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
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
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
NIE‐PDB: Advanced Database Systems | Lecture 9: Wide Column Stores: Cassandra | 19. 11. 2024 38
Selection
SELECT clause: selectors
column name
term
COUNT ( * )
• 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!
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!
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 .
, ,
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
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
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
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