SlideShare una empresa de Scribd logo
1 de 28
Descargar para leer sin conexión
New Cassandra Drivers in
Depth

Michaël Figuière
@mfiguiere
Speaker

                 Michaël Figuière




                      @mfiguiere


©2012 DataStax                      2
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
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
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
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
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
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
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
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
Request Pipelining
                 Client   Cassandra




                                               Without
                                      Request Pipelining

                                                  With
                 Client   Cassandra   Request Pipelining




©2012 DataStax                                        11
Notifications
                                 Node

                 Client

                          Node
                                        Node


                                              Without
                                           Notifications

                                                  With
                                           Notifications

                                 Node

                 Client

                          Node
                                        Node

©2012 DataStax                                       12
Asynchronous Architecture

                              Node
            Client
            Thread
                              Node

            Client
                     Driver
            Thread
                              Node


            Client
            Thread
                              Node




©2012 DataStax                       13
Asynchronous Architecture

                                                  Node
            Client
            Thread       6

                     1                            Node

            Client
                             Driver       2
            Thread                            3
                                                  Node
                                      5
                                              4
            Client
            Thread
                                                  Node




©2012 DataStax                                           14
LoadBalancingPolicy


                 public interface LoadBalancingPolicy {

                     HostDistance distance(Host host);

                     Iterator<Host> newQueryPlan(Query query);

                     [...]

                 }




©2012 DataStax                                                   15
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
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
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
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
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
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
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
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
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
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
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
Betas now available!


      https://github.com/datastax/
Stay Tuned!

          blog.datastax.com
          @mfiguiere

Más contenido relacionado

Más de DataStax Academy

Cassandra @ Netflix: Monitoring C* at Scale, Gossip and Tickler & Python
Cassandra @ Netflix: Monitoring C* at Scale, Gossip and Tickler & PythonCassandra @ Netflix: Monitoring C* at Scale, Gossip and Tickler & Python
Cassandra @ Netflix: Monitoring C* at Scale, Gossip and Tickler & Python
DataStax Academy
 
Standing Up Your First Cluster
Standing Up Your First ClusterStanding Up Your First Cluster
Standing Up Your First Cluster
DataStax Academy
 
Real Time Analytics with Dse
Real Time Analytics with DseReal Time Analytics with Dse
Real Time Analytics with Dse
DataStax Academy
 
Introduction to Data Modeling with Apache Cassandra
Introduction to Data Modeling with Apache CassandraIntroduction to Data Modeling with Apache Cassandra
Introduction to Data Modeling with Apache Cassandra
DataStax Academy
 
Enabling Search in your Cassandra Application with DataStax Enterprise
Enabling Search in your Cassandra Application with DataStax EnterpriseEnabling Search in your Cassandra Application with DataStax Enterprise
Enabling Search in your Cassandra Application with DataStax Enterprise
DataStax Academy
 
Advanced Data Modeling with Apache Cassandra
Advanced Data Modeling with Apache CassandraAdvanced Data Modeling with Apache Cassandra
Advanced Data Modeling with Apache Cassandra
DataStax Academy
 

Más de DataStax Academy (20)

Coursera Cassandra Driver
Coursera Cassandra DriverCoursera Cassandra Driver
Coursera Cassandra Driver
 
Production Ready Cassandra
Production Ready CassandraProduction Ready Cassandra
Production Ready Cassandra
 
Cassandra @ Netflix: Monitoring C* at Scale, Gossip and Tickler & Python
Cassandra @ Netflix: Monitoring C* at Scale, Gossip and Tickler & PythonCassandra @ Netflix: Monitoring C* at Scale, Gossip and Tickler & Python
Cassandra @ Netflix: Monitoring C* at Scale, Gossip and Tickler & Python
 
Cassandra @ Sony: The good, the bad, and the ugly part 1
Cassandra @ Sony: The good, the bad, and the ugly part 1Cassandra @ Sony: The good, the bad, and the ugly part 1
Cassandra @ Sony: The good, the bad, and the ugly part 1
 
Cassandra @ Sony: The good, the bad, and the ugly part 2
Cassandra @ Sony: The good, the bad, and the ugly part 2Cassandra @ Sony: The good, the bad, and the ugly part 2
Cassandra @ Sony: The good, the bad, and the ugly part 2
 
Standing Up Your First Cluster
Standing Up Your First ClusterStanding Up Your First Cluster
Standing Up Your First Cluster
 
Real Time Analytics with Dse
Real Time Analytics with DseReal Time Analytics with Dse
Real Time Analytics with Dse
 
Introduction to Data Modeling with Apache Cassandra
Introduction to Data Modeling with Apache CassandraIntroduction to Data Modeling with Apache Cassandra
Introduction to Data Modeling with Apache Cassandra
 
Cassandra Core Concepts
Cassandra Core ConceptsCassandra Core Concepts
Cassandra Core Concepts
 
Enabling Search in your Cassandra Application with DataStax Enterprise
Enabling Search in your Cassandra Application with DataStax EnterpriseEnabling Search in your Cassandra Application with DataStax Enterprise
Enabling Search in your Cassandra Application with DataStax Enterprise
 
Bad Habits Die Hard
Bad Habits Die Hard Bad Habits Die Hard
Bad Habits Die Hard
 
Advanced Data Modeling with Apache Cassandra
Advanced Data Modeling with Apache CassandraAdvanced Data Modeling with Apache Cassandra
Advanced Data Modeling with Apache Cassandra
 
Advanced Cassandra
Advanced CassandraAdvanced Cassandra
Advanced Cassandra
 
Apache Cassandra and Drivers
Apache Cassandra and DriversApache Cassandra and Drivers
Apache Cassandra and Drivers
 
Getting Started with Graph Databases
Getting Started with Graph DatabasesGetting Started with Graph Databases
Getting Started with Graph Databases
 
Cassandra Data Maintenance with Spark
Cassandra Data Maintenance with SparkCassandra Data Maintenance with Spark
Cassandra Data Maintenance with Spark
 
Analytics with Spark and Cassandra
Analytics with Spark and CassandraAnalytics with Spark and Cassandra
Analytics with Spark and Cassandra
 
Make 2016 your year of SMACK talk
Make 2016 your year of SMACK talkMake 2016 your year of SMACK talk
Make 2016 your year of SMACK talk
 
Client Drivers and Cassandra, the Right Way
Client Drivers and Cassandra, the Right WayClient Drivers and Cassandra, the Right Way
Client Drivers and Cassandra, the Right Way
 
Tales From The Front: An Architecture For Multi-Data Center Scalable Applicat...
Tales From The Front: An Architecture For Multi-Data Center Scalable Applicat...Tales From The Front: An Architecture For Multi-Data Center Scalable Applicat...
Tales From The Front: An Architecture For Multi-Data Center Scalable Applicat...
 

NYC* Tech Day - New Cassandra Drivers in Depth

  • 1. New Cassandra Drivers in Depth Michaël Figuière @mfiguiere
  • 2. 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
  • 13. Asynchronous Architecture Node Client Thread Node Client Driver Thread Node Client Thread Node ©2012 DataStax 13
  • 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
  • 27. Betas now available! https://github.com/datastax/
  • 28. Stay Tuned! blog.datastax.com @mfiguiere