SlideShare uma empresa Scribd logo
1 de 22
Baixar para ler offline
Cassandra for Java Developers 
DataStax Driver & DevCenter 
Michaël Figuière 
Drivers & Developer Tools Architect
Client / Server Communication 
© 2014 DataStax, All Rights Reserved. 
2 
Client 
Client 
Client 
Client 
Node 
Node Replica 
Replica 
Replica 
Node 
Coordinator node: 
Forwards all R/W requests 
to corresponding replicas
Request Pipelining 
© 2014 DataStax, All Rights Reserved. 
3 
Client 
Without 
Request Pipelining 
Cassandra 
Client Cassandra 
With 
Request Pipelining
Notifications 
© 2014 DataStax, All Rights Reserved. 
4 
Client 
Without 
Notifications 
With 
Notifications 
Node 
Node 
Node 
Client 
Node 
Node 
Node
Asynchronous Driver Architecture 
© 2014 DataStax, All Rights Reserved. 
5 
Client 
Thread 
Node 
Node 
Node 
Client 
Thread 
Client 
Thread 
Node 
Driver
Asynchronous Driver Architecture 
© 2014 DataStax, All Rights Reserved. 
6 
Client 
Thread 
Node 
Node 
Node 
Client 
Thread 
Client 
Thread 
Node 
6 
2 
3 
4 
5 
1 
Driver
Failover 
© 2014 DataStax, All Rights Reserved. 
7 
Client 
Thread 
Node 
Node 
Node 
Client 
Thread 
Client 
Thread 
Node 
7 
2 
4 
3 5 1 
Driver 
6
Java Driver Highlights 
• Reference implementation 
• Asynchronous architecture based on Netty 
• Prepared Statements Support 
• Automatic Failover 
• Node Discovery 
• Tunable Load Balancing 
• Round Robin, Multi Data Centers, Latency Awareness, Replica Awareness 
• Cassandra Tracing Support 
• Compression & SSL 
© 2014 DataStax, All Rights Reserved. 
8
DataStax Driver in Practice 
<dependency> 
<groupId>com.datastax.cassandra</groupId> 
<artifactId>cassandra-­‐driver-­‐core</artifactId> 
<version>2.1.0</version> 
</dependency> 
Available in Maven 
Central. Depends on 
Netty, Guava, Metrics 
Supports Apache Cassandra 1.2 to 2.1 
© 2014 DataStax, All Rights Reserved. 9
Connect and Write 
Cluster cluster = Cluster.builder() 
.addContactPoints("127.0.0.1", “127.0.0.2") 
.build(); 
Session session = cluster.connect(“my_keyspace"); 
session.execute( 
"INSERT INTO user (user_id, name, email) 
VALUES (12345, 'johndoe', 'john@doe.com')" 
); 
The rest of the 
nodes will be 
discovered by 
the driver 
A keyspace is 
just like a 
schema in the 
SQL world 
© 2014 DataStax, All Rights Reserved. 10
Read 
Session is a thread safe 
object. A singleton should 
be instantiated at startup 
ResultSet resultSet = session.execute("SELECT * FROM user"); 
List<Row> rows = resultSet.all(); 
for (Row row : rows) { 
String userId = row.getString("user_id"); 
String name = row.getString("name"); 
String email = row.getString("email"); 
} 
Actually ResultSet also 
implements Iterable<Row> 
© 2014 DataStax, All Rights Reserved. 11
Write with Prepared Statements 
PreparedStatement objects 
are also threadsafe, just create 
a singleton at startup 
PreparedStatement insertUser = session.prepare( 
"INSERT INTO user (user_id, name, email) 
VALUES (?, ?, ?)" 
); 
BoundStatement statement = insertUser 
.bind(12345, "johndoe", "john@doe.com") 
.setConsistencyLevel(ConsistencyLevel.QUORUM); 
session.execute(statement); 
Parameters can 
be named as well 
BoundStatement 
is a stateful, NON 
threadsafe object 
Consistency Level can be 
set for each statement 
© 2014 DataStax, All Rights Reserved. 12
Asynchronous Read 
ResultSetFuture future = 
session.executeAsync("SELECT * FROM user"); 
ResultSet resultSet = future.get(); 
List<Row> rows = resultSet.all(); 
for (Row row : rows) { 
String userId = row.getString("user_id"); 
String name = row.getString("name"); 
String email = row.getString("email"); 
} 
Will NOT block unless 
all the connections are 
busy 
Will block until less all 
the connections are 
busy 
© 2014 DataStax, All Rights Reserved. 13
Asynchronous Read with Callbacks 
ResultSetFuture future = 
session.executeAsync("SELECT * FROM user"); 
future.addListener(new Runnable() { 
public void run() { 
// Process the results here 
} 
}, executor); 
ResultSetFuture 
implements Guava’s 
ListenableFuture 
executor = 
Executors 
.newCachedThreadPool(); 
executor = 
MoreExecutors 
.sameThreadExecutor(); 
Only if your listener code 
is trivial and non blocking 
as it’ll be executed in the 
IO Thread 
…Or any thread pool that 
you prefer 
© 2014 DataStax, All Rights Reserved. 14
Query Builder 
import static of 
QueryBuilder is required in 
order to use the DSL 
import static 
com.datastax.driver.core.querybuilder.QueryBuilder.*; 
Statement selectAll = 
select().all().from("user").where(eq("user_id", userId)); 
session.execute(selectAll); 
Statement insert = insertInto("user") 
.value("user_id", 2) 
.value("name", "johndoe") 
.value("email", "john@doe.com"); 
session.execute(insert); 
© 2014 DataStax, All Rights Reserved. 15
Object Mapper 
• Avoid boilerplate for common use cases 
• Map Objects to Statements and ResultSets to Objects 
• Do NOT hide Cassandra from the developer 
• No “clever tricks” à la Hibernate 
• Not JPA compatible, but JPA-ish API 
© 2014 DataStax, All Rights Reserved. 
16
Object Mapper in Practice 
<dependency> 
<groupId>com.datastax.cassandra</groupId> 
<artifactId>cassandra-­‐driver-­‐mapping</artifactId> 
<version>2.1.0</version> 
</dependency> 
Additional artifact for 
object mapping 
Available from Driver 2.1.0 
© 2014 DataStax, All Rights Reserved. 17
Basic Object Mapping 
CREATE 
TYPE 
address 
( 
street 
text, 
city 
text, 
zip 
int 
); 
CREATE 
TABLE 
users 
( 
email 
text 
PRIMARY 
KEY, 
address 
address 
); 
@UDT(keyspace 
= 
"ks", 
name 
= 
"address") 
public 
class 
Address 
{ 
private 
String 
street; 
private 
String 
city; 
private 
int 
zip; 
// 
getters 
and 
setters 
omitted... 
} 
@Table(keyspace 
= 
"ks", 
name 
= 
"users") 
public 
class 
User 
{ 
@PartitionKey 
private 
String 
email; 
private 
Address 
address; 
// 
getters 
and 
setters 
omitted... 
} 
© 2014 DataStax, All Rights Reserved. 18
Basic Object Mapping 
MappingManager 
manager 
= 
new 
MappingManager(session); 
Mapper 
mapper 
= 
manager.mapper(User.class); 
UserProfile 
myProfile 
= 
mapper.get("xyz@example.com"); 
ListenableFuture 
saveFuture 
= 
mapper.saveAsync(anotherProfile); 
mapper.delete("xyz@example.com"); 
Mapper, just like Session, is 
a thread-safe object. Create 
a singleton at startup. 
get() returns a mapped row 
for the given Primary Key 
ListenableFuture from 
Guava. Completed when the 
write is acknowledged. 
© 2014 DataStax, All Rights Reserved. 19
Accessors 
@Accessor 
interface 
UserAccessor 
{ 
@Query("SELECT 
* 
FROM 
user_profiles 
LIMIT 
:max") 
Result<User> 
firstN(@Param("max") 
int 
limit); 
} 
UserAccessor 
accessor 
= 
manager.createAccessor(UserAccessor.class); 
Result<User> 
users 
= 
accessor.firstN(10); 
for 
(User 
user 
: 
users) 
{ 
System.out.println( 
profile.getAddress().getZip() 
); 
} 
Result is like ResultSet 
but specialized for a 
mapped class… 
…so we iterate over it 
just like we would with a 
ResultSet 
© 2014 DataStax, All Rights Reserved. 20
DataStax DevCenter 
Demo
We’re Hiring! 
github.com/datastax 
blog.datastax.com 
@mfiguiere

Mais conteúdo relacionado

Mais procurados

06 response-headers
06 response-headers06 response-headers
06 response-headers
snopteck
 
Cassandra summit 2013 - DataStax Java Driver Unleashed!
Cassandra summit 2013 - DataStax Java Driver Unleashed!Cassandra summit 2013 - DataStax Java Driver Unleashed!
Cassandra summit 2013 - DataStax Java Driver Unleashed!
Michaël Figuière
 
High Throughput Analytics with Cassandra & Azure
High Throughput Analytics with Cassandra & AzureHigh Throughput Analytics with Cassandra & Azure
High Throughput Analytics with Cassandra & Azure
DataStax Academy
 
Php user groupmemcached
Php user groupmemcachedPhp user groupmemcached
Php user groupmemcached
Jason Anderson
 

Mais procurados (19)

Apache ZooKeeper
Apache ZooKeeperApache ZooKeeper
Apache ZooKeeper
 
06 response-headers
06 response-headers06 response-headers
06 response-headers
 
Cassandra summit 2013 - DataStax Java Driver Unleashed!
Cassandra summit 2013 - DataStax Java Driver Unleashed!Cassandra summit 2013 - DataStax Java Driver Unleashed!
Cassandra summit 2013 - DataStax Java Driver Unleashed!
 
Winter is coming? Not if ZooKeeper is there!
Winter is coming? Not if ZooKeeper is there!Winter is coming? Not if ZooKeeper is there!
Winter is coming? Not if ZooKeeper is there!
 
Hazelcast
HazelcastHazelcast
Hazelcast
 
Cassandra Day NY 2014: Getting Started with the DataStax C# Driver
Cassandra Day NY 2014: Getting Started with the DataStax C# DriverCassandra Day NY 2014: Getting Started with the DataStax C# Driver
Cassandra Day NY 2014: Getting Started with the DataStax C# Driver
 
SCWCD : Thread safe servlets : CHAP : 8
SCWCD : Thread safe servlets : CHAP : 8SCWCD : Thread safe servlets : CHAP : 8
SCWCD : Thread safe servlets : CHAP : 8
 
Jafka guide
Jafka guideJafka guide
Jafka guide
 
Cassandra 2.0 better, faster, stronger
Cassandra 2.0   better, faster, strongerCassandra 2.0   better, faster, stronger
Cassandra 2.0 better, faster, stronger
 
Ice mini guide
Ice mini guideIce mini guide
Ice mini guide
 
Async servers and clients in Rest.li
Async servers and clients in Rest.liAsync servers and clients in Rest.li
Async servers and clients in Rest.li
 
Zookeeper
ZookeeperZookeeper
Zookeeper
 
High Throughput Analytics with Cassandra & Azure
High Throughput Analytics with Cassandra & AzureHigh Throughput Analytics with Cassandra & Azure
High Throughput Analytics with Cassandra & Azure
 
Hazelcast
HazelcastHazelcast
Hazelcast
 
Php user groupmemcached
Php user groupmemcachedPhp user groupmemcached
Php user groupmemcached
 
Cassandra 3.0 advanced preview
Cassandra 3.0 advanced previewCassandra 3.0 advanced preview
Cassandra 3.0 advanced preview
 
Hadoop Puzzlers
Hadoop PuzzlersHadoop Puzzlers
Hadoop Puzzlers
 
Beyond the Query: A Cassandra + Solr + Spark Love Triangle Using Datastax Ent...
Beyond the Query: A Cassandra + Solr + Spark Love Triangle Using Datastax Ent...Beyond the Query: A Cassandra + Solr + Spark Love Triangle Using Datastax Ent...
Beyond the Query: A Cassandra + Solr + Spark Love Triangle Using Datastax Ent...
 
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
 

Semelhante a Geneva JUG - Cassandra for Java Developers

Writing robust Node.js applications
Writing robust Node.js applicationsWriting robust Node.js applications
Writing robust Node.js applications
Tom Croucher
 
Networking and Data Access with Eqela
Networking and Data Access with EqelaNetworking and Data Access with Eqela
Networking and Data Access with Eqela
jobandesther
 
Chatting dengan beberapa pc laptop
Chatting dengan beberapa pc laptopChatting dengan beberapa pc laptop
Chatting dengan beberapa pc laptop
yayaria
 
Node js introduction
Node js introductionNode js introduction
Node js introduction
Alex Su
 

Semelhante a Geneva JUG - Cassandra for Java Developers (20)

#3 (Multi Threads With TCP)
#3 (Multi Threads With TCP)#3 (Multi Threads With TCP)
#3 (Multi Threads With TCP)
 
Building and Scaling Node.js Applications
Building and Scaling Node.js ApplicationsBuilding and Scaling Node.js Applications
Building and Scaling Node.js Applications
 
Apache Cassandra and Drivers
Apache Cassandra and DriversApache Cassandra and Drivers
Apache Cassandra and Drivers
 
Ruby Driver Explained: DataStax Webinar May 5th 2015
Ruby Driver Explained: DataStax Webinar May 5th 2015Ruby Driver Explained: DataStax Webinar May 5th 2015
Ruby Driver Explained: DataStax Webinar May 5th 2015
 
Writing robust Node.js applications
Writing robust Node.js applicationsWriting robust Node.js applications
Writing robust Node.js applications
 
Cassandra Summit 2014: Highly Scalable Web Application in the Cloud with Cass...
Cassandra Summit 2014: Highly Scalable Web Application in the Cloud with Cass...Cassandra Summit 2014: Highly Scalable Web Application in the Cloud with Cass...
Cassandra Summit 2014: Highly Scalable Web Application in the Cloud with Cass...
 
soft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.jssoft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.js
 
Networking and Data Access with Eqela
Networking and Data Access with EqelaNetworking and Data Access with Eqela
Networking and Data Access with Eqela
 
Advance Java-Network Programming
Advance Java-Network ProgrammingAdvance Java-Network Programming
Advance Java-Network Programming
 
Chatting dengan beberapa pc laptop
Chatting dengan beberapa pc laptopChatting dengan beberapa pc laptop
Chatting dengan beberapa pc laptop
 
Developing web applications in Rust
Developing web applications in RustDeveloping web applications in Rust
Developing web applications in Rust
 
How to make a high-quality Node.js app, Nikita Galkin
How to make a high-quality Node.js app, Nikita GalkinHow to make a high-quality Node.js app, Nikita Galkin
How to make a high-quality Node.js app, Nikita Galkin
 
JAX London 2015: Java vs Nodejs
JAX London 2015: Java vs NodejsJAX London 2015: Java vs Nodejs
JAX London 2015: Java vs Nodejs
 
C* Summit EU 2013: Cassandra Made Simple with CQL Drivers and DevCenter
C* Summit EU 2013: Cassandra Made Simple with CQL Drivers and DevCenter C* Summit EU 2013: Cassandra Made Simple with CQL Drivers and DevCenter
C* Summit EU 2013: Cassandra Made Simple with CQL Drivers and DevCenter
 
Hazelcast and MongoDB at Cloud CMS
Hazelcast and MongoDB at Cloud CMSHazelcast and MongoDB at Cloud CMS
Hazelcast and MongoDB at Cloud CMS
 
Server side JavaScript: going all the way
Server side JavaScript: going all the wayServer side JavaScript: going all the way
Server side JavaScript: going all the way
 
Java vs. Java Script for enterprise web applications - Chris Bailey
Java vs. Java Script for enterprise web applications - Chris BaileyJava vs. Java Script for enterprise web applications - Chris Bailey
Java vs. Java Script for enterprise web applications - Chris Bailey
 
Node js introduction
Node js introductionNode js introduction
Node js introduction
 
DevOps Enabling Your Team
DevOps Enabling Your TeamDevOps Enabling Your Team
DevOps Enabling Your Team
 
Testing in android
Testing in androidTesting in android
Testing in android
 

Mais de Michaël Figuière

Paris Cassandra Meetup - Overview of New Cassandra Drivers
Paris Cassandra Meetup - Overview of New Cassandra DriversParis Cassandra Meetup - Overview of New Cassandra Drivers
Paris Cassandra Meetup - Overview of New Cassandra Drivers
Michaël Figuière
 
ApacheCon Europe 2012 - Real Time Big Data in practice with Cassandra
ApacheCon Europe 2012 - Real Time Big Data in practice with CassandraApacheCon Europe 2012 - Real Time Big Data in practice with Cassandra
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
NoSQL Matters 2012 - Real Time Big Data in practice with CassandraNoSQL Matters 2012 - Real Time Big Data in practice with Cassandra
NoSQL Matters 2012 - Real Time Big Data in practice with Cassandra
Michaël Figuière
 
GTUG Nantes (Dec 2011) - BigTable et NoSQL
GTUG Nantes (Dec 2011) - BigTable et NoSQLGTUG Nantes (Dec 2011) - BigTable et NoSQL
GTUG Nantes (Dec 2011) - BigTable et NoSQL
Michaël Figuière
 
Duchess France (Nov 2011) - Atelier Apache Mahout
Duchess France (Nov 2011) - Atelier Apache MahoutDuchess France (Nov 2011) - Atelier Apache Mahout
Duchess France (Nov 2011) - Atelier Apache Mahout
Michaël Figuière
 
JUG Summer Camp (Sep 2011) - Les applications et architectures d’entreprise d...
JUG Summer Camp (Sep 2011) - Les applications et architectures d’entreprise d...JUG Summer Camp (Sep 2011) - Les applications et architectures d’entreprise d...
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
BreizhCamp (Jun 2011) - Haute disponibilité et élasticité avec CassandraBreizhCamp (Jun 2011) - Haute disponibilité et élasticité avec Cassandra
BreizhCamp (Jun 2011) - Haute disponibilité et élasticité avec Cassandra
Michaël Figuière
 
Mix-IT (Apr 2011) - Intelligence Collective avec Apache Mahout
Mix-IT (Apr 2011) - Intelligence Collective avec Apache MahoutMix-IT (Apr 2011) - Intelligence Collective avec Apache Mahout
Mix-IT (Apr 2011) - Intelligence Collective avec Apache Mahout
Michaël Figuière
 
Xebia Knowledge Exchange (mars 2011) - Machine Learning with Apache Mahout
Xebia Knowledge Exchange (mars 2011) - Machine Learning with Apache MahoutXebia Knowledge Exchange (mars 2011) - Machine Learning with Apache Mahout
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
Breizh JUG (mar 2011) - NoSQL : Des Grands du Web aux EntreprisesBreizh JUG (mar 2011) - NoSQL : Des Grands du Web aux Entreprises
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
FOSDEM (feb 2011) -  A real-time search engine with Lucene and S4FOSDEM (feb 2011) -  A real-time search engine with Lucene and S4
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
Xebia Knowledge Exchange (feb 2011) - Large Scale Web DevelopmentXebia Knowledge Exchange (feb 2011) - Large Scale Web Development
Xebia Knowledge Exchange (feb 2011) - Large Scale Web Development
Michaël Figuière
 
Xebia Knowledge Exchange (jan 2011) - Trends in Enterprise Applications Archi...
Xebia Knowledge Exchange (jan 2011) - Trends in Enterprise Applications Archi...Xebia Knowledge Exchange (jan 2011) - Trends in Enterprise Applications Archi...
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
Lorraine JUG (dec 2010) - NoSQL, des grands du Web aux entreprisesLorraine JUG (dec 2010) - NoSQL, des grands du Web aux entreprises
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
Tours JUG (oct 2010) - NoSQL, des grands du Web aux entreprisesTours JUG (oct 2010) - NoSQL, des grands du Web aux entreprises
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é
Paris JUG (sept 2010) - NoSQL : Des concepts à la réalitéParis JUG (sept 2010) - NoSQL : Des concepts à la réalité
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
Xebia Knowledge Exchange (mars 2010) - Lucene : From theory to real worldXebia Knowledge Exchange (mars 2010) - Lucene : From theory to real world
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...
Xebia Knowledge Exchange (may 2010) - NoSQL : Using the right tool for the ri...Xebia Knowledge Exchange (may 2010) - NoSQL : Using the right tool for the ri...
Xebia Knowledge Exchange (may 2010) - NoSQL : Using the right tool for the ri...
Michaël Figuière
 

Mais de Michaël Figuière (18)

Paris Cassandra Meetup - Overview of New Cassandra Drivers
Paris Cassandra Meetup - Overview of New Cassandra DriversParis Cassandra Meetup - Overview of New Cassandra Drivers
Paris Cassandra Meetup - Overview of New Cassandra Drivers
 
ApacheCon Europe 2012 - Real Time Big Data in practice with Cassandra
ApacheCon Europe 2012 - Real Time Big Data in practice with CassandraApacheCon Europe 2012 - Real Time Big Data in practice with Cassandra
ApacheCon Europe 2012 - Real Time Big Data in practice with Cassandra
 
NoSQL Matters 2012 - Real Time Big Data in practice with Cassandra
NoSQL Matters 2012 - Real Time Big Data in practice with CassandraNoSQL Matters 2012 - Real Time Big Data in practice with Cassandra
NoSQL Matters 2012 - Real Time Big Data in practice with Cassandra
 
GTUG Nantes (Dec 2011) - BigTable et NoSQL
GTUG Nantes (Dec 2011) - BigTable et NoSQLGTUG Nantes (Dec 2011) - BigTable et NoSQL
GTUG Nantes (Dec 2011) - BigTable et NoSQL
 
Duchess France (Nov 2011) - Atelier Apache Mahout
Duchess France (Nov 2011) - Atelier Apache MahoutDuchess France (Nov 2011) - Atelier Apache Mahout
Duchess France (Nov 2011) - Atelier Apache Mahout
 
JUG Summer Camp (Sep 2011) - Les applications et architectures d’entreprise d...
JUG Summer Camp (Sep 2011) - Les applications et architectures d’entreprise d...JUG Summer Camp (Sep 2011) - Les applications et architectures d’entreprise d...
JUG Summer Camp (Sep 2011) - Les applications et architectures d’entreprise d...
 
BreizhCamp (Jun 2011) - Haute disponibilité et élasticité avec Cassandra
BreizhCamp (Jun 2011) - Haute disponibilité et élasticité avec CassandraBreizhCamp (Jun 2011) - Haute disponibilité et élasticité avec Cassandra
BreizhCamp (Jun 2011) - Haute disponibilité et élasticité avec Cassandra
 
Mix-IT (Apr 2011) - Intelligence Collective avec Apache Mahout
Mix-IT (Apr 2011) - Intelligence Collective avec Apache MahoutMix-IT (Apr 2011) - Intelligence Collective avec Apache Mahout
Mix-IT (Apr 2011) - Intelligence Collective avec Apache Mahout
 
Xebia Knowledge Exchange (mars 2011) - Machine Learning with Apache Mahout
Xebia Knowledge Exchange (mars 2011) - Machine Learning with Apache MahoutXebia Knowledge Exchange (mars 2011) - Machine Learning with Apache Mahout
Xebia Knowledge Exchange (mars 2011) - Machine Learning with Apache Mahout
 
Breizh JUG (mar 2011) - NoSQL : Des Grands du Web aux Entreprises
Breizh JUG (mar 2011) - NoSQL : Des Grands du Web aux EntreprisesBreizh JUG (mar 2011) - NoSQL : Des Grands du Web aux Entreprises
Breizh JUG (mar 2011) - NoSQL : Des Grands du Web aux Entreprises
 
FOSDEM (feb 2011) - A real-time search engine with Lucene and S4
FOSDEM (feb 2011) -  A real-time search engine with Lucene and S4FOSDEM (feb 2011) -  A real-time search engine with Lucene and S4
FOSDEM (feb 2011) - A real-time search engine with Lucene and S4
 
Xebia Knowledge Exchange (feb 2011) - Large Scale Web Development
Xebia Knowledge Exchange (feb 2011) - Large Scale Web DevelopmentXebia Knowledge Exchange (feb 2011) - Large Scale Web Development
Xebia Knowledge Exchange (feb 2011) - Large Scale Web Development
 
Xebia Knowledge Exchange (jan 2011) - Trends in Enterprise Applications Archi...
Xebia Knowledge Exchange (jan 2011) - Trends in Enterprise Applications Archi...Xebia Knowledge Exchange (jan 2011) - Trends in Enterprise Applications Archi...
Xebia Knowledge Exchange (jan 2011) - Trends in Enterprise Applications Archi...
 
Lorraine JUG (dec 2010) - NoSQL, des grands du Web aux entreprises
Lorraine JUG (dec 2010) - NoSQL, des grands du Web aux entreprisesLorraine JUG (dec 2010) - NoSQL, des grands du Web aux entreprises
Lorraine JUG (dec 2010) - NoSQL, des grands du Web aux entreprises
 
Tours JUG (oct 2010) - NoSQL, des grands du Web aux entreprises
Tours JUG (oct 2010) - NoSQL, des grands du Web aux entreprisesTours JUG (oct 2010) - NoSQL, des grands du Web aux entreprises
Tours JUG (oct 2010) - NoSQL, des grands du Web aux entreprises
 
Paris JUG (sept 2010) - NoSQL : Des concepts à la réalité
Paris JUG (sept 2010) - NoSQL : Des concepts à la réalitéParis JUG (sept 2010) - NoSQL : Des concepts à la réalité
Paris JUG (sept 2010) - NoSQL : Des concepts à la réalité
 
Xebia Knowledge Exchange (mars 2010) - Lucene : From theory to real world
Xebia Knowledge Exchange (mars 2010) - Lucene : From theory to real worldXebia Knowledge Exchange (mars 2010) - Lucene : From theory to real world
Xebia Knowledge Exchange (mars 2010) - Lucene : From theory to real world
 
Xebia Knowledge Exchange (may 2010) - NoSQL : Using the right tool for the ri...
Xebia Knowledge Exchange (may 2010) - NoSQL : Using the right tool for the ri...Xebia Knowledge Exchange (may 2010) - NoSQL : Using the right tool for the ri...
Xebia Knowledge Exchange (may 2010) - NoSQL : Using the right tool for the ri...
 

Último

UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
rknatarajan
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Dr.Costas Sachpazis
 

Último (20)

UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 

Geneva JUG - Cassandra for Java Developers

  • 1. Cassandra for Java Developers DataStax Driver & DevCenter Michaël Figuière Drivers & Developer Tools Architect
  • 2. Client / Server Communication © 2014 DataStax, All Rights Reserved. 2 Client Client Client Client Node Node Replica Replica Replica Node Coordinator node: Forwards all R/W requests to corresponding replicas
  • 3. Request Pipelining © 2014 DataStax, All Rights Reserved. 3 Client Without Request Pipelining Cassandra Client Cassandra With Request Pipelining
  • 4. Notifications © 2014 DataStax, All Rights Reserved. 4 Client Without Notifications With Notifications Node Node Node Client Node Node Node
  • 5. Asynchronous Driver Architecture © 2014 DataStax, All Rights Reserved. 5 Client Thread Node Node Node Client Thread Client Thread Node Driver
  • 6. Asynchronous Driver Architecture © 2014 DataStax, All Rights Reserved. 6 Client Thread Node Node Node Client Thread Client Thread Node 6 2 3 4 5 1 Driver
  • 7. Failover © 2014 DataStax, All Rights Reserved. 7 Client Thread Node Node Node Client Thread Client Thread Node 7 2 4 3 5 1 Driver 6
  • 8. Java Driver Highlights • Reference implementation • Asynchronous architecture based on Netty • Prepared Statements Support • Automatic Failover • Node Discovery • Tunable Load Balancing • Round Robin, Multi Data Centers, Latency Awareness, Replica Awareness • Cassandra Tracing Support • Compression & SSL © 2014 DataStax, All Rights Reserved. 8
  • 9. DataStax Driver in Practice <dependency> <groupId>com.datastax.cassandra</groupId> <artifactId>cassandra-­‐driver-­‐core</artifactId> <version>2.1.0</version> </dependency> Available in Maven Central. Depends on Netty, Guava, Metrics Supports Apache Cassandra 1.2 to 2.1 © 2014 DataStax, All Rights Reserved. 9
  • 10. Connect and Write Cluster cluster = Cluster.builder() .addContactPoints("127.0.0.1", “127.0.0.2") .build(); Session session = cluster.connect(“my_keyspace"); session.execute( "INSERT INTO user (user_id, name, email) VALUES (12345, 'johndoe', 'john@doe.com')" ); The rest of the nodes will be discovered by the driver A keyspace is just like a schema in the SQL world © 2014 DataStax, All Rights Reserved. 10
  • 11. Read Session is a thread safe object. A singleton should be instantiated at startup ResultSet resultSet = session.execute("SELECT * FROM user"); List<Row> rows = resultSet.all(); for (Row row : rows) { String userId = row.getString("user_id"); String name = row.getString("name"); String email = row.getString("email"); } Actually ResultSet also implements Iterable<Row> © 2014 DataStax, All Rights Reserved. 11
  • 12. Write with Prepared Statements PreparedStatement objects are also threadsafe, just create a singleton at startup PreparedStatement insertUser = session.prepare( "INSERT INTO user (user_id, name, email) VALUES (?, ?, ?)" ); BoundStatement statement = insertUser .bind(12345, "johndoe", "john@doe.com") .setConsistencyLevel(ConsistencyLevel.QUORUM); session.execute(statement); Parameters can be named as well BoundStatement is a stateful, NON threadsafe object Consistency Level can be set for each statement © 2014 DataStax, All Rights Reserved. 12
  • 13. Asynchronous Read ResultSetFuture future = session.executeAsync("SELECT * FROM user"); ResultSet resultSet = future.get(); List<Row> rows = resultSet.all(); for (Row row : rows) { String userId = row.getString("user_id"); String name = row.getString("name"); String email = row.getString("email"); } Will NOT block unless all the connections are busy Will block until less all the connections are busy © 2014 DataStax, All Rights Reserved. 13
  • 14. Asynchronous Read with Callbacks ResultSetFuture future = session.executeAsync("SELECT * FROM user"); future.addListener(new Runnable() { public void run() { // Process the results here } }, executor); ResultSetFuture implements Guava’s ListenableFuture executor = Executors .newCachedThreadPool(); executor = MoreExecutors .sameThreadExecutor(); Only if your listener code is trivial and non blocking as it’ll be executed in the IO Thread …Or any thread pool that you prefer © 2014 DataStax, All Rights Reserved. 14
  • 15. Query Builder import static of QueryBuilder is required in order to use the DSL import static com.datastax.driver.core.querybuilder.QueryBuilder.*; Statement selectAll = select().all().from("user").where(eq("user_id", userId)); session.execute(selectAll); Statement insert = insertInto("user") .value("user_id", 2) .value("name", "johndoe") .value("email", "john@doe.com"); session.execute(insert); © 2014 DataStax, All Rights Reserved. 15
  • 16. Object Mapper • Avoid boilerplate for common use cases • Map Objects to Statements and ResultSets to Objects • Do NOT hide Cassandra from the developer • No “clever tricks” à la Hibernate • Not JPA compatible, but JPA-ish API © 2014 DataStax, All Rights Reserved. 16
  • 17. Object Mapper in Practice <dependency> <groupId>com.datastax.cassandra</groupId> <artifactId>cassandra-­‐driver-­‐mapping</artifactId> <version>2.1.0</version> </dependency> Additional artifact for object mapping Available from Driver 2.1.0 © 2014 DataStax, All Rights Reserved. 17
  • 18. Basic Object Mapping CREATE TYPE address ( street text, city text, zip int ); CREATE TABLE users ( email text PRIMARY KEY, address address ); @UDT(keyspace = "ks", name = "address") public class Address { private String street; private String city; private int zip; // getters and setters omitted... } @Table(keyspace = "ks", name = "users") public class User { @PartitionKey private String email; private Address address; // getters and setters omitted... } © 2014 DataStax, All Rights Reserved. 18
  • 19. Basic Object Mapping MappingManager manager = new MappingManager(session); Mapper mapper = manager.mapper(User.class); UserProfile myProfile = mapper.get("xyz@example.com"); ListenableFuture saveFuture = mapper.saveAsync(anotherProfile); mapper.delete("xyz@example.com"); Mapper, just like Session, is a thread-safe object. Create a singleton at startup. get() returns a mapped row for the given Primary Key ListenableFuture from Guava. Completed when the write is acknowledged. © 2014 DataStax, All Rights Reserved. 19
  • 20. Accessors @Accessor interface UserAccessor { @Query("SELECT * FROM user_profiles LIMIT :max") Result<User> firstN(@Param("max") int limit); } UserAccessor accessor = manager.createAccessor(UserAccessor.class); Result<User> users = accessor.firstN(10); for (User user : users) { System.out.println( profile.getAddress().getZip() ); } Result is like ResultSet but specialized for a mapped class… …so we iterate over it just like we would with a ResultSet © 2014 DataStax, All Rights Reserved. 20
  • 22. We’re Hiring! github.com/datastax blog.datastax.com @mfiguiere