31
Java Applica)ons Building with Cassandra Aumcfshmvoe

Cassandra Day Denver 2014: Building Java Applications with Apache Cassandra

Embed Size (px)

DESCRIPTION

Speaker: Tim Berglund, Global Director of Training at DataStax So you’re a JVM developer, you understand Cassandra’s architecture, and you’re on your way to knowing its data model well enough to build descriptive data models that perform well. What you need now is to know the Java Driver. What seems like an inconsequential library that proxies your application’s queries to your Cassandra cluster is actually a sophisticated piece of code that solves a lot of problems for you that early Cassandra developers had to code by hand. Come to this session to see features you might be missing and examples of how to use the Java driver in real applications.

Citation preview

Page 1: Cassandra Day Denver 2014: Building Java Applications with Apache Cassandra

JavaApplica)ons

Building

with  Cassandra

Page 2: Cassandra Day Denver 2014: Building Java Applications with Apache Cassandra

2000

4000

6000

8000

A000

C000

E000

0000

Client Machine

Load Balancer

Page 3: Cassandra Day Denver 2014: Building Java Applications with Apache Cassandra

tim name: Tim

role: teacher

kristenname: Kristen

role: marketing

billy role: CEO

matt name: Matt

role: founder

status: active

status: chill

Page 4: Cassandra Day Denver 2014: Building Java Applications with Apache Cassandra

https://github.com/datastax/java-­‐driver

Page 5: Cassandra Day Denver 2014: Building Java Applications with Apache Cassandra

DataStax  Java  Driver

• Open  source  (ASL  2.0)  • Where  CQL  happens  • Ac9vely  developed  • Now  faster  than  ThriD  • Doesn’t  make  you  want  to  die

Page 6: Cassandra Day Denver 2014: Building Java Applications with Apache Cassandra

Every Use Casehas a Story

Page 7: Cassandra Day Denver 2014: Building Java Applications with Apache Cassandra

YourStory:

Page 8: Cassandra Day Denver 2014: Building Java Applications with Apache Cassandra

apply plugin: 'java'repositories { jcenter()} dependencies { compile 'com.datastax.cassandra:cassandra-driver-core:2.1.2' testCompile 'org.testng:testng:6.8.8' testCompile 'junit:junit:4.11'}

Gradle Build

Page 9: Cassandra Day Denver 2014: Building Java Applications with Apache Cassandra

session.execute( "INSERT INTO sensor \ (sensor_id, reading, time) \ VALUES \ (24601, 2.171828, now())”);

ClusterInterfaceCluster cluster = Cluster.builder() .addContactPoints(“192.168.50.1”) .build(); Session session = cluster.connect(“IoT_keyspace");

Page 10: Cassandra Day Denver 2014: Building Java Applications with Apache Cassandra

YourStory:

Page 11: Cassandra Day Denver 2014: Building Java Applications with Apache Cassandra

ResultSet resultSet = session.execute("SELECT * FROM sensor");

List<Row> rows = resultSet.all();

for(Row row : rows) { String sensorId = row.getString("sensor_id" ); double reading = row.getDouble("reading" ); Date time = row.getDate("time" ); }

BasicQueries

Page 12: Cassandra Day Denver 2014: Building Java Applications with Apache Cassandra

PreparedStatement insertReading = session.prepare( "INSERT INTO sensor \ (sensor_id, reading, time) VALUES (?, ?, ?)” ); Statement statement = insertReading .bind(24601, 2.71828, new Date()) .setConsistencyLevel(ConsistencyLevel.QUORUM);

session.execute(statement);

PreparedStatements

Page 13: Cassandra Day Denver 2014: Building Java Applications with Apache Cassandra

YourStory:

Page 14: Cassandra Day Denver 2014: Building Java Applications with Apache Cassandra

2000

4000

6000

8000

A000

C000

E000

0000

Client Machine

Page 15: Cassandra Day Denver 2014: Building Java Applications with Apache Cassandra

ClusterInterface

Cluster cluster = Cluster.builder() .addContactPoints(“192.168.50.1”) .build();

Page 16: Cassandra Day Denver 2014: Building Java Applications with Apache Cassandra

ClusterInterface

Cluster cluster = Cluster.builder() .addContactPoints(“192.168.50.1”, “192.168.50.4”, “192.168.50.8”) .build();

Page 17: Cassandra Day Denver 2014: Building Java Applications with Apache Cassandra

YourStory:

Page 18: Cassandra Day Denver 2014: Building Java Applications with Apache Cassandra

2000

4000

6000

8000

A000

C000

E000

0000

Client Machine

Page 19: Cassandra Day Denver 2014: Building Java Applications with Apache Cassandra

2000

4000

6000

8000

A000

C000

E000

0000

Client Machine

1.2ms

2.9ms

15.3ms

Page 20: Cassandra Day Denver 2014: Building Java Applications with Apache Cassandra

Outsmar(ng  Yourself

• Pick  one  node  • Send  all  coordina9on  traffic  to  that  node  • Hey,  maybe  make  it  a  fat  node!  • WhiteListPolicy

Page 21: Cassandra Day Denver 2014: Building Java Applications with Apache Cassandra

List<InetSocketAddress> whiteList = new ArrayList<InetSocketAddress>();whiteList.add(...);Cluster c = Cluster.builder().withLoadBalancingPolicy( new WhiteListPolicy( new RoundRobinPolicy(), whiteList)).build();

WhiteListing

Page 22: Cassandra Day Denver 2014: Building Java Applications with Apache Cassandra

YourStory:

Page 23: Cassandra Day Denver 2014: Building Java Applications with Apache Cassandra

apply plugin: 'java'repositories { jcenter()} dependencies { compile 'com.datastax.cassandra:cassandra-driver-core:2.1.2' compile 'com.datastax.cassandra:cassandra-driver-mapping:2.1.2' testCompile 'org.testng:testng:6.8.8' testCompile 'junit:junit:4.11'}

MapperObject

Page 24: Cassandra Day Denver 2014: Building Java Applications with Apache Cassandra

CREATE TABLE sensor ( sensor_id int, reading double, time timestamp );

MapperObject

@UDT(keyspace = "IoT_keyspace", name = "sensor") public class Sensor { private int sensorId; private double reading; private Date Time; // getters and setters elided with extreme prejudice...}

Page 25: Cassandra Day Denver 2014: Building Java Applications with Apache Cassandra

MapperObject

MappingManager manager = new MappingManager(session);Mapper mapper = manager.mapper(Sensor.class);Sensor reading = mapper.get("24601");

Page 26: Cassandra Day Denver 2014: Building Java Applications with Apache Cassandra

YourStory:

Page 27: Cassandra Day Denver 2014: Building Java Applications with Apache Cassandra

ResultSetFuture future = session.executeAsync("SELECT * FROM sensor");

// Returns immediately// Go do productive things here…// Then finally block on the results when you must

ResultSet futureResults = future.get();List<Row> rows = resultSet.all();

FutureResults

Page 28: Cassandra Day Denver 2014: Building Java Applications with Apache Cassandra

Executor executor = Executors.newCachedThreadPool();ResultSetFuture future = session.executeAsync("SELECT * FROM sensor");

future.addListener(new Runnable() { public void run() { // Do the things here… } }, executor);

FutureResults

Page 29: Cassandra Day Denver 2014: Building Java Applications with Apache Cassandra

YourStory:

Page 30: Cassandra Day Denver 2014: Building Java Applications with Apache Cassandra
Page 31: Cassandra Day Denver 2014: Building Java Applications with Apache Cassandra

ThankYou!