In my pursuit to learn different kind of databases I have chosen to learn Neo4j as my first nosql database.

Neo4j is a graph database that allows you to store information in nodes and relationships. Both these entities can have properties attached to them. Together with the sophisticated query language, Cypher, you can do advance queries and analysis of the database.

This database is a good choice when having data that is connected in for example social graphs. It is also good to save spatial data because of the query language that can be used to map routes.

This is a step-by-step instruction to create a Neo4j database in Azure and then use Azure Mobile Services to access it. These instructions presumes some knowledge of Azure already.

1. In the Azure portal, download and register a new Ubuntu Neo4j Image from VM Depot

clip_image002

2. Create a new Virtual Machine from that image

Step 1

clip_image004

Step 2

clip_image006

Step 3

clip_image008

3. Test if it works by opening a browser and go to http://<your name>.cloudapp.net:7474.

If you get a login window, read the text carefully and you see what the credentials are to login.

clip_image010

4. Open an SSH connection to your machine. In Mac, use Terminal, in Windows, use Putty and login.

5. Go to /var/lib/neo4j/plugins

6. Download the following file http://dist.neo4j.org.s3.amazonaws.com/authentication-extension/authentication-extension-1.8.1-1.0-SNAPSHOT.jar with this command:

sudo wget http://dist.neo4j.org.s3.amazonaws.com/authentication-extension/authentication-extension-1.8.1-1.0-SNAPSHOT.jar

7. Go to /var/lib/neo4j/conf

8. Edit the file neo4j-server.properties and add: (change to your own password and username)

org.neo4j.server.credentials=username:password

org.neo4j.server.thirdparty_jaxrs_classes=org.neo4j.server.extension.auth=/auth

I use sudo pico neo4j-server.properties

8. Go to /var/lib/neo4j and run sudo ./bin/neo4j restart to restart the neo4j server.

9. Test by going to the site again.

Now you should login with the credentials you specified above.

Azure Mobile Service

1. Create a new Azure Mobile Service

2. Create a table called “person

3. Set the Permissions for insert to Everyone

(This is to make it more simple. This should never be used in Production.)

4. Open that table and go to the script menu and do something to the insert script.

Add for example a comment. This is to trigger a creation of the javascript that we will use later.

5. Go to the dashboard page and choose “Set up source control”.

6. Copy the git url. (If you don’t find it, look under the menu Configure.)

7. Open a command window on your development machine and clone the repository:

git clone https://<…>.scm.azure-mobile.net/<…>.git

8. Still in the command window, go to the directory ./services/table

9. Import the Node module Neo4j:

npm install neo4j

I presume that node.js is already installed on the machine. If not, go to http://nodejs.org/ and install it first.

10. Edit the file person.insert.js

function insert(item, user, request)
{
  var neo4j = require('neo4j');
  var db = new neo4j.GraphDatabase('http://<username>:<password>@<your neo url>.cloudapp.net:7474');
  var node = db.createNode({name: item.name});
  node.save(function (err, node) {
    if (err)
    {
      console.err('Error saving new node to database:', err);
    }
    else
    {
      console.log('Node saved to database with id:', node.id);
    }
  });

  request.execute();
}

11. In the command window, do:

Git add .
Git commit –m “neo4j”
Git push

12. Done

Test

I use Postman in Google Chrome

clip_image012

Hit send and if it works you can now go to neo4j admin site: http://<your name>.cloudapp.net:7474 and you will find that you node has been saved into the database.

clip_image014