SlideShare a Scribd company logo
1 of 33
Download to read offline
Going Native With Apache Cassandra™
QCon London, 2014
www.datastax.com
@DataStaxEMEA
About Me
©2014 DataStax. Do not distribute without consent. 2
Johnny Miller
Solutions Architect
www.datastax.com
@DataStaxEU
@CyanMiller
•  https://www.linkedin.com/in/johnnymiller
DataStax
©2014 DataStax. Do not distribute without consent. 3
•  Founded in April 2010
•  We drive Apache Cassandra™
•  400+ customers (24 of the Fortune 100)
•  220+ employees
•  Contribute approximately 80% of the code to Cassandra
•  Home to Apache Cassandra Chair & most committers
•  Headquartered in San Francisco Bay area
•  European headquarters established in London
We are hiring
www.datastax.com/careers
What do I mean by going native?
©2014 DataStax. Do not distribute without consent. 4
•  Traditionally, Cassandra clients (Hector, Astynax1 etc..) were
developed using Thrift
•  With Cassandra 1.2 (Jan 2013) and the introduction of CQL3
and the CQL native protocol a new easier way of using
Cassandra was introduced.
•  Why?
•  Easier to develop and model
•  Best practices for building modern distributed applications
•  Integrated tools and experience
•  Enable Cassandra to evolve easier and support new features
1Astynax is being updated to include the native driver: https://github.com/Netflix/astyanax/wiki/Astyanax-over-Java-Driver
CQL
©2014 DataStax. Do not distribute without consent. 5
•  Cassandra Query Language
•  CQL is intended to provide a common, simpler and
easier to use interface into Cassandra - and you
probably already know it!
e.g. SELECT * FROM users
•  Usual statements
•  CREATE / DROP / ALTER TABLE / SELECT
Creating A Keyspace
©2014 DataStax. Do not distribute without consent. 6
CREATE KEYSPACE johnny WITH REPLICATION =
{‘class’:’NetworkTopologyStrategy’, ‘USA’:3, ‘EU’: 2};
Node 1
1st copy
Node 4
Node 5
Node 2
2nd copy
Node 3
Node 1
1st copy
Node 4
Node 5
Node 2
2nd copy
Node 3
3rd copy
DC: USA DC: EU
CQL Basics
©2014 DataStax. Do not distribute without consent. 7
CREATE TABLE sporty_league (
team_name varchar,
player_name varchar,
jersey int,
PRIMARY KEY (team_name, player_name)
);
SELECT * FROM sporty_league WHERE team_name = ‘Mighty Mutts’ and player_name = ‘Lucky’;
INSERT INTO sporty_league (team_name, player_name, jersey) VALUES ('Mighty Mutts',’Felix’,
90);
Predicates
•  On the partition key: = and IN
•  On the cluster columns: <, <=, =, >=, >, IN
Collections Data Type
©2014 DataStax. Do not distribute without consent. 8
•  CQL supports having columns that contain collections of data.
•  The collection types include:
•  Set, List and Map.
•  Some performance considerations around collections.
•  Sometimes more efficient to denormalise further rather than use
collections if intending to store lots of data.
•  Favour sets over list – more performant
•  Watch out for collection indexing in Cassandra 2.1!
CREATE TABLE collections_example (
id int PRIMARY KEY,
set_example set<text>,
list_example list<text>,
map_example map<int, text>
);
Query Tracing
©2014 DataStax. Do not distribute without consent. 9
•  You can turn tracing on or off for queries with the TRACING ON |
OFF command.
•  This can help you understand what Cassandra is doing and identify
any performance problems.
Worth reading: http://www.datastax.com/dev/blog/tracing-in-cassandra-1-2
Plus much, much more…
©2014 DataStax. Do not distribute without consent. 10
•  Light Weight Transactions
INSERT INTO customer_account (customerID, customer_email) VALUES (‘LauraS’, ‘lauras@gmail.com’) IF NOT
EXISTS;
UPDATE customer_account SET customer_email=’laurass@gmail.com’
IF customer_email=’lauras@gmail.com’;
•  Counters
UPDATE UserActions SET total = total + 2
WHERE user = 123 AND action = ’xyz';
•  Time to live (TTL)
INSERT INTO users (id, first, last) VALUES (‘abc123’, ‘abe’, ‘lincoln’) USING TTL 3600;
•  Batch Statements
BEGIN BATCH
INSERT INTO users (userID, password, name) VALUES ('user2', 'ch@ngem3b', 'second user')
UPDATE users SET password = 'ps22dhds' WHERE userID = 'user2'
INSERT INTO users (userID, password) VALUES ('user3', 'ch@ngem3c')
DELETE name FROM users WHERE userID = 'user2’
APPLY BATCH;
•  New CQL features coming in Cassandra 2.0.6
•  http://www.datastax.com/dev/blog/cql-in-2-0-6
CQL Native Protocol
Request Pipelining
©2014 DataStax. Do not distribute without consent. 12
With it:
Without it:
Notifications
©2014 DataStax. Do not distribute without consent. 13
Without Notifications
With Notifications
Polling
Pushing
Notifications are for technical events only:
•  Topology changes
•  Node status changes,
•  Schema changes
Asynchronous Architecture
©2014 DataStax. Do not distribute without consent. 14
Client
Thread
Client
Thread
Client
Thread
Driver
1
2 3
4
5
6
Native Drivers
Native Drivers
©2014 DataStax. Do not distribute without consent. 16
•  Java
•  C#
•  Python
•  C++ (beta)
•  ODBC (beta)
•  Clojure
•  Erlang
•  Node.js
•  Ruby
•  Plus many, many more….
Get them here: http://www.datastax.com/download
Connect and Write
©2014 DataStax. Do not distribute without consent. 17
Cluster cluster = Cluster.builder()
.addContactPoints("10.158.02.40", "10.158.02.44")
.build();
Session session = cluster.connect("akeyspace");
session.execute(
"INSERT INTO user (username, password) ”
+ "VALUES(‘johnny’, ‘password1234’)"
);
Note: Clusters and Sessions should be long-lived and re-used.
Read from a table
©2014 DataStax. Do not distribute without consent. 18
ResultSet rs = session.execute("SELECT * FROM user");
List<Row> rows = rs.all();
for (Row row : rows) {
String userName = row.getString("username");
String password = row.getString("password");
}
Asynchronous Read
©2014 DataStax. Do not distribute without consent. 19
ResultSetFuture future = session.executeAsync(
"SELECT * FROM user");
for (Row row : future.get()) {
String userName = row.getString("username");
String password = row.getString("password");
}
Note: The future returned implements Guava's ListenableFuture interface. This
means you can use all Guava's Futures1 methods!
1http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/util/concurrent/Futures.html
Read with Callbacks
©2014 DataStax. Do not distribute without consent. 20
final ResultSetFuture future =
session.executeAsync("SELECT * FROM user");
future.addListener(new Runnable() {
public void run() {
for (Row row : future.get()) {
String userName = row.getString("username");
String password = row.getString("password");
}
}
}, executor);
Parallelize Calls
©2014 DataStax. Do not distribute without consent. 21
int queryCount = 99;
List<ResultSetFuture> futures = new ArrayList<ResultSetFuture>();
for (int i=0; i<queryCount; i++) {
futures.add(
session.executeAsync("SELECT * FROM user "
+"WHERE username = '"+i+"'"));
}
for(ResultSetFuture future : futures) {
for (Row row : future.getUninterruptibly()) {
//do something
}
}
Tip
©2014 DataStax. Do not distribute without consent. 22
•  If you need to do a lot of work, it’s often better to
make many small queries concurrently than to make
one big query.
•  executeAsync and Futures – makes this really easy!
•  Big queries can put a high load on one coordinator
•  Big queries can skew your 99th percentile latencies for
other queries
•  If one small query fails you can easily retry, if a big query
than you have to retry the whole thing
Prepared Statements
©2014 DataStax. Do not distribute without consent. 23
PreparedStatement statement = session.prepare(
"INSERT INTO user (username, password) "
+ "VALUES (?, ?)");
BoundStatement bs = statement.bind();
bs.setString("username", "johnny");
bs.setString("password", "password1234");
session.execute(bs);
Query Builder
©2014 DataStax. Do not distribute without consent. 24
Query query = QueryBuilder
.select()
.all()
.from("akeyspace", "user")
.where(eq("username", "johnny"));
query.setConsistencyLevel(ConsistencyLevel.ONE);
ResultSet rs = session.execute(query);
Multi Data Center Load Balancing
©2014 DataStax. Do not distribute without consent. 25
•  Local nodes are queried first, if none are available
the request will be sent to a remote data center
Cluster cluster = Cluster.builder()	
	 	.addContactPoints("10.158.02.40", "10.158.02.44")	
	 	.withLoadBalancingPolicy(	
	 	 	new DCAwareRoundRobinPolicy("DC1"))	
	 	.build();	
	
	
	
	
	
Name of the local DC
Token Aware Load Balancing
©2014 DataStax. Do not distribute without consent. 26
•  Nodes that own a replica of the data being read or
written by the query will be contacted first
Cluster cluster = Cluster.builder()	
	 	.addContactPoints("10.158.02.40", "10.158.02.44")	
	 	.withLoadBalancingPolicy(	
	 	 	new TokenAwarePolicy(	
	 	 	 	new DCAwareRoundRobinPolicy("DC1")))	
	 	.build();	
	
	
	
	
http://www.datastax.com/drivers/java/2.0/com/datastax/driver/core/policies/TokenAwarePolicy.html
Retry Policies
©2014 DataStax. Do not distribute without consent. 27
•  This defined the behavior to adopt when a request
returns a timeout or is unavailable.
Cluster cluster = Cluster.builder()
.addContactPoints("10.158.02.40", "10.158.02.44")
.withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE)
.withLoadBalancingPolicy(new TokenAwarePolicy(new
DCAwareRoundRobinPolicy("DC1")))
.build();
•  DefaultRetryPolicy
•  DowngradingConsistencyRetryPolicy
•  FallthroughRetryPolicy
•  LoggingRetryPolicy
http://www.datastax.com/drivers/java/2.0/com/datastax/driver/core/policies/RetryPolicy.html
Reconnection Policies
©2014 DataStax. Do not distribute without consent. 28
•  Policy that decides how often the reconnection to a
dead node is attempted.
Cluster cluster = Cluster.builder()
.addContactPoints("10.158.02.40", "10.158.02.44”)
.withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE)
.withReconnectionPolicy(new ConstantReconnectionPolicy(1000))
.withLoadBalancingPolicy(new TokenAwarePolicy(new
DCAwareRoundRobinPolicy("DC1")))
.build();

	
•  ConstantReconnectionPolicy
•  ExponentialReconnectionPolicy
Automatic Paging
©2014 DataStax. Do not distribute without consent. 29
•  This was new in Cassandra 2.0
•  Previously – you would select data in batches
Query Tracing
©2014 DataStax. Do not distribute without consent. 30
•  Tracing is enabled on a per-query basis.
Query query = QueryBuilder
.select()
.all()
.from("akeyspace", "user")
.where(eq("username", "johnny"))
.enableTracing();
ResultSet rs = session.execute(query);
ExecutionInfo executionInfo = rs.getExecutionInfo();
QueryTrace queryTrace = executionInfo.getQueryTrace();
DevCenter
©2014 DataStax. Do not distribute without consent. 31
•  Desktop app
•  friendly, familiar, productive
•  Free
http://www.datastax.com/devcenter
Find Out More
©2014 DataStax. Do not distribute without consent. 32
DataStax:
•  http://www.datastax.com
Getting Started:
•  http://www.datastax.com/documentation/gettingstarted/index.html
Training:
•  http://www.datatstax.com/training
Downloads:
•  http://www.datastax.com/download
Documentation:
•  http://www.datastax.com/docs
Developer Blog:
•  http://www.datastax.com/dev/blog
Community Site:
•  http://planetcassandra.org
Webinars:
•  http://planetcassandra.org/Learn/CassandraCommunityWebinars
©2014 DataStax. Do not distribute without consent. 33

More Related Content

What's hot

C*ollege Credit: Is My App a Good Fit for Cassandra?
C*ollege Credit: Is My App a Good Fit for Cassandra?C*ollege Credit: Is My App a Good Fit for Cassandra?
C*ollege Credit: Is My App a Good Fit for Cassandra?
DataStax
 

What's hot (20)

Apache Cassandra Certification
Apache Cassandra CertificationApache Cassandra Certification
Apache Cassandra Certification
 
Nyc summit intro_to_cassandra
Nyc summit intro_to_cassandraNyc summit intro_to_cassandra
Nyc summit intro_to_cassandra
 
From PoCs to Production
From PoCs to ProductionFrom PoCs to Production
From PoCs to Production
 
Cassandra TK 2014 - Large Nodes
Cassandra TK 2014 - Large NodesCassandra TK 2014 - Large Nodes
Cassandra TK 2014 - Large Nodes
 
Introduction to DataStax Enterprise Graph Database
Introduction to DataStax Enterprise Graph DatabaseIntroduction to DataStax Enterprise Graph Database
Introduction to DataStax Enterprise Graph Database
 
Webinar | Introducing DataStax Enterprise 4.6
Webinar | Introducing DataStax Enterprise 4.6Webinar | Introducing DataStax Enterprise 4.6
Webinar | Introducing DataStax Enterprise 4.6
 
How to size up an Apache Cassandra cluster (Training)
How to size up an Apache Cassandra cluster (Training)How to size up an Apache Cassandra cluster (Training)
How to size up an Apache Cassandra cluster (Training)
 
Webinar: Dyn + DataStax - helping companies deliver exceptional end-user expe...
Webinar: Dyn + DataStax - helping companies deliver exceptional end-user expe...Webinar: Dyn + DataStax - helping companies deliver exceptional end-user expe...
Webinar: Dyn + DataStax - helping companies deliver exceptional end-user expe...
 
C*ollege Credit: Is My App a Good Fit for Cassandra?
C*ollege Credit: Is My App a Good Fit for Cassandra?C*ollege Credit: Is My App a Good Fit for Cassandra?
C*ollege Credit: Is My App a Good Fit for Cassandra?
 
C* Summit 2013: Searching for a Needle in a Big Data Haystack by Jason Ruther...
C* Summit 2013: Searching for a Needle in a Big Data Haystack by Jason Ruther...C* Summit 2013: Searching for a Needle in a Big Data Haystack by Jason Ruther...
C* Summit 2013: Searching for a Needle in a Big Data Haystack by Jason Ruther...
 
Cassandra Summit 2014: Apache Cassandra Best Practices at Ebay
Cassandra Summit 2014: Apache Cassandra Best Practices at EbayCassandra Summit 2014: Apache Cassandra Best Practices at Ebay
Cassandra Summit 2014: Apache Cassandra Best Practices at Ebay
 
Webinar: Getting Started with Apache Cassandra
Webinar: Getting Started with Apache CassandraWebinar: Getting Started with Apache Cassandra
Webinar: Getting Started with Apache Cassandra
 
Introducing DataStax Enterprise 4.7
Introducing DataStax Enterprise 4.7Introducing DataStax Enterprise 4.7
Introducing DataStax Enterprise 4.7
 
Oracle to Cassandra Core Concepts Guide Pt. 2
Oracle to Cassandra Core Concepts Guide Pt. 2Oracle to Cassandra Core Concepts Guide Pt. 2
Oracle to Cassandra Core Concepts Guide Pt. 2
 
Oracle to Cassandra Core Concepts Guid Part 1: A new hope
Oracle to Cassandra Core Concepts Guid Part 1: A new hopeOracle to Cassandra Core Concepts Guid Part 1: A new hope
Oracle to Cassandra Core Concepts Guid Part 1: A new hope
 
Real-time personal trainer on the SMACK stack
Real-time personal trainer on the SMACK stackReal-time personal trainer on the SMACK stack
Real-time personal trainer on the SMACK stack
 
Making Every Drop Count: How i20 Addresses the Water Crisis with the IoT and ...
Making Every Drop Count: How i20 Addresses the Water Crisis with the IoT and ...Making Every Drop Count: How i20 Addresses the Water Crisis with the IoT and ...
Making Every Drop Count: How i20 Addresses the Water Crisis with the IoT and ...
 
Cold Storage That Isn't Glacial (Joshua Hollander, Protectwise) | Cassandra S...
Cold Storage That Isn't Glacial (Joshua Hollander, Protectwise) | Cassandra S...Cold Storage That Isn't Glacial (Joshua Hollander, Protectwise) | Cassandra S...
Cold Storage That Isn't Glacial (Joshua Hollander, Protectwise) | Cassandra S...
 
Cassandra Community Webinar: MySQL to Cassandra - What I Wish I'd Known
Cassandra Community Webinar: MySQL to Cassandra - What I Wish I'd KnownCassandra Community Webinar: MySQL to Cassandra - What I Wish I'd Known
Cassandra Community Webinar: MySQL to Cassandra - What I Wish I'd Known
 
How jKool Analyzes Streaming Data in Real Time with DataStax
How jKool Analyzes Streaming Data in Real Time with DataStaxHow jKool Analyzes Streaming Data in Real Time with DataStax
How jKool Analyzes Streaming Data in Real Time with DataStax
 

Similar to Going native with Apache Cassandra

What We Learned About Cassandra While Building go90 (Christopher Webster & Th...
What We Learned About Cassandra While Building go90 (Christopher Webster & Th...What We Learned About Cassandra While Building go90 (Christopher Webster & Th...
What We Learned About Cassandra While Building go90 (Christopher Webster & Th...
DataStax
 
Webinar: Get On-Demand Education Anytime, Anywhere with Coursera and DataStax
Webinar: Get On-Demand Education Anytime, Anywhere with Coursera and DataStaxWebinar: Get On-Demand Education Anytime, Anywhere with Coursera and DataStax
Webinar: Get On-Demand Education Anytime, Anywhere with Coursera and DataStax
DataStax
 

Similar to Going native with Apache Cassandra (20)

Introduction to CQL and Data Modeling with Apache Cassandra
Introduction to CQL and Data Modeling with Apache CassandraIntroduction to CQL and Data Modeling with Apache Cassandra
Introduction to CQL and Data Modeling with Apache Cassandra
 
Helsinki Cassandra Meetup #2: Introduction to CQL3 and DataModeling
Helsinki Cassandra Meetup #2: Introduction to CQL3 and DataModelingHelsinki Cassandra Meetup #2: Introduction to CQL3 and DataModeling
Helsinki Cassandra Meetup #2: Introduction to CQL3 and DataModeling
 
Druid and Hive Together : Use Cases and Best Practices
Druid and Hive Together : Use Cases and Best PracticesDruid and Hive Together : Use Cases and Best Practices
Druid and Hive Together : Use Cases and Best Practices
 
BI, Reporting and Analytics on Apache Cassandra
BI, Reporting and Analytics on Apache CassandraBI, Reporting and Analytics on Apache Cassandra
BI, Reporting and Analytics on Apache Cassandra
 
Webinar: DataStax Enterprise 5.0 What’s New and How It’ll Make Your Life Easier
Webinar: DataStax Enterprise 5.0 What’s New and How It’ll Make Your Life EasierWebinar: DataStax Enterprise 5.0 What’s New and How It’ll Make Your Life Easier
Webinar: DataStax Enterprise 5.0 What’s New and How It’ll Make Your Life Easier
 
NoSQL's biggest lie: SQL never went away - Martin Esmann
NoSQL's biggest lie: SQL never went away - Martin EsmannNoSQL's biggest lie: SQL never went away - Martin Esmann
NoSQL's biggest lie: SQL never went away - Martin Esmann
 
Demystifying Data Warehouse as a Service (DWaaS)
Demystifying Data Warehouse as a Service (DWaaS)Demystifying Data Warehouse as a Service (DWaaS)
Demystifying Data Warehouse as a Service (DWaaS)
 
Johnny Miller – Cassandra + Spark = Awesome- NoSQL matters Barcelona 2014
Johnny Miller – Cassandra + Spark = Awesome- NoSQL matters Barcelona 2014Johnny Miller – Cassandra + Spark = Awesome- NoSQL matters Barcelona 2014
Johnny Miller – Cassandra + Spark = Awesome- NoSQL matters Barcelona 2014
 
Can Your Mobile Infrastructure Survive 1 Million Concurrent Users?
Can Your Mobile Infrastructure Survive 1 Million Concurrent Users?Can Your Mobile Infrastructure Survive 1 Million Concurrent Users?
Can Your Mobile Infrastructure Survive 1 Million Concurrent Users?
 
Webinar - QuerySurge and Azure DevOps in the Azure Cloud
 Webinar - QuerySurge and Azure DevOps in the Azure Cloud Webinar - QuerySurge and Azure DevOps in the Azure Cloud
Webinar - QuerySurge and Azure DevOps in the Azure Cloud
 
Spark + Cassandra = Real Time Analytics on Operational Data
Spark + Cassandra = Real Time Analytics on Operational DataSpark + Cassandra = Real Time Analytics on Operational Data
Spark + Cassandra = Real Time Analytics on Operational Data
 
Exploring sql server 2016
Exploring sql server 2016Exploring sql server 2016
Exploring sql server 2016
 
Exploring sql server 2016 bi
Exploring sql server 2016 biExploring sql server 2016 bi
Exploring sql server 2016 bi
 
Enterprise Architecture vs. Data Architecture
Enterprise Architecture vs. Data ArchitectureEnterprise Architecture vs. Data Architecture
Enterprise Architecture vs. Data Architecture
 
What We Learned About Cassandra While Building go90 (Christopher Webster & Th...
What We Learned About Cassandra While Building go90 (Christopher Webster & Th...What We Learned About Cassandra While Building go90 (Christopher Webster & Th...
What We Learned About Cassandra While Building go90 (Christopher Webster & Th...
 
High available BizTalk infrastructure on Azure IaaS
High available BizTalk infrastructure on Azure IaaSHigh available BizTalk infrastructure on Azure IaaS
High available BizTalk infrastructure on Azure IaaS
 
Delivering Apache Hadoop for the Modern Data Architecture
Delivering Apache Hadoop for the Modern Data Architecture Delivering Apache Hadoop for the Modern Data Architecture
Delivering Apache Hadoop for the Modern Data Architecture
 
ASHviz - Dats visualization research experiments using ASH data
ASHviz - Dats visualization research experiments using ASH dataASHviz - Dats visualization research experiments using ASH data
ASHviz - Dats visualization research experiments using ASH data
 
DataStax NYC Java Meetup: Cassandra with Java
DataStax NYC Java Meetup: Cassandra with JavaDataStax NYC Java Meetup: Cassandra with Java
DataStax NYC Java Meetup: Cassandra with Java
 
Webinar: Get On-Demand Education Anytime, Anywhere with Coursera and DataStax
Webinar: Get On-Demand Education Anytime, Anywhere with Coursera and DataStaxWebinar: Get On-Demand Education Anytime, Anywhere with Coursera and DataStax
Webinar: Get On-Demand Education Anytime, Anywhere with Coursera and DataStax
 

Recently uploaded

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Recently uploaded (20)

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 

Going native with Apache Cassandra

  • 1. Going Native With Apache Cassandra™ QCon London, 2014 www.datastax.com @DataStaxEMEA
  • 2. About Me ©2014 DataStax. Do not distribute without consent. 2 Johnny Miller Solutions Architect www.datastax.com @DataStaxEU @CyanMiller •  https://www.linkedin.com/in/johnnymiller
  • 3. DataStax ©2014 DataStax. Do not distribute without consent. 3 •  Founded in April 2010 •  We drive Apache Cassandra™ •  400+ customers (24 of the Fortune 100) •  220+ employees •  Contribute approximately 80% of the code to Cassandra •  Home to Apache Cassandra Chair & most committers •  Headquartered in San Francisco Bay area •  European headquarters established in London We are hiring www.datastax.com/careers
  • 4. What do I mean by going native? ©2014 DataStax. Do not distribute without consent. 4 •  Traditionally, Cassandra clients (Hector, Astynax1 etc..) were developed using Thrift •  With Cassandra 1.2 (Jan 2013) and the introduction of CQL3 and the CQL native protocol a new easier way of using Cassandra was introduced. •  Why? •  Easier to develop and model •  Best practices for building modern distributed applications •  Integrated tools and experience •  Enable Cassandra to evolve easier and support new features 1Astynax is being updated to include the native driver: https://github.com/Netflix/astyanax/wiki/Astyanax-over-Java-Driver
  • 5. CQL ©2014 DataStax. Do not distribute without consent. 5 •  Cassandra Query Language •  CQL is intended to provide a common, simpler and easier to use interface into Cassandra - and you probably already know it! e.g. SELECT * FROM users •  Usual statements •  CREATE / DROP / ALTER TABLE / SELECT
  • 6. Creating A Keyspace ©2014 DataStax. Do not distribute without consent. 6 CREATE KEYSPACE johnny WITH REPLICATION = {‘class’:’NetworkTopologyStrategy’, ‘USA’:3, ‘EU’: 2}; Node 1 1st copy Node 4 Node 5 Node 2 2nd copy Node 3 Node 1 1st copy Node 4 Node 5 Node 2 2nd copy Node 3 3rd copy DC: USA DC: EU
  • 7. CQL Basics ©2014 DataStax. Do not distribute without consent. 7 CREATE TABLE sporty_league ( team_name varchar, player_name varchar, jersey int, PRIMARY KEY (team_name, player_name) ); SELECT * FROM sporty_league WHERE team_name = ‘Mighty Mutts’ and player_name = ‘Lucky’; INSERT INTO sporty_league (team_name, player_name, jersey) VALUES ('Mighty Mutts',’Felix’, 90); Predicates •  On the partition key: = and IN •  On the cluster columns: <, <=, =, >=, >, IN
  • 8. Collections Data Type ©2014 DataStax. Do not distribute without consent. 8 •  CQL supports having columns that contain collections of data. •  The collection types include: •  Set, List and Map. •  Some performance considerations around collections. •  Sometimes more efficient to denormalise further rather than use collections if intending to store lots of data. •  Favour sets over list – more performant •  Watch out for collection indexing in Cassandra 2.1! CREATE TABLE collections_example ( id int PRIMARY KEY, set_example set<text>, list_example list<text>, map_example map<int, text> );
  • 9. Query Tracing ©2014 DataStax. Do not distribute without consent. 9 •  You can turn tracing on or off for queries with the TRACING ON | OFF command. •  This can help you understand what Cassandra is doing and identify any performance problems. Worth reading: http://www.datastax.com/dev/blog/tracing-in-cassandra-1-2
  • 10. Plus much, much more… ©2014 DataStax. Do not distribute without consent. 10 •  Light Weight Transactions INSERT INTO customer_account (customerID, customer_email) VALUES (‘LauraS’, ‘lauras@gmail.com’) IF NOT EXISTS; UPDATE customer_account SET customer_email=’laurass@gmail.com’ IF customer_email=’lauras@gmail.com’; •  Counters UPDATE UserActions SET total = total + 2 WHERE user = 123 AND action = ’xyz'; •  Time to live (TTL) INSERT INTO users (id, first, last) VALUES (‘abc123’, ‘abe’, ‘lincoln’) USING TTL 3600; •  Batch Statements BEGIN BATCH INSERT INTO users (userID, password, name) VALUES ('user2', 'ch@ngem3b', 'second user') UPDATE users SET password = 'ps22dhds' WHERE userID = 'user2' INSERT INTO users (userID, password) VALUES ('user3', 'ch@ngem3c') DELETE name FROM users WHERE userID = 'user2’ APPLY BATCH; •  New CQL features coming in Cassandra 2.0.6 •  http://www.datastax.com/dev/blog/cql-in-2-0-6
  • 12. Request Pipelining ©2014 DataStax. Do not distribute without consent. 12 With it: Without it:
  • 13. Notifications ©2014 DataStax. Do not distribute without consent. 13 Without Notifications With Notifications Polling Pushing Notifications are for technical events only: •  Topology changes •  Node status changes, •  Schema changes
  • 14. Asynchronous Architecture ©2014 DataStax. Do not distribute without consent. 14 Client Thread Client Thread Client Thread Driver 1 2 3 4 5 6
  • 16. Native Drivers ©2014 DataStax. Do not distribute without consent. 16 •  Java •  C# •  Python •  C++ (beta) •  ODBC (beta) •  Clojure •  Erlang •  Node.js •  Ruby •  Plus many, many more…. Get them here: http://www.datastax.com/download
  • 17. Connect and Write ©2014 DataStax. Do not distribute without consent. 17 Cluster cluster = Cluster.builder() .addContactPoints("10.158.02.40", "10.158.02.44") .build(); Session session = cluster.connect("akeyspace"); session.execute( "INSERT INTO user (username, password) ” + "VALUES(‘johnny’, ‘password1234’)" ); Note: Clusters and Sessions should be long-lived and re-used.
  • 18. Read from a table ©2014 DataStax. Do not distribute without consent. 18 ResultSet rs = session.execute("SELECT * FROM user"); List<Row> rows = rs.all(); for (Row row : rows) { String userName = row.getString("username"); String password = row.getString("password"); }
  • 19. Asynchronous Read ©2014 DataStax. Do not distribute without consent. 19 ResultSetFuture future = session.executeAsync( "SELECT * FROM user"); for (Row row : future.get()) { String userName = row.getString("username"); String password = row.getString("password"); } Note: The future returned implements Guava's ListenableFuture interface. This means you can use all Guava's Futures1 methods! 1http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/util/concurrent/Futures.html
  • 20. Read with Callbacks ©2014 DataStax. Do not distribute without consent. 20 final ResultSetFuture future = session.executeAsync("SELECT * FROM user"); future.addListener(new Runnable() { public void run() { for (Row row : future.get()) { String userName = row.getString("username"); String password = row.getString("password"); } } }, executor);
  • 21. Parallelize Calls ©2014 DataStax. Do not distribute without consent. 21 int queryCount = 99; List<ResultSetFuture> futures = new ArrayList<ResultSetFuture>(); for (int i=0; i<queryCount; i++) { futures.add( session.executeAsync("SELECT * FROM user " +"WHERE username = '"+i+"'")); } for(ResultSetFuture future : futures) { for (Row row : future.getUninterruptibly()) { //do something } }
  • 22. Tip ©2014 DataStax. Do not distribute without consent. 22 •  If you need to do a lot of work, it’s often better to make many small queries concurrently than to make one big query. •  executeAsync and Futures – makes this really easy! •  Big queries can put a high load on one coordinator •  Big queries can skew your 99th percentile latencies for other queries •  If one small query fails you can easily retry, if a big query than you have to retry the whole thing
  • 23. Prepared Statements ©2014 DataStax. Do not distribute without consent. 23 PreparedStatement statement = session.prepare( "INSERT INTO user (username, password) " + "VALUES (?, ?)"); BoundStatement bs = statement.bind(); bs.setString("username", "johnny"); bs.setString("password", "password1234"); session.execute(bs);
  • 24. Query Builder ©2014 DataStax. Do not distribute without consent. 24 Query query = QueryBuilder .select() .all() .from("akeyspace", "user") .where(eq("username", "johnny")); query.setConsistencyLevel(ConsistencyLevel.ONE); ResultSet rs = session.execute(query);
  • 25. Multi Data Center Load Balancing ©2014 DataStax. Do not distribute without consent. 25 •  Local nodes are queried first, if none are available the request will be sent to a remote data center Cluster cluster = Cluster.builder() .addContactPoints("10.158.02.40", "10.158.02.44") .withLoadBalancingPolicy( new DCAwareRoundRobinPolicy("DC1")) .build(); Name of the local DC
  • 26. Token Aware Load Balancing ©2014 DataStax. Do not distribute without consent. 26 •  Nodes that own a replica of the data being read or written by the query will be contacted first Cluster cluster = Cluster.builder() .addContactPoints("10.158.02.40", "10.158.02.44") .withLoadBalancingPolicy( new TokenAwarePolicy( new DCAwareRoundRobinPolicy("DC1"))) .build(); http://www.datastax.com/drivers/java/2.0/com/datastax/driver/core/policies/TokenAwarePolicy.html
  • 27. Retry Policies ©2014 DataStax. Do not distribute without consent. 27 •  This defined the behavior to adopt when a request returns a timeout or is unavailable. Cluster cluster = Cluster.builder() .addContactPoints("10.158.02.40", "10.158.02.44") .withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE) .withLoadBalancingPolicy(new TokenAwarePolicy(new DCAwareRoundRobinPolicy("DC1"))) .build(); •  DefaultRetryPolicy •  DowngradingConsistencyRetryPolicy •  FallthroughRetryPolicy •  LoggingRetryPolicy http://www.datastax.com/drivers/java/2.0/com/datastax/driver/core/policies/RetryPolicy.html
  • 28. Reconnection Policies ©2014 DataStax. Do not distribute without consent. 28 •  Policy that decides how often the reconnection to a dead node is attempted. Cluster cluster = Cluster.builder() .addContactPoints("10.158.02.40", "10.158.02.44”) .withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE) .withReconnectionPolicy(new ConstantReconnectionPolicy(1000)) .withLoadBalancingPolicy(new TokenAwarePolicy(new DCAwareRoundRobinPolicy("DC1"))) .build();
 •  ConstantReconnectionPolicy •  ExponentialReconnectionPolicy
  • 29. Automatic Paging ©2014 DataStax. Do not distribute without consent. 29 •  This was new in Cassandra 2.0 •  Previously – you would select data in batches
  • 30. Query Tracing ©2014 DataStax. Do not distribute without consent. 30 •  Tracing is enabled on a per-query basis. Query query = QueryBuilder .select() .all() .from("akeyspace", "user") .where(eq("username", "johnny")) .enableTracing(); ResultSet rs = session.execute(query); ExecutionInfo executionInfo = rs.getExecutionInfo(); QueryTrace queryTrace = executionInfo.getQueryTrace();
  • 31. DevCenter ©2014 DataStax. Do not distribute without consent. 31 •  Desktop app •  friendly, familiar, productive •  Free http://www.datastax.com/devcenter
  • 32. Find Out More ©2014 DataStax. Do not distribute without consent. 32 DataStax: •  http://www.datastax.com Getting Started: •  http://www.datastax.com/documentation/gettingstarted/index.html Training: •  http://www.datatstax.com/training Downloads: •  http://www.datastax.com/download Documentation: •  http://www.datastax.com/docs Developer Blog: •  http://www.datastax.com/dev/blog Community Site: •  http://planetcassandra.org Webinars: •  http://planetcassandra.org/Learn/CassandraCommunityWebinars
  • 33. ©2014 DataStax. Do not distribute without consent. 33