Sunday, 22 May 2011

Apache Cassandra & RESTful API

Over the past few months I have been heavily involved in an uber-top-secret nights & weekends project.  Is it selling a virtual Chia-Pet?  I wish.  That would be COOL.  Regardless of what it is, I jumped into the 21st century and got on board with the REST of the world.

REST:  Representational State Transfer:  http://en.wikipedia.org/wiki/Representational_State_Transfer

Without getting into too many details, what I love is how easily I was able to create a REST API that interfaces with Apache Cassandra.  I have an API that fully supports PUT / POST / GET / DELETE / SEARCH without having to write all that much code.  Admittedly, I did it with phpcassa which may not be the best, most efficient, or whatever .. but I have to say.  It works and I rarely touch the code.

Some simple concepts I implemented:

  • A resource is a 1:1 mapping to a Cassandra column family.  For example:
    • http://api.example.com/user/ is a reference to the "user" column family
  • The element that follows the resource is a 1:1 mapping to a key in a Cassandra column family
    • http://api.example.com/user/sdolgy is a reference to the key "sdolgy" in the column family "user"
What is great here is that as the design changes or new information is added to Cassandra, I am making no changes to the REST API code.  New column families, new keys ... it's all good.  Simple.  

The only issues I continue to have are the ones around security.  This is always an epic or regal battle / debate on the best way to approach and tackle this problem.  Some people are big into OAuth.  There are still security concerns over OAuth and even some references to that, despite the big boys (facebook / google / twitter / etc. ) are using it ... it doesn't mean it's more secure or the best way to secure a REST API.  For now, I have implemented my own variation and will have to revisit in the future before we launch the online CHIA PET....

Here is a good tutorial on creating a PHP REST API if you are keen to get started:


-sd

2 comments:

jrsfiend said...

sdolgy I admire your quest for knowledge - I'm also a fan of apache cassandra with the REST API - I wonder if you can guess who I am, I stumbled on this blog post while googling 'apache cassandra oauth' today. . .

Brian O'Neill said...

I share your desire for a REST interface to Cassandra, which lead me to start work on an extension to Cassandra, which would deliver it. Let me know what you think.

http://code.google.com/a/apache-extras.org/p/virgil/
http://brianoneill.blogspot.com/2011/10/virgil-gui-and-rest-layer-for-cassandra.html