Neo4j + GrapheneDB: it has never been so simple!

Well, aren’t we in the Cloud era? Can we ignore IaaS, Saas and Paas?

The answer is no, of course! So I started looking how we could deploy Neo4j in the cloud.

Neo4j can be run in various cloud scenarios and on a number of cloud stacks

On previous page link, the first proposed cloud option is Neo4j hosting with GrapheneDB

GrapheneDBNeo4j Graph Database as a Service. GrapheneDB offers hosted, reliable and optimized graph databases to power your app.

1. Neo4Art joins GrapheneDB: Van Gogh’s journey is moving to the cloud

I was absolutely impressed how easily and fast I was able to deploy my Neo4j graph DB in the cloud. And you have to consider it was my very first time I was using GrapheneDB.

It took me 3 steps (after those, I could start enjoying with my fresh new new4j instance in the cloud):

  • Signing up to GrapheneDB;
  • Creating a FREE database instance;
  • Loading data;

and 15 snapshots to tell you how I did it (actually the longest part of this simple, little post):

1.1 Signing up to GrapheneDB

Same as usual…

1.2 Creating a FREE Neo4j DB instance

Sandbox instances are sold for free, with 512 MB Storage and 256 MB RAM:

Click on “Create database“:

Provide instance name (neo4art in my case, of course) and click “Create“:

… and after a few seconds my instance was created!

Then I wanted to check if this new instance was up and running! So I tried to access to its REST URL by providing required credentials:

Ok, up and running 🙂

Then I tried to access to Neo4j browser too:

… but I quickly realized my instance was empty. I needed my data!

1.3 Loading data

To load my data, I have used their Restore database function, which can be found in the Admin tab.

This function requires a zip file containing the graph.db directory of an already created database:

# cd /your/path/to/neo4j-community-edition-2.0.1/data/graph.db
# zip -r ../neo4art.zip

Click on “Restore database” located in the DANGER ZONE:

Click on the highlighted icon to upload zip file containing data:

Choose zip file from file system:

Click on “Restore” button:

Wait for a few seconds…

… and data will be loaded:

1.4 Have fun

Finally, I could test if my neo4j cloud instance was up, running and my data loaded too:

MATCH (artist:ARTIST {last_name: 'Van Gogh'})-[:MOVED_TO*]->(stage:STAGE)-[:IN]->(city:CITY)
OPTIONAL MATCH (stage)-[:WHERE_WAS_INFLUENCED_BY*]->(influencer:ARTIST)-[:BELONGS_TO*]->(art_movement:ART_MOVEMENT)
RETURN artist, stage, city, influencer, art_movement

2. Accessing Neo4Art on GrapheneDB via REST API

Well, it was time to do some REST test, and all I needed was already there: neo4j REST API

neo4j rest api

How can I find Van Gogh’s influencers via REST? Easy again: REST API Cypher is the response.

It was sufficient to create a simple JSON file (influencers.json):

{
 "query": "MATCH (artist:ARTIST {last_name: 'Van Gogh'})-[:MOVED_TO*]->(stage:STAGE)-[:IN]->(city:CITY)
           OPTIONAL MATCH (stage)-[:WHERE_WAS_INFLUENCED_BY*]->(influencer:ARTIST)-[:BELONGS_TO*]->(art_movement:ART_MOVEMENT)
           RETURN artist.first_name + ' ' + artist.last_name as artist, stage.from, stage.to, city.name, city.country,
                  influencer.first_name + ' ' + influencer.last_name, collect(art_movement.name) as art_movements
           ORDER BY stage.from",
 "params": {}
}

… and a tiny little shell (influencers-rest.sh):

#!/bin/sh
curl -X POST -d @influencers.json http://neo4art:**************@neo4art.sb01.stations.graphenedb.com:24789/db/data/cypher -H "Content-Type: application/json"

To get my response:

vincent:graphene lorenzo$ ./influencers-rest.sh 
{
 "columns" : [ "artist", "stage.from", "stage.to", "city.name", "city.country", "influencer.first_name + ' ' + influencer.last_name", "art_movements" ],
 "data" : [ [ "Vincent Van Gogh", 188312, 188511, "Neuen", "Netherland", null, [ ] ], [ "Vincent Van Gogh", 188512, 188602, "Antwerp", "Belgium", "Peter Rubens", [ "Baroque" ] ], [ "Vincent Van Gogh", 188603, 188802, "Paris", "France", "Henri Toulouse-Lautrec", [ "Art Nouveau", "Post-Impressionism" ] ], [ "Vincent Van Gogh", 188603, 188802, "Paris", "France", "Eugène Gauguin", [ "Post-Impressionism" ] ], [ "Vincent Van Gogh", 188603, 188802, "Paris", "France", "Émile Bernard", [ "Post-Impressionism" ] ], [ "Vincent Van Gogh", 188603, 188802, "Paris", "France", "Oscar-Claude Monet", [ "Impressionism" ] ], [ "Vincent Van Gogh", 188603, 188802, "Paris", "France", "Georges-Pierre Seurat", [ "Post-Impressionism", "Pointillism", "Neo-impressionism" ] ], [ "Vincent Van Gogh", 188603, 188802, "Paris", "France", "Camille Pissarro", [ "Impressionism", "Post-Impressionism" ] ], [ "Vincent Van Gogh", 188802, 188905, "Arles", "France", null, [ ] ], [ "Vincent Van Gogh", 188905, 189005, "Saint-Rémy-de-Provence", "France", null, [ ] ], [ "Vincent Van Gogh", 189005, 189007, "Auvers-sur-Oise", "France", null, [ ] ] ]
}

3. Accessing Neo4Art + GrapheneDB via JAVA API

Then, it was time to write some JAVA code. Once again, all I needed was already there: Neo4j Java API

neo4j java api

My GrapheneDB Neo4Art instance’s coordinates are:

It was sufficient to add some dependencies in my pom.xml:

<!-- Neo4j -->
<dependency>
  <groupId>org.neo4j</groupId>
  <artifactId>neo4j</artifactId>
  <version>2.0.1</version>
</dependency>
<dependency>
  <groupId>org.neo4j</groupId>
  <artifactId>neo4j-rest-graphdb</artifactId>
  <version>2.0.0-M06</version>
</dependency>

Implemented a connection class with a shutdown hook – as clearly explained by Neo4j Team:

package it.inserpio.neo4art;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.rest.graphdb.RestGraphDatabase;

public class ApplicationConfig
{
  public static GraphDatabaseService getConnection()
  {
    GraphDatabaseService graphDB =
           new RestGraphDatabase("http://neo4art.sb01.stations.graphenedb.com:24789/db/data/", "neo4art", "**********");

    registerShutdownHook(graphDB);

    return graphDB;
  }

  private static void registerShutdownHook(final GraphDatabaseService graphDb)
  {
    // Registers a shutdown hook for the Neo4j instance so that it
    // shuts down nicely when the VM exits (even if you "Ctrl-C" the
    // running application).

    Runtime.getRuntime().addShutdownHook(new Thread()
    {
      @Override
      public void run()
      {
        graphDb.shutdown();
      }
    });
  }
}

and test it:

package it.inserpio.neo4art;

import org.junit.Assert;
import org.junit.Test;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;

public class ApplicationConfigTest
{
  @Test
  public void shouldConnectTo()
  {
    GraphDatabaseService neo4art = ApplicationConfig.getConnection();

    Node vanGogh = neo4art.getNodeById(0);

    Assert.assertEquals("Van Gogh", vanGogh.getProperty("last_name"));
  }
}

Yes I know: it’s a very short example. I’ll go deeper in the next post…

Please, be patient 🙂

Advertisements

2 thoughts on “Neo4j + GrapheneDB: it has never been so simple!

  1. Do you have a graphenDB neo4j example for ASP or Visual Basic that uses MSXMLHttp ? I have been able to make it work with locally installed Neo4j 2.0 Community but not yet with my GraphenDB.

  2. Pingback: Neo4Art moves to Cloud Foundry at SpringOne 2GX 2014 | Lorenzo Speranzoni's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s