4. @chbatey
Existing experience?
• JDBC?
- Direct Cassandra Java API very similar
• Spring data?
- Support for Cassandra
• Hibernate?
- Lightweight mapping API bundled with the driver
8. @chbatey
Queries
ResultSet rows = session.execute("select * from auction”);
// gets all the rows
List<Row> all = rows.all();
// can be used in fall looks etc
Iterator<Row> iterator = rows.iterator();
// Mapping using the streams API
List<Auction> auctionStream = all.stream().map(row -> new Auction(
row.getString("name"),
row.getString("owner"),
Instant.ofEpochMilli(row.getLong("ends"))))
.collect(Collectors.toList());
9. @chbatey
Prepared Statements
// Prepare the statement once
PreparedStatement createAuction = session.prepare("insert INTO auctions
(name, owner, ends) VALUES (?, ?, ?)");
// Execute it many times
BoundStatement boundStatement = createAuction.bind("chris", "batey", Instant.now());
ResultSet result = session.execute(boundStatement);
17. @chbatey
Guava Listenable Future
public ResultSetFuture getBids(String name) {
return session.executeAsync("select * from auction_bids where name = ?", name);
}
ResultSetFuture bids = auctionDao.getBids("Cassandra T-Shirt");
// blocks for ever, pure future method
ResultSet rows = bids.get();
// blocks for ever, removes ExecutionException
ResultSet uninterruptibly = bids.getUninterruptibly();
uninterruptibly.all().forEach(System.out::println);
18. @chbatey
Guava Listenable Future
public ResultSetFuture getBids(String name) {
return session.executeAsync("select * from auction_bids where name = ?", name);
}
//call backs
Futures.addCallback(bids, new FutureCallback<ResultSet>() {
@Override
public void onSuccess(ResultSet result) {
// do something
}
@Override
public void onFailure(Throwable t) {
// sad panda :(
}
});
19. @chbatey
Multiple auctions
public List<ResultSetFuture> getBids(String... names) {
List<ResultSetFuture> listOfFutures = Arrays.stream(names)
.map(name -> session.executeAsync("select * from auction_bids where name = ?", name))
.collect(Collectors.toList());
return listOfFutures;
}
for (ResultSetFuture r: bidsForMultipleAuctions) {
r.getUninterruptibly().forEach(System.out::println);
}
// a list of futures in order of completion
List<ListenableFuture<ResultSet>> inOrder = Futures.inCompletionOrder(bidsForMultipleAuctions);
20. @chbatey
Java 8 to the rescue
public CompletableFuture<ResultSet> getBidsCF(String name) {
return convert(session.executeAsync("select * from auction_bids where name = ?", name));
}
CompletableFuture<ResultSet> tShirt = auctionDao.getBidsCF("Cassandra T-Shirt");
CompletableFuture<ResultSet> trousers = auctionDao.getBidsCF("Trousers");
CompletableFuture<List<Row>> tShirtRows = tShirt.thenApply(rs -> rs.all());
CompletableFuture<List<Row>> trouserRows = trousers.thenApply(rs -> rs.all());
CompletableFuture<List<Row>> allTheRows = tShirtRows.thenCombine(trouserRows, (r1, r2) ->
{
r1.addAll(r2); return r1;
});
allTheRows.thenAccept(all -> all.forEach(System.out::println));
22. @chbatey
Development Env + Testing
• Cassandra Unit
- Embeds a single Cassandra node in the same JVM for unit
testing
• https://github.com/jsevellec/cassandra-unit
• Cassandra Cluster Manager
- Spins up small clusters locally
• https://github.com/pcmanus/ccm
• Stubbed Cassandra
- Pretends to be a real Cassandra
23. @chbatey
Cassandra cluster manager
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
-- Address Load Tokens Owns Host ID Rack
UN 127.0.0.1 102.27 KB 256 ? 15ad7694-3e76-4b74-aea0-fa3c0fa59532 rack1
UN 127.0.0.2 102.18 KB 256 ? cca7d0bb-e884-49f9-b098-e38fbe895cbc rack1
UN 127.0.0.3 93.16 KB 256 ? 1f9737d3-c1b8-4df1-be4c-d3b1cced8e30 rack1
UN 127.0.0.4 102.1 KB 256 ? fe27b958-5d3a-4f78-9880-76cb7c9bead1 rack1
UN 127.0.0.5 93.18 KB 256 ? 66eb3f23-8889-44d6-a9e7-ecdd57ed61d0 rack1
UN 127.0.0.6 102.12 KB 256 ? e2e99a7b-c1fb-4f2a-9e4f-7a4666f8245e rack1
ccm create test -v 2.0.5 -n 6 -s