Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Use Cassandra with Scala

DZone's Guide to

How to Use Cassandra with Scala

· Big Data Zone
Free Resource

Effortlessly power IoT, predictive analytics, and machine learning applications with an elastic, resilient data infrastructure. Learn how with Mesosphere DC/OS.

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

Test cases are a good entry point, so let's 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… what's a widget? For us, let's think of it 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) 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 implementation 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.


Learn to design and build better data-rich applications with this free eBook from O’Reilly. Brought to you by Mesosphere DC/OS.

Topics:

Published at DZone with permission of Joe Stein, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}