Using Cassandra with Scala and Akka

With all this talk about NoSQL and new programming languages, I though I’d try getting Cassandra to work with Scala. Always being interested in productivity, I wanted to know how easy and concise an integration would be. One option was to use the Java client for Cassandra, as using Java libraries in Scala is rather easy. Obviously more concise would be a library written for Scala, so I tried Akka.

What is Cassandra? Taken from the homepage

Cassandra is a highly scalable, eventually consistent, distributed, structured key-value store. Cassandra brings together the distributed systems technologies from Dynamo and the data model from Google’s BigTable. Like Dynamo, Cassandra is eventually consistent. Like BigTable, Cassandra provides a ColumnFamily-based data model richer than typical key/value systems.

Cassandra has a steep learning curve as it’s more than a key/value store. The article WTF is a SuperColumn helped me tremendously.

After installing Cassandra you need to configure your storage. For this short example only one entry is necessary.

What is Akka?

What is Akka? Akka is a framework stack for highly scalable, concurrent applications in Scala and Java. Akka supports different concurrency paradigms like actors and software transactional memory (STM) and easy and automatic storage to backends, including Cassandra and MongoDB.

As I’m using Maven – still I know – I wanted to get Akka running with Maven. Getting Akka to work with Maven was easy. I compiled Akka to get a 0.6 JAR which I put in an embedded repository in Maven. For Cassandra I’ve used the 0.4 version. I took a clue from the Akka Maven description and created an embedded repository for my Akka JAR.

Then adding a dependency for Akka. The generated Akka JAR contains all dependencies which is nice for prototyping but would be needed to change in a production enviroment.

Now your application should build with an Akka dependency.

The Scala code

The example application I’ve written is managing a shopping list. I’ve used this example before to illustrate AJAX, REST, JSON and XML. The core is a domain class called ShoppingList. It’s modeled after the domain classes in Lift and uses a composable, domain driven design. The class is short and does only contain a name, an id an a list of items.

Now we want to store an instance of ShoppingList. In which format to write data to the Cassandra Storage? I decided to go with a more key/value storage approach at first and do not use Cassandra for structuring data. This leaves the question how to store the value. Beside binary formats there are two obvious contenders: JSON and XML. Both have pros and cons.

JSON has the benefit of storing – after checking – data from AJAX calls directly to the storage and retrieving data and delivering data without processing to AJAX calls. The downside of JSON is it does contain less semantic information and could lead to problems down the road. XML is a better format for long term storage and enterprise integration as it contains more semantic information and is easy accessible. The downside: Very few people use XML for AJAX and it’s much more verbose than JSON.

Let’s try both approaches with Scala. Converting an object to XML is easy with Scala, as Scala has builtin XML support.

With LiftJson (which is usable without Lift) it’s nearly as easy to generate JSON:

which would result in

Reading and writing to Cassandra with Akka

Now to the juicy bits, reading and writing to Cassandra with Akka.

where the CassandraStore is a static wrapper around the Akka Cassandra sessions. Obviously this would need to go into a injected dependency or a base class. Storing data into Cassandra is just as easy.

Up to now I’m still quite satisfied with the code, how Akka worked with Cassandra. Cassandra setup in a VirtualBox and Ubuntu was easy and is a sane approach for working on Windows. With this code it should be easy for you to get Cassandra going with Scala. Any hints, opinions and suggestions, as usual in the comments.

197 thoughts on “Using Cassandra with Scala and Akka”

  1. Pingback: electric vehicle
  2. Pingback: best
  3. Pingback: fucoxanthin
  4. Pingback: geniux pills
  5. Pingback: intelligex
  6. Pingback: intellux
  7. Pingback: probrain
  8. Pingback: radian c
  9. Pingback: supreme antler
  10. Pingback: genius reviews
  11. Pingback: testomax
  12. Pingback: TRACKR BRAVO
  13. Pingback: alpha f1
  14. Pingback: alpha fuel
  15. Pingback: caralluma extract
  16. Pingback: anabolic rx24
  17. Pingback: cognifocus
  18. Pingback: garcinia melt
  19. Pingback: max synapse
  20. Pingback: nerventrax
  21. Pingback: nutra forskolin
  22. Pingback: premium cleanse
  23. Pingback: testorip
  24. Pingback: testoroar
  25. Pingback: zynev
  26. Pingback: pro forskolin
  27. Pingback: forskolin slim
  28. Pingback: garcinia cleanse
  29. Pingback: forskolin fuel
  30. Pingback: vitapulse
  31. Pingback: Vita pulse
  32. Pingback: pure colon detox
  33. Pingback: somnapure
  34. Pingback: perfect biotics
  35. Pingback: reviews vitapulse
  36. Pingback: vitapulse vitamins
  37. Pingback: vitapulse scam
  38. Pingback: vitapulse
  39. Pingback: vitapulse
  40. Pingback: vita pulse
  41. Pingback: probiotic america
  42. Pingback: taurus traits
  43. Pingback: taurus
  44. Pingback: Diabetes Destroyer
  45. Pingback: Fat Loss Factor
  46. Pingback: Diabetes Destroyer
  47. Pingback: Ecomaxx Reviews
  48. Pingback: T90 Xplode
  49. Pingback: Maxtropin
  50. Pingback: Tea Tox Review
  51. Pingback: MegaDrox Reviews
  52. Pingback: Test X180
  53. Pingback: Test X180
  54. Pingback: Breast Actives
  55. Pingback: vita pulse
  56. Pingback: recapture 360
  57. Pingback: probiotics america
  58. Pingback: Perfect Biotics
  59. Pingback: Bonuses
  60. Pingback: have a peek here
  61. Pingback: this contact form
  62. Pingback: check over here
  63. Pingback: check here
  64. Pingback: Acne No More Ebook
  65. Pingback: my company
  66. Pingback: Visit Website
  67. Pingback: This Site
  68. Pingback: Clicking Here
  69. Pingback: Visit Website
  70. Pingback: directory
  71. Pingback: Visit Website
  72. Pingback: more info here
  73. Pingback: official site
  74. Pingback: look at this site
  75. Pingback: click here
  76. Pingback: their explanation
  77. Pingback: his explanation
  78. Pingback: directory
  79. Pingback: official site
  80. Pingback: her latest blog
  81. Pingback: find more
  82. Pingback: Wp Profit Builder
  83. Pingback: other
  84. Pingback: my site
  85. Pingback: original site
  86. Pingback: visit website
  87. Pingback: click here
  88. Pingback: tires
  89. Pingback: rims
  90. Pingback: wheels
  91. Pingback: read more
  92. Pingback: read more
  93. Pingback: read more
  94. Pingback: click here
  95. Pingback: click here

Comments are closed.