Cassandra Day Chicago 2015: Building Java Applications with Apache Cassandra

34
Java Applica)ons Building with Cassandra Aumcfshmvoe

Transcript of Cassandra Day Chicago 2015: Building Java Applications with Apache Cassandra

JavaApplica)ons

Building

with  Cassandra

Treat CassandraLike a Database

An AwesomeDatabase

• Always  on  • Horizontally  scalable  • Mul5ple  data  centers  • Cloud/hybrid    • Web/mobile/IoT

2000

4000

6000

8000

A000

C000

E000

0000

Client Machine

Load Balancer

tim name: Tim

role: teacher

kristenname: Kristen

role: marketing

billy role: CEO

matt name: Matt

role: founder

status: active

status: chill

CREATE TABLE product_browsing_data (product_id uuid, user_id uuid, clicked_at timestamp, serialized_browsing_data text, PRIMARY KEY (product_id, user_id, clicked_at) );

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

DataStax  Java  Driver

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

Every Use Casehas a Story

YourStory:

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

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");

YourStory:

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

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

YourStory:

2000

4000

6000

8000

A000

C000

E000

0000

Client Machine

ClusterInterface

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

ClusterInterface

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

YourStory:

2000

4000

6000

8000

A000

C000

E000

0000

Client Machine

2000

4000

6000

8000

A000

C000

E000

0000

Client Machine

1.2ms

2.9ms

15.3ms

Outsmar)ng  Yourself• Pick  one  node  • Send  all  coordina5on  traffic  to  that  node  • Hey,  maybe  make  it  a  fat  node!  • WhiteListPolicy  • PLEASE  DO  NOT  DO  THIS

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

WhiteListing

YourStory:

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

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...}

MapperObject

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

YourStory:

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

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

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

FutureResults

YourStory:

ThankYou!