Home > Distributed Systems, Open Source Projects > Using Cassandra from Scala

Using Cassandra from Scala

I have started a new open source project https://github.com/stealthly/scala-cassandra that is a Scala wrapper for CQL  specifically its a wrapper of the DataStax java-driver.

Test cases are a good entry point so lets start here https://github.com/stealthly/scala-cassandra/blob/master/src/test/scala/ScalaCassandraSpec.scala

class ScalaCassandraSpec extends Specification {

    CQL.init()
    CQL.startup("MetaStore")
    Meta.createTable()

    "Meta objects" should {
      "be able to store & retrieve their binary state" in {

      //we use a Thrift object here for portability of the data stored
      val tMeta = new TMeta()

      //setting up some randomness so we can confirm what we are writing is what we get back
      val metaUUID = UUID.randomUUID()
      val dataum = Random.alphanumeric.take(1000000).mkString

      tMeta.setId(metaUUID.toString)
      tMeta.setDatum(dataum)

      Meta.save(tMeta) //Saved to C*

      val someNewTMeta = Meta(metaUUID)
      someNewTMeta.getId() must_== metaUUID.toString

      someNewTMeta.getDatum() must_== dataum
      }
    }
}

The Meta class (contained in the https://github.com/stealthly/scala-cassandra/blob/master/src/main/scala/MetaDAO.scala file) is like a Widget… Whats a Widget? For us lets consider its like “sample code”.

When you want to add something different than a widget create a new thrift file in the `thrift/interface` directory

Then in that directory (lets say you created an IDL called Music.thrift) then run `thrift -gen java Music.thrift` which we did for you already and copy the jar outputted from a `mvn package` to the `lib` folder so you don’t have to worry about it. You can even just keep using the Meta implementaiotn and just shove JSON or XML or whatever you want into it; however, it makes more sense to partition the objects some so you can create wider rows with a collection key in your table along with your partition key. You can also flatten the things by storing a HashMap on the table and retrieving it.

http://stealth.ly

Advertisement
  1. November 16, 2013 at 2:06 pm

    Great start! I’m working on a Scala project that will require Cassandra as a backend, so might start using your wrapper. Any plans on supporting Akka.actors as well?

    • November 16, 2013 at 5:27 pm

      I use RoundRobinRouter dispatch with Akka when I implement already. I can think of a generic version so others can implement it too, yup.

      Maybe something sorta like

      class CassandraSave extends Actor {
      CQL.init()
      CQL.startup(“MetaStore”)
      Meta.createTable()

      def receive = {
      case msg ⇒
      {
      Meta.save(msg)
      }
      }
      }

  2. November 18, 2013 at 12:43 pm

    Currently Meta.save(msg) is a blocking call, but It should be non blocking version as well, i.e. returning a Future[T]. The question is: does Datastax drivers support async calls or not?

  3. November 18, 2013 at 1:11 pm

    Yes, queries can be executed against the DataStax Java Driver asynchronously with a Future which is how the retrieve works see https://github.com/stealthly/scala-cassandra/blob/master/src/main/scala/MetaDAO.scala#L45

  4. November 18, 2013 at 1:46 pm

    Ah! Good. Then it should be easy to add an async version as well. As soon as I have it ready, I’ll send you a pull request then.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: