0% found this document useful (0 votes)
158 views

Presentation Cassandra Datastax

The document shows a table with information about cities, their countries, and populations. It demonstrates partitioning the table by country to distribute the data across multiple nodes in a Cassandra cluster.

Uploaded by

AMAR BESSALAH
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
158 views

Presentation Cassandra Datastax

The document shows a table with information about cities, their countries, and populations. It demonstrates partitioning the table by country to distribute the data across multiple nodes in a Cassandra cluster.

Uploaded by

AMAR BESSALAH
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 151

󾓧 󾓪

󾓦 󾓬
YOUR LAPTOP

Browser WebConf

Github
YOUR LAPTOP

Browser Webconf
1. Clone or download repository material

https://github.com/DataStax-Acade
my/cassandra-workshop-online

https://bit.ly/cassandra-cert-FREE
Familiar Language Easy Dev Tools Great Drivers
INSERT INTO mytable
(id,name,address) VALUES
(1,’Bob Smith’,’1 Main
Street’)
SELECT * FROM mytable
WHERE id=1
UPDATE mytable SET
name=’Tom Smith’ WHERE
id=1
DELETE FROM mytable WHERE
id=1
Apache Cassandra™ = NoSQL Distributed Database
1 Installation = 1 NODE
NODE ✔ Capacity: ± 1TB
✔ Throughput: 3000 Tx/sec/core
NODE NODE

DataCenter | Ring

NODE NODE
Communication:
✔ Gossiping

NODE NODE
Country City Population

USA New York 8.000.000


USA Los Angeles 4.000.000
FR Paris 2.230.000
DE Berlin 3.350.000
UK London 9.200.000
AU Sydney 4.900.000
DE Nuremberg 500.000
CA Toronto 6.200.000
CA Montreal 4.200.000
FR Toulouse 1.100.000
JP Tokyo 37.430.000
IN Mumbai 20.200.000

Partition Key
USA New York 8.000.000
USA Los Angeles 4.000.000 Country City Population

FR Paris 2.230.000
DE Berlin 3.350.000
UK London 9.200.000
AU Sydney 4.900.000
DE Nuremberg 500.000
CA Toronto 6.200.000
CA Montreal 4.200.000
FR Toulouse 1.100.000
JP Tokyo 37.430.000
IN Mumbai 20.200.000

Partition Key
USA New York 8.000.000
USA Los Angeles 4.000.000 Country City Population

FR Paris 2.230.000
DE Berlin 3.350.000
UK London 9.200.000
AU Sydney 4.900.000
DE Nuremberg 500.000
CA Toronto 6.200.000
CA Montreal 4.200.000
FR Toulouse 1.100.000
JP Tokyo 37.430.000
IN Mumbai 20.200.000

Partition Key
USA New York 8.000.000
USA Los Angeles 4.000.000 Country City Population

FR Paris 2.230.000
DE Berlin 3.350.000

UK London 9.200.000
AU Sydney 4.900.000
DE Nuremberg 500.000
CA Toronto 6.200.000
CA Montreal 4.200.000
FR Toulouse 1.100.000
JP Tokyo 37.430.000
IN Mumbai 20.200.000

Partition Key
USA New York 8.000.000
USA Los Angeles 4.000.000 Country City Population

FR Paris 2.230.000
DE Berlin 3.350.000

AU Sydney 4.900.000
DE Nuremberg 500.000
CA Toronto 6.200.000
UK London 9.200.000 CA Montreal 4.200.000
FR Toulouse 1.100.000
JP Tokyo 37.430.000
IN Mumbai 20.200.000

Partition Key
USA New York 8.000.000
USA Los Angeles 4.000.000 Country City Population

FR Paris 2.230.000
DE Berlin 3.350.000

DE Nuremberg 500.000
CA Toronto 6.200.000
UK London 9.200.000 CA Montreal 4.200.000
FR Toulouse 1.100.000
JP Tokyo 37.430.000
IN Mumbai 20.200.000

AU Sydney 4.900.000
Partition Key
USA New York 8.000.000
USA Los Angeles 4.000.000 Country City Population

FR Paris 2.230.000
DE Berlin 3.350.000
DE Nuremberg 500.000

CA Toronto 6.200.000
UK London 9.200.000 CA Montreal 4.200.000
FR Toulouse 1.100.000
JP Tokyo 37.430.000
IN Mumbai 20.200.000

AU Sydney 4.900.000
Partition Key
USA New York 8.000.000
USA Los Angeles 4.000.000 Country City Population

FR Paris 2.230.000
DE Berlin 3.350.000
DE Nuremberg 500.000

UK London 9.200.000
FR Toulouse 1.100.000
JP Tokyo 37.430.000
IN Mumbai 20.200.000

AU Sydney 4.900.000 CA Toronto 6.200.000


Partition Key
CA Montreal 4.200.000
USA New York 8.000.000
USA Los Angeles 4.000.000 Country City Population

FR Paris 2.230.000
DE Berlin 3.350.000
FR Toulouse 1.100.000
DE Nuremberg 500.000

UK London 9.200.000

JP Tokyo 37.430.000
IN Mumbai 20.200.000

AU Sydney 4.900.000 CA Toronto 6.200.000


Partition Key
CA Montreal 4.200.000
USA New York 8.000.000
USA Los Angeles 4.000.000 Country City Population

FR Paris 2.230.000
DE Berlin 3.350.000
FR Toulouse 1.100.000
DE Nuremberg 500.000

UK London 9.200.000 JP Tokyo 37.430.000

IN Mumbai 20.200.000

AU Sydney 4.900.000 CA Toronto 6.200.000


Partition Key
CA Montreal 4.200.000
USA New York 8.000.000
USA Los Angeles 4.000.000 Country City Population

FR Paris 2.230.000
DE Berlin 3.350.000
FR Toulouse 1.100.000
DE Nuremberg 500.000

UK London 9.200.000 JP Tokyo 37.430.000

AU Sydney 4.900.000 CA Toronto 6.200.000


IN Mumbai 20.200.000 CA Montreal 4.200.000
A-E

F-G
X-Z

H-K
U-W

Q-T L-P
CO City Population CO City Population
AU Sydney 4.900.000 59 Sydney 4.900.000

CA Toronto 6.200.000 12 Toronto 6.200.000


Partitioner
CA Montreal 4.200.000 12 Montreal 4.200.000
Hashing Function
DE Berlin 3.350.000 45 Berlin 3.350.000
DE Nuremberg 500.000 45 Nuremberg 500.000

Partition Key Tokens

A-E
VIRTUAL NODE
AP
- - - -
- - -
x
- - -

- - -
y - - -
- - -

z - - -
CREATE KEYSPACE users
WITH REPLICATION = {
'class' : 'NetworkTopologyStrategy',
'datacenter1' : 3
};
CREATE TABLE users.users_by_city (
city text,
last_name text,
first_name text,
address text,
email text,
PRIMARY KEY ((city), last_name, first_name));
Data

Models

Application
Application

Models

Data
Entities & Relationships

Queries
R1: Find comments related to target video using its identifier
• Get most recent first
• Implement Paging

R2: Find comments related to target user using its identifier


• Get most recent first
• Implement Paging

R3: Implement CRUD operations


Q1: Find comments for a video with a comments_by_video
known id (show most recent first)

Q2: Find comments posted for a user with a


comments_by_user
known id (show most recent first)

Q3: CRUD Operations


comments_by_user comments_by_video

↑ ↑

↑ ↑
comments_by_user comments_by_video

UUID UUID
↑ ↑
TIMEUUID TIMEUUID
UUID UUID
TEXT TEXT
CREATE TABLE IF NOT EXISTS comments_by_user (
userid uuid,
commentid timeuuid,
videoid uuid,
comment text,
PRIMARY KEY ((userid), commentid)
) WITH CLUSTERING ORDER BY (commentid DESC);

CREATE TABLE IF NOT EXISTS comments_by_video (


videoid uuid,
commentid timeuuid,
userid uuid,
comment text,
PRIMARY KEY ((videoid), commentid)
) WITH CLUSTERING ORDER BY (commentid DESC);
SELECT * FROM comments_by_user
WHERE userid = <some UUID>

SELECT * FROM comments_by_video


WHERE videoid = <some UUID>
󾓧 󾓪

󾓦 󾓬
I’m an ordered LIST

0 1 2 3 4 6

I’m a MAP of
key/value pairs
K1 V1
K2 V2
K3 V3
K4 V4
K5 V5
Insert

Replace entire list

Append
Replace an element

Note: replacing an element requires a read-before-write, which


implies performance penalty.
Insert

Replace entire map

Add to map
Insert

Replace entire UDT

Replace one UDT field


Select field












This format must be
observed
Incrementing a counter:
This can be an
integer value

Just change the


sign
Decrementing a counter:
INSERT INTO one_table …
INSERT INTO another_table...
INSERT INTO yet_another_table...
BEGIN BATCH

INSERT INTO one_table …


INSERT INTO another_table...
INSERT INTO yet_another_table...

APPLY BATCH;
N o !
BEGIN BATCH

INSERT INTO one_table …


INSERT INTO another_table...
INSERT INTO yet_another_table...

APPLY BATCH;


proxies proxies proxies

Loadbalanced url
// Delegate all configuration to file or default
CqlSession cqlSession = CqlSession.builder().build();

// Explicit Settings
CqlSession cqlSession = CqlSession.builder()
//.addContactPoint(new InetSocketAddress("127.0.0.1", 9042))
.withCloudSecureConnectBundle(Paths.get("/tmp/apollo.zip"))
.withContactPoint
.withKeyspace("killrvideo")
.withAuthCredentials("KVUser", "KVPassword")
.build();
@PreDestroy
public void cleanup() {
if (null != cqlSession) {
cqlSession.close();
}
}
cqlSession.execute("SELECT * FROM killrvideo.users");
Statement statement = …

// (1) Explicit SimpleStatement Definition


SimpleStatement.newInstance("select * from t1 where c1 = 5");

// (2) Externalize Parameters (no name)


SimpleStatement.builder("select * from t1 where c1 = ?")
.addPositionalValue(5);

// (3) Externalize Parameters (name)


SimpleStatement.builder("select * from t1 where c1 = :myVal")
.addNamedValue("myVal”, 5);

cqlSession.execute(statement);
PreparedStatement ps = cqlSession.prepare("SELECT * from t1 where c1 = ?");

BoundStatement bound = ps.bind(5);

cqlSession.execute(bound);
ResultSet rs = cqlSession.execute(myStatement);

// Plumbery
ExecutionInfo info = rs.getExecutionInfo();
int executionTime = info.getQueryTrace().getDurationMicros();

// Data: NOT ALL DATA RETRIEVED IMMEDIATELY (only when needed .next())
Iterator<Row> iterRow = rs.iterator();
int itemsFirstCall = rs.getAvailableWithoutFetching();
// We know there is a single row (eg: count)
Row singleRow = resultSet.one();

// We know there are not so many results we can get all (fetch all pages)
List<Row> allRows = resultSet.all();

// Browse iterable
for(Row myRow : resultSet.iterator()) {
// .. Parsing rows
}

// Use Lambda
rs.forEach(row -> { row.getColumnDefinitions(); });

// Use for LWT


boolean isQueryExecuted = rs.wasApplied();
// Sample row
Row row = resultSet.one();

// Check null before read


Boolean isUsernNameNull = row.isNull("userName");

// Reading Values from row


String userName1 = row.get("username", String.class);
String userName2 = row.getString("username");
String userName3 = row.getString(CqlIdentifier.fromCql("username"));

// Tons of types available


row.getUuid("userid");
row.getBoolean(”register");
row.getCqlDuration(”elapsed");

// Enforce few items per page (often = UI requirements)
myStatement = myStatement.setPageSize(10);
ResultSet page1 = cqlSession.execute(myStatement);

// Paging State
ByteBuffer pagingState = page1.getExecutionInfo().getPagingState();
myStatement = myStatement.setPageState(pagingState);

// Very same statement with pagingState provided


ResultSet page2 = cqlSession.execute(myStatement);
Parameters

PreparedStatement
& Parameters
Bind

😴 Parameters

BoundStatement

ResultSet
ResultSet
Results
API

Parameters
v
PreparedStatement
& Parameters Bind
Parameters

BoundStatement

CompletionStage

AsyncResultSet
AsyncResultSet
Result
Parameters
v
PreparedStatement
& Parameters Bind
Parameters
Flux ReactiveResultSet BoundStatement

Subscribe
Query
execution
Row
ReactiveRow
Subscriber.onNext

onComplete()
community.datastax.com
Date Time Content Type

You might also like