Más contenido relacionado La actualidad más candente (20) Similar a ChtiJUG - Cassandra 2.0 (20) Más de Michaël Figuière (19) ChtiJUG - Cassandra 2.06. Client / Server Communication
Client
?
Node
Replica
Client
Node
Replica
Client
Client
©2012 DataStax
Node
Replica
6
7. Client / Server Communication
Client
Node
Replica
Client
Node
Replica
Client
Client
Node
Replica
Coordinator node:
Forwards all R/W requests
to corresponding replicas
©2012 DataStax
7
10. Tunable Consistency
Time
R +W < N
A
A
A
B
A
A
B
A
A
Read waiting for one node
to answer
©2012 DataStax
Write and wait for
acknowledge from one node
10
11. Tunable Consistency
Time
R +W = N
A
A
A
B
B
A
B
B
A
Read waiting for one node
to answer
©2012 DataStax
Write and wait for
acknowledges from two nodes
11
12. Tunable Consistency
Time
R +W > N
A
A
A
B
B
A
B
B
A
Read waiting for two nodes
to answer
©2012 DataStax
Write and wait for
acknowledges from two nodes
12
17. CQL Denormalized Model
Cassandra comes with a Data Model abstraction made of denormalized,
statically defined tables
Data duplicated over several tables
©2012 DataStax
17
18. Cassandra Data Model
Timeline Table
user_id
tweet_id
author
gmason
1735
phenry
Give me liberty or give me death
gmason
1742
gwashington
I chopped down the cherry tree
ahamilton
1767
jadams
A government of laws, not men
ahamilton
1794
gwashington
I chopped down the cherry tree
Partition
Key
Clustering
Key
©2012 DataStax
body
18
19. Cassandra Data Model
Timeline Table
user_id
tweet_id
author
body
gmason
1735
phenry
Give me liberty or give me death
gmason
1742
gwashington
I chopped down the cherry tree
ahamilton
1767
jadams
A government of laws, not men
ahamilton
1794
gwashington
I chopped down the cherry tree
CQL
CREATE TABLE timeline (
user_id varchar,
tweet_id timeuuid,
author varchar,
body varchar,
PRIMARY KEY (user_id, tweet_id));
©2012 DataStax
19
20. Cassandra Data Model
Timeline Table
user_id
tweet_id
author
body
gmason
1735
phenry
Give me liberty or give me death
gmason
1742
gwashington
I chopped down the cherry tree
ahamilton
1767
jadams
A government of laws, not men
ahamilton
1794
gwashington
I chopped down the cherry tree
Timeline Physical Layout
gmason
ahamilton
©2012 DataStax
[1735, author]
[1735, body]
[1742, author]
[1742, body]
gwashington
I chopped down the...
phenry
Give me liberty or give...
[1767, author]
[1767, body]
[1794, author]
[1794, body]
gwashington
I chopped down the...
jadams
A government of laws...
20
21. Example: Video DB Application
CREATE TABLE users (
username varchar,
firstname varchar,
lastname varchar,
email list<varchar>,
password varchar,
created_date timestamp,
PRIMARY KEY (username)
);
©2012 DataStax
21
22. Example: Video DB Application
CREATE TABLE videos (
videoid uuid,
videoname varchar,
username varchar,
description varchar,
location map<varchar,varchar>,
tags set<varchar>,
upload_date timestamp,
PRIMARY KEY (videoid)
);
©2012 DataStax
22
23. Example: Video DB Application
CREATE TABLE comments_by_video (
videoid uuid,
username varchar,
comment_ts timeuuid,
comment varchar,
PRIMARY KEY (videoid,comment_ts,username)
) WITH CLUSTERING ORDER BY (comment_ts DESC,
username ASC);
©2012 DataStax
23
24. Example: Video DB Application
CREATE TABLE comments_by_user (
username varchar,
videoid uuid,
comment_ts timeuuid,
comment varchar,
PRIMARY KEY (username,comment_ts,videoid)
) WITH CLUSTERING ORDER BY (comment_ts DESC,
videoid ASC);
©2012 DataStax
24
25. Example: Video DB Application
CREATE TABLE video_rating (
videoid uuid,
rating_counter counter,
rating_total counter,
PRIMARY KEY (videoid)
);
©2012 DataStax
25
27. DataStax Java Driver
• Reference Implementation
• Asynchronous architecture based on Netty
• Prepared Statements Support
• Automatic Fail-over
• Node Discovery
• Cassandra Tracing Support
• Tunable policies
•
•
•
©2012 DataStax
LoadBalancingPolicy
ReconnectionPolicy
RetryPolicy
27
28. Connect and Write
Cluster cluster = new Cluster.builder()
.addContactPoints("10.0.0.1", "10.0.0.2")
.build();
Session session = cluster.connect("myKeyspace");
session.execute(
"INSERT INTO user (user_id, name, email)
VALUES (12345, 'johndoe', 'john@doe.com')"
);
©2012 DataStax
28
29. Read
ResultSet rs = session.execute("SELECT * FROM test");
List<Row> rows = rs.all();
for (Row row : rows) {
String userId = row.getString("user_id");
String name = row.getString("name");
String email = row.getString("email");
}
©2012 DataStax
29
30. Asynchronous Read
ResultSetFuture future =
session.executeAsync("SELECT * FROM test");
for (Row row : future.get()) {
String userId = row.getString("user_id");
String name = row.getString("name");
String email = row.getString("email");
}
©2012 DataStax
30
31. Read with Callback
final ResultSetFuture future =
session.executeAsync("SELECT * FROM test");
future.addListener(new Runnable() {
public run() {
for (Row row : future.get()) {
String userId = row.getString("user_id");
String name = row.getString("name");
String email = row.getString("email");
}
}
}, executor);
©2012 DataStax
31
32. Lightweight Transactions
Session 1
SELECT * FROM users
WHERE username = ’jbellis’
[empty resultset]
INSERT INTO users (...)
VALUES (’jbellis’, ...)
©2012 DataStax
Session 2
SELECT * FROM users
WHERE username = ’jbellis’
[empty resultset]
INSERT INTO users (...)
VALUES (’jbellis’, ...)
32
34. Lightweight Transactions
INSERT INTO USERS (username, email, ...)
VALUES (‘jbellis’, ‘jbellis@datastax.com’, ... )
IF NOT EXISTS;
UPDATE USERS
SET email = ’jonathan@datastax.com’, ...
WHERE username = ’jbellis’
IF email = ’jbellis@datastax.com’;
©2012 DataStax
34
35. Paging with Cassandra 1.2
CREATE TABLE timeline (
user_id uuid,
tweet_id timeuuid,
tweet_author uuid,
tweet_body text,
PRIMARY KEY (user_id, tweet_id)
);
©2012 DataStax
SELECT *
FROM timeline
WHERE (user_id = :last_key
AND tweet_id > :last_tweet)
OR token(user_id) >
token(:last_key)
LIMIT 100
35