Sunday, February 19, 2012

Neo4J with Scala Play! 2.0 on Heroku (Part 1)

Neo4J with Scala Play! 2.0 on Heroku

In this new posts series I’ll try to gather all steps of a spike I did building a prototype using scala and a graph database.

Chosen Technologies

Play! Framework as the web framework, in its 2.0 version built from sources.
Neo4J as the back end service for storing graph data.
Scala for telling the computer what it should do...
Here is an overview of what will be covered in the current suite.
  1. How to install Play! 2.0 from Git (or download the last version that must be > beta)
  2. Install Neo4J and run it in a Server Mode. Explain its REST/Json Interface.
  3. Create a Play! project. Update it to open it in IDEA Community Edition.
  4. An introduction of the Json facilities of Play! Scala. With the help of the SJson paradigm.
  5. Introduction of the Dispatch Scala library for HTTP communication.
  6. How to use effeciently Dispatch’s Handler and Play!’s Json functionality together. Create generic Neo4J nodes using the result. 
  7. Enhance the previous work to create a persistent service that can re/store domain model instances.
  8. Create some views (don’t bother me for ‘em … I’m not a designer ^^) using Scala templates and Jquery ajax for browsing model and creating instances.
  9. Deploy the whole stuffs on Heroku.

Play 2.0 Framework

This post intent is not to explain how powerful is Play framework (2.0) is. For that I'd recommend this wiki page.
However we'll explain all needed steps to build it from sources.
When I first wronte this post, the released version wasn't suficient for what I needed to do with Neo4J. But now you could just download the RC2 and unzip it somewhere, find it here.

Prerequesites

In this section, we’ll assume that you’ve already setup your scala and git environment (oh yeah and the JDK as well, and not the JRE only! we’ll need javac). If not please refer to those sites:

  • http://www.scala-lang.org/node/201 
  • http://help.github.com/set-up-git-redirect

G[e/i]t sources

First of all, open your preferred Git tool and retrieve the sources (warn: choose a unix like path, otherwise you might encountered problems with spaces for instance).

Use git clone git://github.com/playframework/Play20.git. And wait for having all sources downloaded.

First step in sbt

Seconds later, open a console and do the following to run the built tool used by Play 2.0, that is sbt.
cd Play20/framework
build

This will launch the embedded sbt (0.11.2) which needs some libraries automatically fetched.

Build and fetch

While being in the sbt-console, you can now ask sbt to build the framework and fill in the local Ivy repository with needed libraries (Play2.0 runtime deps).

Enter build-repository in the console and hit enter.

Minutes later, you”ll be able to quit the console by CTRL+D, and to check what happens in you Play20 folder.

Actually, aside the framework folder, you have now a folder named repository that contains every needed deps (includind play).

Let’s check by listing all files in play: ls Play20/repository/local/play and find libraries such play, anorm, template for the scala 2.9.1 version.

Done!

Great!

You’ve just finished the Play2.0 installation.
You can, for convenience, update your PATH to point to the Play20 folder (where resides the play executable)

Let’s move to the Neo4J setup