[2.x-Scala] Play with Cassandra Database

684 views
Skip to first unread message

Ari King

unread,
Sep 10, 2013, 3:43:29 PM9/10/13
to play-fr...@googlegroups.com
I'm interested in using cassandra as the database for a play application. However, I'm not sure how to use any cassandra client (Hector, Astyanax, Datastax) with play. Does anyone know of example Play apps that demonstrate how to use hector, astyanax, or datastax with play? Thanks.

-Ari

Chanan Braunstein

unread,
Sep 10, 2013, 4:05:29 PM9/10/13
to play-fr...@googlegroups.com
Hi Ari,

We use the Datastax Java driver. It fairly straight forward to use. I don't have an example up on a public github but here is a sample Actor from one of our POC's that inserts data into a table:

package com.pearson.mathxl.gradebookdemo.backend.grades;

import akka.actor.UntypedActor;
import com.datastax.driver.core.BoundStatement;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.Session;
import com.typesafe.config.ConfigFactory;

public class InsertGradeActor extends UntypedActor {
 
private final String cassandraHost = ConfigFactory.load().getString("cassandra.host");
 
private final Cluster cluster = Cluster.builder().addContactPoint(cassandraHost).build();
 
private final Session session = cluster.connect("gradebook");
 
private final PreparedStatement insertStatment = session.prepare("UPDATE scores SET scores[?] = ? WHERE courseId = ? AND assignmentId = ?;");
 
private final BoundStatement bs = new BoundStatement(insertStatment);
 
 
@Override
 
public void onReceive(Object message) throws Exception {
 
Grade grade = (Grade)message;
 
 bs
.setString(0, grade.getStudentId());
 bs
.setInt(1, grade.getScore());
 bs
.setString(2, grade.getCourseId());
 bs
.setString(3, grade.getAssignmentId());
 session
.execute(bs);
 getSender
().tell("Done!", getSelf());
 
}
}


Will Sargent

unread,
Sep 11, 2013, 1:11:49 AM9/11/13
to play-fr...@googlegroups.com
There is an activator project for akka and Cassandra: http://typesafe.com/activator/template/activator-akka-cassandra

You can then use Play to talk to Akka, and just pass the messages through.

Will.


On Tue, Sep 10, 2013 at 12:43 PM, Ari King <ari.bran...@gmail.com> wrote:
I'm interested in using cassandra as the database for a play application. However, I'm not sure how to use any cassandra client (Hector, Astyanax, Datastax) with play. Does anyone know of example Play apps that demonstrate how to use hector, astyanax, or datastax with play? Thanks.

-Ari

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages