Cassandra for Java Developers @ Paris JUG

40

Transcript of Cassandra for Java Developers @ Paris JUG

Page 1: Cassandra for Java Developers @ Paris JUG
Page 2: Cassandra for Java Developers @ Paris JUG

Cassandra for Java DevelopersDataStax Java Driver & DevCenter

Page 3: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

About Me

Alexandre DutraDriver and Tools Software Engineer @

3

@alexdut

linkedin.com/in/alexandredutra

github.com/adutra

[email protected]

Page 4: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

Agenda

1. Java Driver Architecture• Native Protocol• Node Discovery• Load Balancing• Failover & Fault Tolerance

4

Page 5: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

Agenda

1. Java Driver Architecture• Native Protocol• Node Discovery• Load Balancing• Failover & Fault Tolerance

2. Java Driver In Practice• Asynchronous queries• Prepared Statements• Query Builder• Object Mapping API

5

TDD!

Page 6: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

Agenda

1. Java Driver Architecture• Native Protocol• Node Discovery• Load Balancing• Failover & Fault Tolerance

2. Java Driver In Practice• Asynchronous queries• Prepared Statements• Query Builder• Object Mapping API

6

3. DevCenter• Connection Manager• CQL Editor• Query Tracing• Visual DDL

TDD!

Demo!

Page 7: Cassandra for Java Developers @ Paris JUG

I. Java Driver Architecture

Page 8: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

Native Protocol - Basics

8

Client Coord

Node Replica

Replica

ReplicaNode

Coordinator node:Forwards all R/W requeststo corresponding replicas

Native Protocol

Page 9: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

Native Protocol - Request Pipelining

9

Client

WithoutRequest Pipelining

Cassandra

Page 10: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

Native Protocol - Request Pipelining

10

Client

WithRequest Pipelining

Cassandra

1

2

1

3

2

3

StreamID

Page 11: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

Native Protocol - Notifications

11

Client

WithoutNotifications

WithNotifications

NodeNode

Node

Client

NodeNode

Node

Poll

Push

Page 12: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

Node Discovery

12

Contact point will reply with cluster topology, peer addresses etc.

Node

Node

Contact Point Node

Client

Contact Point

Contact Point

Cluster.init()

Control connection

Session.init()

Regular connections

Discovered node

Page 13: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

Load Balancing

13

Node

Node

Node

ClientThread

Node

Driver

Cassandra 2.0N connections per node

Page 14: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

Load Balancing

14

Node

Node

Node

ClientThread

Node

Driver

Cassandra 2.1+ 1 single connection per node

Page 15: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

Load Balancing

15

ClientThread

Node

Node

Node

Node

6

23

45

1Driver

Load balancing picks up a node

Page 16: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

Load Balancing - Round Robin

16

Node

Node

NodeNode

Client

Node

Node

1

2

3 etc.

Page 17: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

Load Balancing - DC Aware Round Robin

17

Node

Node

Node

Client

Datacenter A

Local nodes are tried first

Node

Node

Node

Datacenter B

Page 18: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

Load Balancing - DC Aware Round Robin

18

Node

Node

Node

Client

Datacenter A

Node

Node

Node

Datacenter B

If no local node is available, the request is sent to a remote node.

Page 19: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

Load Balancing - Available Policies

19

RoundRobinPolicy

LoadBalancingPolicy

DCAwareRoundRobinPolicy

Page 20: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

Load Balancing - Available Policies

20

TokenAwarePolicy

WhiteListPolicy

LatencyAwarePolicy

RoundRobinPolicy

LoadBalancingPolicy

DCAwareRoundRobinPolicy

Page 21: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

Load Balancing - Token Awareness

21

Nodes that own a Replica of the PK being read or written by the query will be contacted first.

Node

Node

ReplicaNode

Client

Replica

Replica

Page 22: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

Load Balancing - Default Policy

22

TokenAwarePolicy

DCAwareRoundRobinPolicy

Token awareness

DC awareness

Page 23: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

Automatic Failover

23

ClientThread

Node

Node

Node

Node

7

2

4

53

1

Driver

6

X

Load balancing tries next node

Page 24: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

Fault Tolerance

24

• UnavailableException  

• Consistency level not met

• ReadTimeoutException  

• WriteTimeoutException  

• Coordinator times out

Retry

X⚠️

Client

Page 25: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

Fault Tolerance

25

• UnavailableException  

• Consistency level not met

• ReadTimeoutException  

• WriteTimeoutException  

• Coordinator times out

Retry

X⚠️

Client

• NoHostAvailableException  

• Cluster is down

• Network connectivity issue

Client X

Reconnect

Page 26: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

Retry - Available Policies

26

LoggingRetryPolicyDefaultRetryPolicy

RetryPolicy

DowngradingConsistencyRetryPolicy

FallthroughRetryPolicy

Page 27: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

Reconnection - Available Policies

27

ExponentialReconnectionPolicy

ReconnectionPolicy

ConstantReconnectionPolicy

(default)

Page 28: Cassandra for Java Developers @ Paris JUG

II. Java Driver In Practice

Page 29: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

Java Driver - Maven Artifact

<dependency>  <groupId>com.datastax.cassandra</groupId>  <artifactId>cassandra-­‐driver-­‐core</artifactId>  <version>2.1.4</version>  

</dependency>  

Available in Maven Central (OSGi bundle).

Supports Apache Cassandra 1.2 to 2.1

• https://github.com/datastax/java-driver

• http://www.datastax.com/documentation/developer/java-driver/2.1

29

Page 30: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

Bootstrapping

30

1) Ready… (configure)

Cluster cluster = Cluster.builder() .addContactPoints("10.1.2.5", "cassandra_node3") .build();

2) Steady… (connect)

Session session = cluster.connect("my_keyspace");

3) Go! (execute)

session.execute( "INSERT INTO user (user_id, name, email) VALUES (12345, 'johndoe', '[email protected]')");

The rest of the nodes will be discovered by the driver

Cluster and Session are thread-safe. Singletons should be created at startup!

Page 31: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

Live Coding!

• https://www.github.com/adutra/cassandra-java-driver-demo

31

Page 32: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

Object Mapping API

• Map Objects to Statements and ResultSets to Objects

• Avoid boilerplate for common use cases

• Do NOT hide Cassandra from the developer

• No “clever tricks” à la Hibernate

• Not JPA compatible, but JPA-ish API

32

Page 33: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

Object Mapping API - Maven Artifact

33

<dependency>  <groupId>com.datastax.cassandra</groupId>  <artifactId>cassandra-­‐driver-­‐mapping</artifactId>  <version>2.1.4</version>  

</dependency>  

Additional artifact for object mapping

Available since Driver 2.1.0

Page 34: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

Basic Object Mapping

34

CREATE  TYPE  address  (          street  text,          city  text,          zip  int  );      CREATE  TABLE  users  (          login  text  PRIMARY  KEY,        name  text,        age  int,        address  frozen<address>  );

@UDT(name  =  "address")  public  class  Address  {          private  String  street;          private  String  city;          private  int  zip;              //  getters  and  setters  omitted...  }      @Table(name  =  "users")  public  class  User  {          @PartitionKey          private  String  login;          private  String  name;          private  Integer  age;          @Frozen          private  Address  address;              //  getters  and  setters  omitted...  }  

Page 35: Cassandra for Java Developers @ Paris JUG

III. DevCenter

Page 36: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

DevCenter

• Easy-to-use visual query tool for Cassandra and DSE• Current version: 1.2.2

• 1.3 coming soon• Features

• Create and manage connections• Create and run CQL queries + results visualization• Syntax highlighting & validation• Content assist• Query Tracing Support• Visual DDL assistants (coming soon)

36

Page 37: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

DevCenter

• Audience• Developers (prototyping)• Ops (administration)• Students (learn CQL)

• Technology• Standalone Eclipse RCP application• Linux, Mac, Windows

• datastax.com/download

37

Page 38: Cassandra for Java Developers @ Paris JUG

Questions?

Page 39: Cassandra for Java Developers @ Paris JUG

We’re Hiring!

@DataStaxCareers

blog.datastax.com

github.com/datastax

Page 40: Cassandra for Java Developers @ Paris JUG

Copyright @ 2015 ParisJug. Licence CC - Creative Commons 2.0 France – Paternité - Pas d'Utilisation Commerciale - Partage des Conditions Initiales à l'Identique

Paris JUG Sponsors

40