Más contenido relacionado Más de DataStax Academy (20) NYC* Tech Day - New Cassandra Drivers in Depth2. Speaker
Michaël Figuière
@mfiguiere
©2012 DataStax 2
3. CQL: the new face of Cassandra
• CQL 3
• Simpler Data Model using denormalized Tables
• SQL-like query language
• Schema definition
• CQL Binary Protocol
• Introduced in Cassandra 1.2 (CASSANDRA-2478)
• Designed for CQL 3
• Opens doors for Streaming, Notifications, ...
• Thrift will keep being supported by Cassandra
©2012 DataStax 3
4. CQL3 Denormalized Model
CQL3 Query Language comes with a new Data Model abstraction made of
denormalized, statically defined tables
Data duplicated over several tables
©2012 DataStax 4
5. CQL3 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
Partition Clustering
Key Key
©2012 DataStax 5
6. CQL3 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 6
7. CQL3 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
[1735, author] [1735, body] [1742, author] [1742, body]
gmason
gwashington I chopped down the... phenry Give me liberty or give...
[1767, author] [1767, body] [1794, author] [1794, body]
ahamilton
gwashington I chopped down the... jadams A government of laws...
©2012 DataStax 7
8. Current Drivers Architecture
CQL API Thrift API OO API DB API
Thrift RPC Thrift RPC
Client Library CQL Driver
* This is a simplified view of drivers architecture. Details vary from one language to another.
©2012 DataStax 8
9. New Drivers Architecture
DB API CQL API OO API
CQL Native Protocol
Next Generation Driver
* This is a simplified view of drivers architecture. Details vary from one language to another.
©2012 DataStax 9
10. DataStax Java Driver
• Reference Implementation
• Asynchronous architecture based on Netty
• Prepared Statements Support
• Automatic Fail-over
• Node Discovery
• Cassandra Tracing Support
• Tunable policies
• LoadBalancingPolicy
• ReconnectionPolicy
•
©2012 DataStax
RetryPolicy
10
11. Request Pipelining
Client Cassandra
Without
Request Pipelining
With
Client Cassandra Request Pipelining
©2012 DataStax 11
12. Notifications
Node
Client
Node
Node
Without
Notifications
With
Notifications
Node
Client
Node
Node
©2012 DataStax 12
14. Asynchronous Architecture
Node
Client
Thread 6
1 Node
Client
Driver 2
Thread 3
Node
5
4
Client
Thread
Node
©2012 DataStax 14
15. LoadBalancingPolicy
public interface LoadBalancingPolicy {
HostDistance distance(Host host);
Iterator<Host> newQueryPlan(Query query);
[...]
}
©2012 DataStax 15
16. DCAwareRoundRobinPolicy
Datacenter A
Local nodes are
queried first, if non Client Node
are available, the
request will be sent Client Node
to a remote node.
Node
Client
Client Node
Client Node
Node
Client
Datacenter B
©2012 DataStax 16
17. TokenAwarePolicy
Nodes that own a
Replica of the data
being read or written
by the query will be
contacted first.
Replica
Node Node
Client Replica
Node
Node
Replica
Node
©2012 DataStax 17
18. RetryPolicy
public interface RetryPolicy {
RetryDecision onReadTimeout(Query query, ConsistencyLevel cl, [...] );
RetryDecision onWriteTimeout(Query query, ConsistencyLevel cl, [...] );
RetryDecision onUnavailable(Query query, ConsistencyLevel cl, [...] );
public static class RetryDecision {
public static enum Type { RETRY, RETHROW, IGNORE };
private final Type type;
private final ConsistencyLevel retryCL;
[...]
}
}
©2012 DataStax 18
19. Downgrading Consistency
If the requested
Consistency Level
cannot be reached
(QUORUM here), a
second request with a
lower CL is sent
automatically. Node Replica
Client Node
Replica
Node
Replica
©2012 DataStax 19
20. Connect and Write
Cluster cluster = 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 20
21. 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 21
22. 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 22
23. Prepared Statements
PreparedStatement ps =
session.prepare("SELECT * FROM users WHERE id = ?");
BoundStatement bs =
session.execute(ps.bind("123")).one().getInt("age");
bs.setString("k");
int age = session.execute(bs).one().getInt("age");
age = session.execute(ps.bind("123")).one().getInt("age");
©2012 DataStax 23
24. Query Builder
String query = "SELECT a,b,"C"
FROM foo
WHERE a IN (127.0.0.1,127.0.0.3)
AND "C"='foo'
ORDER BY a ASC,b DESC LIMIT 42;";
Query select =
select("a", "b", quote("C")).from("foo")
.where(in("a", InetAddress.getByName("127.0.0.1"),
InetAddress.getByName("127.0.0.3")))
.and(eq(quote("C"), "foo"))
.orderBy(asc("a"), desc("b"))
.limit(42);
©2012 DataStax 24
25. Object Mapping
@Table(name = "user")
public class User { public enum Gender {
@PartitionKey @EnumValue("m")
@Column(name = "user_id") MALE,
private String userId;
@EnumValue("f")
private String name; FEMALE;
}
private String email;
private Gender gender;
}
©2012 DataStax 25
26. Inheritance
@Table(name = "catalog")
@Inheritance(
@InheritanceValue("tv")
subClasses = {Phone.class, TV.class},
public class TV
column = "product_type"
extends Product {
)
public abstract class Product {
private float size;
}
@Column(name = "product_id")
private String productId;
private float price;
private String vendor;
private String model;
}
©2012 DataStax 26