SlideShare a Scribd company logo
DataStax Java Driver Unleashed!
CQL The new face of Cassandra
* CQL3
* Simpler Data Model using Denormalized Tables
* SQL-like Query Language
* Schema Definition
* CQL Native Protocol
* Introduced in Cassandra 1.2
* Designed for CQL3
* Thrift will keep being supported by Cassandra
* Cassandra lives closer to users and application
* Data model expresses your application intent
* Not made for generalizable queries
* Key to a successful project
CQL Data Model Overview
CQL3 Data Model
Data duplicated over several tables
CQL3 Query Language
comes with a new Data
Model abstraction
made of denormalized,
statically defined tables
CQL3 Data Model
gmason
user_id
1735
tweet_id
phenry
author
Give me liberty or give me death
body
Partition
Key
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
Clustering
Key
Timeline Table
CQL3 Data Model
gmason
user_id
1735
tweet_id
phenry
author
Give me liberty or give me death
body
Partition
Key
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
Clustering
Key
Timeline Table
CREATE TABLE timeline (
user_id varchar,
tweet_id timeuuid,
author varchar,
body varchar,
PRIMARY KEY (user_id, tweet_id));
CQL
DB API
CQL Native Protocol
CQL API OO API
Next Generation Driver
New Driver Architecture
* Reference Implementation
* Asynchronous architecture based on Netty
* Prepared Statements Support
* Automatic Fail-over
* Node Discovery
* Cassandra Tracing Support
* Tunable Policies
Java Driver Overview
Client
Without
Request Pipelining
Cassandra
Client Cassandra
With
Request Pipelining
Request Pipelining
Client
Without
Notifications
With
Notifications
Node
Node
Node
Client
Node
Node
Node
Notifications
Client
Thread
Node
Node
Node
Client
Thread
Client
Thread
Node
Driver
Asynchronous Architecture
1
2
3
4
5
6
Client
Thread
Node
Node
Node
Client
Thread
Client
Thread
Node
Driver
Asynchronous Architecture
contactPoints = {“10.0.0.1”,”10.0.0.2”}
keyspace = “videodb”
public VideoDbBasicImpl(List<String> contactPoints, String keyspace) {
cluster = Cluster
.builder()
.addContactPoints(
! contactPoints.toArray(new String[contactPoints.size()]))
.build();
session = cluster.connect(keyspace);
}
Creating a Basic Connection
public void setUserByUsingString(User user) {
StringBuffer userInsert = new StringBuffer(
"INSERT INTO users (username, firstname, lastname, email, password, created_date) VALUES (");
userInsert.append("'" + user.getUsername() + "'");
userInsert.append("'" + user.getFirstname() + "'");
userInsert.append("'" + user.getLastname() + "'");
userInsert.append("'" + user.getEmail() + "'");
userInsert.append("'" + user.getPassword() + "'");
userInsert.append("'" + user.getCreated_date().toString() + "'");
userInsert.append(")");
session.execute(userInsert.toString());
}
Basic write using insert
! public User getUserByUsernameUsingString(String username) {
! ! User user = new User();
! ! ResultSet rs = session.execute("SELECT * FROM users WHERE username = '"
! ! ! ! + username + "'");
! ! // A result set has Rows which can be iterated over
! ! for (Row row : rs) {
! ! ! user.setUsername(username);
! ! ! user.setFirstname(row.getString("firstname"));
! ! ! user.setLastname(row.getString("lastname"));
! ! ! user.setEmail(row.getString("email"));
! ! ! user.setPassword(row.getString("Password"));
! ! ! user.setCreated_date(row.getDate("created_date"));
! ! }
! ! return user;
! }
Basic Read using Select
public void setUserByPreparedStatement(User user) {
BoundStatement bs = setUser.bind();
bs.setString("username", user.getUsername());
bs.setString("firstname", user.getFirstname());
bs.setString("lastname", user.getLastname());
bs.setString("email", user.getEmail());
bs.setString("password", user.getPassword());
bs.setDate("created_date", user.getCreated_date());
! !
session.execute(bs);
}
Writing with Prepared Statements
public List<Video> getVideosByUsernameUsingAsyncRead(String username) {
BoundStatement bs = getVideosByUsernamePreparedStatement.bind();
List<ResultSetFuture> futures = new ArrayList<ResultSetFuture>();
List<Video> videos = new ArrayList<Video>();
bs.setString("username", username);
for (Row row : session.execute(bs)) {
futures.add(session.executeAsync(getVideoByIDPreparedStatement
.bind(row.getUUID("videoid"))));
}
for (ResultSetFuture future : futures) {
for (Row row : future.getUninterruptibly()) {
Video video = new Video();
video.setVideoid(row.getUUID("videoid"));
videos.add(video);
}
}
return videos;
}
Asynchronous Read
for (String tag : tags) {
BoundStatement bs = getVideosByTagPreparedStatement.bind(tag);
final ResultSetFuture future = session.executeAsync(bs);
future.addListener(new Runnable() {
public void run() {
for (Row row : future.getUninterruptibly()) {
UUID videoId = row.getUUID("videoid");
if (videoIds.putIfAbsent(videoId, PRESENT) == null) {
videoFutures.add(session
.executeAsync(getVideoByIDPreparedStatement
.bind(videoId)));
}
}
}
}, executor);
}
Performing a Read with a Listener
public Video getVideoByIdUsingQueryBuilder(String videoId) {
Video video = new Video();
Query query = select().all().from("videodb", "videos")
.where(eq("videoId", videoId)).limit(10);
query.setConsistencyLevel(ConsistencyLevel.ONE);
ResultSet rs = session.execute(query);
for (Row row : rs) {
video.setVideoid(row.getUUID("videoid"));
video.setVideoname(row.getString("videoName"));
video.setUsername(row.getString("username"));
video.setDescription(row.getString("description"));
}
return video;
}
Using the Query Builder
Load
Balancing
Policy
Node
Node
Node
Health
Monitor
Load Balancing and Failover
ReconnectionNotifications
Client
Retry
Policy
Response
Dispatcher
1 3
2
4
5
6
Node
Node
NodeClient
Datacenter B
Node
Node
Node
Client
Client
Client
Client
Client
Datacenter A
Local nodes are queried
first, if non are available,
the request will be sent
to a remote node.
Multi Datacenter Load Balancing
Multi Datacenter Load Balancing
Node
Node
Replica
Node
Client Node
NodeReplica
Replica
Nodes that own a
Replica of the data
being read or written
by the query will be
contacted first.
contactPoints = {“10.0.0.1”,”10.0.0.2”}
keyspace = “videodb”
public VideoDbBasicImpl(List<String> contactPoints, String keyspace) {
cluster = Cluster
.builder()
.addContactPoints(
! contactPoints.toArray(new String[contactPoints.size()]))
.withLoadBalancingPolicy(Policies.defaultLoadBalancingPolicy())
.build();
session = cluster.connect(keyspace);
}
Create your own policy!
Add a Load Balancing Policy
Client
If the requested
Consistency Level
cannot be reached
(QUORUM here), a
second request with a
lower CL is sent
automatically.
Node
Node Replica
Replica
Node
Replica
Downgrading Retry Policy
contactPoints = {“10.0.0.1”,”10.0.0.2”}
keyspace = “videodb”
public VideoDbBasicImpl(List<String> contactPoints, String keyspace) {
cluster = Cluster
.builder()
.addContactPoints(
! contactPoints.toArray(new String[contactPoints.size()]))
.withLoadBalancingPolicy(Policies.defaultLoadBalancingPolicy())
.withRetryPolicy(Policies.defaultRetryPolicy())
.build();
session = cluster.connect(keyspace);
}
Create your own policy!
Specify a Retry Policy
public enum Gender {
	 @EnumValue("m")
	 MALE,
	
	 @EnumValue("f")
	 FEMALE;
}
@Table(name = "user")
public class User {
	
	 @PartitionKey
	 @Column(name = "user_id")
	 private String userId;
	
	 private String name;
	
	 private String email;
	
	 private Gender gender;
}
Object Mapping
DevCenter an IDE for Developers
Questions ?

More Related Content

What's hot (20)

PDF
Php user groupmemcached
Jason Anderson
 
PDF
Ice mini guide
Ady Liu
 
PDF
Apache ZooKeeper
Scott Leberknight
 
PDF
Easy Scaling with Open Source Data Structures, by Talip Ozturk
ZeroTurnaround
 
PDF
Cassandra summit keynote 2014
jbellis
 
PDF
Cassandra Summit 2015
jbellis
 
PDF
Cassandra 2.0 better, faster, stronger
Patrick McFadin
 
PDF
Hazelcast
oztalip
 
PPTX
DataStax NYC Java Meetup: Cassandra with Java
carolinedatastax
 
PDF
Zookeeper
Geng-Dian Huang
 
PDF
Tokyo cassandra conference 2014
jbellis
 
PDF
Cassandra Summit 2013 Keynote
jbellis
 
PPTX
Hazelcast
Bruno Lellis
 
PPTX
Cassandra Day NY 2014: Getting Started with the DataStax C# Driver
DataStax Academy
 
PDF
Clustering your Application with Hazelcast
Hazelcast
 
PPTX
Async servers and clients in Rest.li
Karan Parikh
 
PDF
Advanced Data Modeling with Apache Cassandra
DataStax Academy
 
PDF
Cassandra 3.0 advanced preview
Patrick McFadin
 
PPTX
Hadoop Puzzlers
DataWorks Summit
 
PDF
Apache Cassandra Lesson: Data Modelling and CQL3
Markus Klems
 
Php user groupmemcached
Jason Anderson
 
Ice mini guide
Ady Liu
 
Apache ZooKeeper
Scott Leberknight
 
Easy Scaling with Open Source Data Structures, by Talip Ozturk
ZeroTurnaround
 
Cassandra summit keynote 2014
jbellis
 
Cassandra Summit 2015
jbellis
 
Cassandra 2.0 better, faster, stronger
Patrick McFadin
 
Hazelcast
oztalip
 
DataStax NYC Java Meetup: Cassandra with Java
carolinedatastax
 
Zookeeper
Geng-Dian Huang
 
Tokyo cassandra conference 2014
jbellis
 
Cassandra Summit 2013 Keynote
jbellis
 
Hazelcast
Bruno Lellis
 
Cassandra Day NY 2014: Getting Started with the DataStax C# Driver
DataStax Academy
 
Clustering your Application with Hazelcast
Hazelcast
 
Async servers and clients in Rest.li
Karan Parikh
 
Advanced Data Modeling with Apache Cassandra
DataStax Academy
 
Cassandra 3.0 advanced preview
Patrick McFadin
 
Hadoop Puzzlers
DataWorks Summit
 
Apache Cassandra Lesson: Data Modelling and CQL3
Markus Klems
 

Viewers also liked (14)

PPT
Visual Essay with Soundtrack
bboyhan
 
PDF
La règle du carmel
christianemeres
 
PDF
Jean de la Croix et Jonathan le Goéland
christianemeres
 
PDF
Jean de la croix, maître de prière
christianemeres
 
DOCX
Digital abstract
Obula Reddy
 
PPTX
P5 ram instillation
har139
 
PPT
Photoshop 网页教程实例讲解
changsha
 
PPTX
Software Performance Engineering-01
V pathirana
 
PDF
Gustos Musicales
arturofl
 
PDF
Final Version of the Poster
Caitlin Schober
 
PDF
Profile
Raquel Tumol
 
PDF
Cannistra carmel-aujourd'hui
christianemeres
 
DOCX
Report (Electromagnetic Password Door Lock System)
Siang Wei Lee
 
DOCX
Umesh Resume
Umesh Reddy AnnapuReddy
 
Visual Essay with Soundtrack
bboyhan
 
La règle du carmel
christianemeres
 
Jean de la Croix et Jonathan le Goéland
christianemeres
 
Jean de la croix, maître de prière
christianemeres
 
Digital abstract
Obula Reddy
 
P5 ram instillation
har139
 
Photoshop 网页教程实例讲解
changsha
 
Software Performance Engineering-01
V pathirana
 
Gustos Musicales
arturofl
 
Final Version of the Poster
Caitlin Schober
 
Profile
Raquel Tumol
 
Cannistra carmel-aujourd'hui
christianemeres
 
Report (Electromagnetic Password Door Lock System)
Siang Wei Lee
 
Ad

Similar to Cassandra summit 2013 - DataStax Java Driver Unleashed! (20)

PDF
C* Summit EU 2013: Cassandra Made Simple with CQL Drivers and DevCenter
DataStax Academy
 
PDF
Cassandra 3.0 Data Modeling
DataStax Academy
 
PDF
Cassandra Drivers and Tools
Duyhai Doan
 
PDF
Cassandra drivers and libraries
Duyhai Doan
 
PDF
KillrVideo: Data Modeling Evolved (Patrick McFadin, Datastax) | Cassandra Sum...
DataStax
 
PDF
Hey Relational Developer, Let's Go Crazy (Patrick McFadin, DataStax) | Cassan...
DataStax
 
PDF
Building your First Application with Cassandra
Luke Tillman
 
PDF
Introduction to Data Modeling with Apache Cassandra
Luke Tillman
 
PDF
Paris Cassandra Meetup - Overview of New Cassandra Drivers
Michaël Figuière
 
PDF
Cassandra Day Chicago 2015: Building Your First Application with Apache Cassa...
DataStax Academy
 
PDF
Cassandra Day Atlanta 2015: Building Your First Application with Apache Cassa...
DataStax Academy
 
PDF
Cassandra Day London 2015: Building Your First Application in Apache Cassandra
DataStax Academy
 
PDF
Going native with Apache Cassandra
Johnny Miller
 
PDF
Using JPA applications in the era of NoSQL: Introducing Hibernate OGM
PT.JUG
 
PDF
Introduction to Apache Cassandra™ + What’s New in 4.0
DataStax
 
ODP
Meetup cassandra sfo_jdbc
zznate
 
PDF
Introduction to Data Modeling with Apache Cassandra
DataStax Academy
 
PDF
Cassandra Day Atlanta 2015: Data Modeling 101
DataStax Academy
 
PDF
Cassandra Day Chicago 2015: Apache Cassandra Data Modeling 101
DataStax Academy
 
C* Summit EU 2013: Cassandra Made Simple with CQL Drivers and DevCenter
DataStax Academy
 
Cassandra 3.0 Data Modeling
DataStax Academy
 
Cassandra Drivers and Tools
Duyhai Doan
 
Cassandra drivers and libraries
Duyhai Doan
 
KillrVideo: Data Modeling Evolved (Patrick McFadin, Datastax) | Cassandra Sum...
DataStax
 
Hey Relational Developer, Let's Go Crazy (Patrick McFadin, DataStax) | Cassan...
DataStax
 
Building your First Application with Cassandra
Luke Tillman
 
Introduction to Data Modeling with Apache Cassandra
Luke Tillman
 
Paris Cassandra Meetup - Overview of New Cassandra Drivers
Michaël Figuière
 
Cassandra Day Chicago 2015: Building Your First Application with Apache Cassa...
DataStax Academy
 
Cassandra Day Atlanta 2015: Building Your First Application with Apache Cassa...
DataStax Academy
 
Cassandra Day London 2015: Building Your First Application in Apache Cassandra
DataStax Academy
 
Going native with Apache Cassandra
Johnny Miller
 
Using JPA applications in the era of NoSQL: Introducing Hibernate OGM
PT.JUG
 
Introduction to Apache Cassandra™ + What’s New in 4.0
DataStax
 
Meetup cassandra sfo_jdbc
zznate
 
Introduction to Data Modeling with Apache Cassandra
DataStax Academy
 
Cassandra Day Atlanta 2015: Data Modeling 101
DataStax Academy
 
Cassandra Day Chicago 2015: Apache Cassandra Data Modeling 101
DataStax Academy
 
Ad

More from Michaël Figuière (19)

PDF
EclipseCon - Building an IDE for Apache Cassandra
Michaël Figuière
 
PDF
NYC* Tech Day - New Cassandra Drivers in Depth
Michaël Figuière
 
PDF
ApacheCon Europe 2012 - Real Time Big Data in practice with Cassandra
Michaël Figuière
 
PDF
NoSQL Matters 2012 - Real Time Big Data in practice with Cassandra
Michaël Figuière
 
PDF
GTUG Nantes (Dec 2011) - BigTable et NoSQL
Michaël Figuière
 
PDF
Duchess France (Nov 2011) - Atelier Apache Mahout
Michaël Figuière
 
PDF
JUG Summer Camp (Sep 2011) - Les applications et architectures d’entreprise d...
Michaël Figuière
 
PDF
BreizhCamp (Jun 2011) - Haute disponibilité et élasticité avec Cassandra
Michaël Figuière
 
PDF
Mix-IT (Apr 2011) - Intelligence Collective avec Apache Mahout
Michaël Figuière
 
PDF
Xebia Knowledge Exchange (mars 2011) - Machine Learning with Apache Mahout
Michaël Figuière
 
PDF
Breizh JUG (mar 2011) - NoSQL : Des Grands du Web aux Entreprises
Michaël Figuière
 
PDF
FOSDEM (feb 2011) - A real-time search engine with Lucene and S4
Michaël Figuière
 
PDF
Xebia Knowledge Exchange (feb 2011) - Large Scale Web Development
Michaël Figuière
 
PDF
Xebia Knowledge Exchange (jan 2011) - Trends in Enterprise Applications Archi...
Michaël Figuière
 
PDF
Lorraine JUG (dec 2010) - NoSQL, des grands du Web aux entreprises
Michaël Figuière
 
PDF
Tours JUG (oct 2010) - NoSQL, des grands du Web aux entreprises
Michaël Figuière
 
PPTX
Paris JUG (sept 2010) - NoSQL : Des concepts à la réalité
Michaël Figuière
 
PPTX
Xebia Knowledge Exchange (mars 2010) - Lucene : From theory to real world
Michaël Figuière
 
PPTX
Xebia Knowledge Exchange (may 2010) - NoSQL : Using the right tool for the ri...
Michaël Figuière
 
EclipseCon - Building an IDE for Apache Cassandra
Michaël Figuière
 
NYC* Tech Day - New Cassandra Drivers in Depth
Michaël Figuière
 
ApacheCon Europe 2012 - Real Time Big Data in practice with Cassandra
Michaël Figuière
 
NoSQL Matters 2012 - Real Time Big Data in practice with Cassandra
Michaël Figuière
 
GTUG Nantes (Dec 2011) - BigTable et NoSQL
Michaël Figuière
 
Duchess France (Nov 2011) - Atelier Apache Mahout
Michaël Figuière
 
JUG Summer Camp (Sep 2011) - Les applications et architectures d’entreprise d...
Michaël Figuière
 
BreizhCamp (Jun 2011) - Haute disponibilité et élasticité avec Cassandra
Michaël Figuière
 
Mix-IT (Apr 2011) - Intelligence Collective avec Apache Mahout
Michaël Figuière
 
Xebia Knowledge Exchange (mars 2011) - Machine Learning with Apache Mahout
Michaël Figuière
 
Breizh JUG (mar 2011) - NoSQL : Des Grands du Web aux Entreprises
Michaël Figuière
 
FOSDEM (feb 2011) - A real-time search engine with Lucene and S4
Michaël Figuière
 
Xebia Knowledge Exchange (feb 2011) - Large Scale Web Development
Michaël Figuière
 
Xebia Knowledge Exchange (jan 2011) - Trends in Enterprise Applications Archi...
Michaël Figuière
 
Lorraine JUG (dec 2010) - NoSQL, des grands du Web aux entreprises
Michaël Figuière
 
Tours JUG (oct 2010) - NoSQL, des grands du Web aux entreprises
Michaël Figuière
 
Paris JUG (sept 2010) - NoSQL : Des concepts à la réalité
Michaël Figuière
 
Xebia Knowledge Exchange (mars 2010) - Lucene : From theory to real world
Michaël Figuière
 
Xebia Knowledge Exchange (may 2010) - NoSQL : Using the right tool for the ri...
Michaël Figuière
 

Recently uploaded (20)

PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PDF
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PPT
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
PDF
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
PDF
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
PPTX
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PDF
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 

Cassandra summit 2013 - DataStax Java Driver Unleashed!

  • 1. DataStax Java Driver Unleashed!
  • 2. CQL The new face of Cassandra * CQL3 * Simpler Data Model using Denormalized Tables * SQL-like Query Language * Schema Definition * CQL Native Protocol * Introduced in Cassandra 1.2 * Designed for CQL3 * Thrift will keep being supported by Cassandra
  • 3. * Cassandra lives closer to users and application * Data model expresses your application intent * Not made for generalizable queries * Key to a successful project CQL Data Model Overview
  • 4. CQL3 Data Model Data duplicated over several tables CQL3 Query Language comes with a new Data Model abstraction made of denormalized, statically defined tables
  • 5. CQL3 Data Model gmason user_id 1735 tweet_id phenry author Give me liberty or give me death body Partition Key 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 Clustering Key Timeline Table
  • 6. CQL3 Data Model gmason user_id 1735 tweet_id phenry author Give me liberty or give me death body Partition Key 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 Clustering Key Timeline Table CREATE TABLE timeline ( user_id varchar, tweet_id timeuuid, author varchar, body varchar, PRIMARY KEY (user_id, tweet_id)); CQL
  • 7. DB API CQL Native Protocol CQL API OO API Next Generation Driver New Driver Architecture
  • 8. * Reference Implementation * Asynchronous architecture based on Netty * Prepared Statements Support * Automatic Fail-over * Node Discovery * Cassandra Tracing Support * Tunable Policies Java Driver Overview
  • 13. contactPoints = {“10.0.0.1”,”10.0.0.2”} keyspace = “videodb” public VideoDbBasicImpl(List<String> contactPoints, String keyspace) { cluster = Cluster .builder() .addContactPoints( ! contactPoints.toArray(new String[contactPoints.size()])) .build(); session = cluster.connect(keyspace); } Creating a Basic Connection
  • 14. public void setUserByUsingString(User user) { StringBuffer userInsert = new StringBuffer( "INSERT INTO users (username, firstname, lastname, email, password, created_date) VALUES ("); userInsert.append("'" + user.getUsername() + "'"); userInsert.append("'" + user.getFirstname() + "'"); userInsert.append("'" + user.getLastname() + "'"); userInsert.append("'" + user.getEmail() + "'"); userInsert.append("'" + user.getPassword() + "'"); userInsert.append("'" + user.getCreated_date().toString() + "'"); userInsert.append(")"); session.execute(userInsert.toString()); } Basic write using insert
  • 15. ! public User getUserByUsernameUsingString(String username) { ! ! User user = new User(); ! ! ResultSet rs = session.execute("SELECT * FROM users WHERE username = '" ! ! ! ! + username + "'"); ! ! // A result set has Rows which can be iterated over ! ! for (Row row : rs) { ! ! ! user.setUsername(username); ! ! ! user.setFirstname(row.getString("firstname")); ! ! ! user.setLastname(row.getString("lastname")); ! ! ! user.setEmail(row.getString("email")); ! ! ! user.setPassword(row.getString("Password")); ! ! ! user.setCreated_date(row.getDate("created_date")); ! ! } ! ! return user; ! } Basic Read using Select
  • 16. public void setUserByPreparedStatement(User user) { BoundStatement bs = setUser.bind(); bs.setString("username", user.getUsername()); bs.setString("firstname", user.getFirstname()); bs.setString("lastname", user.getLastname()); bs.setString("email", user.getEmail()); bs.setString("password", user.getPassword()); bs.setDate("created_date", user.getCreated_date()); ! ! session.execute(bs); } Writing with Prepared Statements
  • 17. public List<Video> getVideosByUsernameUsingAsyncRead(String username) { BoundStatement bs = getVideosByUsernamePreparedStatement.bind(); List<ResultSetFuture> futures = new ArrayList<ResultSetFuture>(); List<Video> videos = new ArrayList<Video>(); bs.setString("username", username); for (Row row : session.execute(bs)) { futures.add(session.executeAsync(getVideoByIDPreparedStatement .bind(row.getUUID("videoid")))); } for (ResultSetFuture future : futures) { for (Row row : future.getUninterruptibly()) { Video video = new Video(); video.setVideoid(row.getUUID("videoid")); videos.add(video); } } return videos; } Asynchronous Read
  • 18. for (String tag : tags) { BoundStatement bs = getVideosByTagPreparedStatement.bind(tag); final ResultSetFuture future = session.executeAsync(bs); future.addListener(new Runnable() { public void run() { for (Row row : future.getUninterruptibly()) { UUID videoId = row.getUUID("videoid"); if (videoIds.putIfAbsent(videoId, PRESENT) == null) { videoFutures.add(session .executeAsync(getVideoByIDPreparedStatement .bind(videoId))); } } } }, executor); } Performing a Read with a Listener
  • 19. public Video getVideoByIdUsingQueryBuilder(String videoId) { Video video = new Video(); Query query = select().all().from("videodb", "videos") .where(eq("videoId", videoId)).limit(10); query.setConsistencyLevel(ConsistencyLevel.ONE); ResultSet rs = session.execute(query); for (Row row : rs) { video.setVideoid(row.getUUID("videoid")); video.setVideoname(row.getString("videoName")); video.setUsername(row.getString("username")); video.setDescription(row.getString("description")); } return video; } Using the Query Builder
  • 20. Load Balancing Policy Node Node Node Health Monitor Load Balancing and Failover ReconnectionNotifications Client Retry Policy Response Dispatcher 1 3 2 4 5 6
  • 21. Node Node NodeClient Datacenter B Node Node Node Client Client Client Client Client Datacenter A Local nodes are queried first, if non are available, the request will be sent to a remote node. Multi Datacenter Load Balancing
  • 22. Multi Datacenter Load Balancing Node Node Replica Node Client Node NodeReplica Replica Nodes that own a Replica of the data being read or written by the query will be contacted first.
  • 23. contactPoints = {“10.0.0.1”,”10.0.0.2”} keyspace = “videodb” public VideoDbBasicImpl(List<String> contactPoints, String keyspace) { cluster = Cluster .builder() .addContactPoints( ! contactPoints.toArray(new String[contactPoints.size()])) .withLoadBalancingPolicy(Policies.defaultLoadBalancingPolicy()) .build(); session = cluster.connect(keyspace); } Create your own policy! Add a Load Balancing Policy
  • 24. Client If the requested Consistency Level cannot be reached (QUORUM here), a second request with a lower CL is sent automatically. Node Node Replica Replica Node Replica Downgrading Retry Policy
  • 25. contactPoints = {“10.0.0.1”,”10.0.0.2”} keyspace = “videodb” public VideoDbBasicImpl(List<String> contactPoints, String keyspace) { cluster = Cluster .builder() .addContactPoints( ! contactPoints.toArray(new String[contactPoints.size()])) .withLoadBalancingPolicy(Policies.defaultLoadBalancingPolicy()) .withRetryPolicy(Policies.defaultRetryPolicy()) .build(); session = cluster.connect(keyspace); } Create your own policy! Specify a Retry Policy
  • 26. public enum Gender { @EnumValue("m") MALE, @EnumValue("f") FEMALE; } @Table(name = "user") public class User { @PartitionKey @Column(name = "user_id") private String userId; private String name; private String email; private Gender gender; } Object Mapping
  • 27. DevCenter an IDE for Developers