Neo4j in the Cloud

By | March 21, 2013

neo4j14Neo4j is a robust (fully ACID) transactional property graph database. A graph database stores data in a graph, the most generic of data structures, capable of elegantly representing any kind of data in a highly accessible way. Due to its graph data model, Neo4j is highly agile and blazing fast. For connected data operations, Neo4j runs a thousand times faster than relational databases.

The main benefits of using Neo4j are:

  • intuitive, using a graph model for data representation
  • reliable, with full ACID transactions
  • durable and fast, using a custom disk-based, native storage engine
  • massively scalable, up to several billion nodes/relationships/properties
  • highly-available, when distributed across multiple machines
  • expressive, with a powerful, human readable graph query language
  • fast, with a powerful traversal framework for high-speed graph queries
  • embeddable, with a few small jars
  • simple, accessible by a convenient REST interface or an object-oriented Java API

And today we’ll show you how to get all these benefits in the cloud very fast and easy. Let’s get started!

Create the environment

1. Go to and sign up if you haven’t done it yet or log in with your Jelastic credentials by clicking the Sign In link on the page.

2. Ask Jelastic to create a new environment.


3. In the Environment topology dialog which opens, pick Tomcat 7 as your application server, set your cloudlet limits and type your environment name, for example, neo4jtest. Then click Create.


Wait just a minute for your environment to be created.


Deploy application

1. It’s very easy to use Neo4j embedded in Java applications. Create the application with Neo4j as you usually do and build the war file.

In our case we use simple HelloWorld application and a graph that looks like this:


Here’s the code:

package com.example.neo4jjelastic;

import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.kernel.impl.util.FileUtils;

public class EmbeddedNeo4j
    private static final String DB_PATH = System.getProperty("user.home") + "/neo4j_db";
    String greeting;
    // START SNIPPET: vars
    GraphDatabaseService graphDb;
    Node firstNode;
    Node secondNode;
    Relationship relationship;
    // END SNIPPET: vars

    // START SNIPPET: createReltype
    private static enum RelTypes implements RelationshipType
    // END SNIPPET: createReltype

    void createDb()
        // START SNIPPET: startDb
        graphDb = new GraphDatabaseFactory().newEmbeddedDatabase( DB_PATH );
        registerShutdownHook( graphDb );
        // END SNIPPET: startDb

        // START SNIPPET: transaction
        Transaction tx = graphDb.beginTx();
            // Updating operations go here
            // END SNIPPET: transaction
            // START SNIPPET: addData
            firstNode = graphDb.createNode();
            firstNode.setProperty( "message", "Hello, " );
            secondNode = graphDb.createNode();
            secondNode.setProperty( "message", "World!" );

            relationship = firstNode.createRelationshipTo( secondNode, RelTypes.KNOWS );
            relationship.setProperty( "message", "brave Neo4j " );
            // END SNIPPET: addData

            // START SNIPPET: readData
            System.out.print( firstNode.getProperty( "message" ) );
            System.out.print( relationship.getProperty( "message" ) );
            System.out.print( secondNode.getProperty( "message" ) );
            // END SNIPPET: readData

            greeting = ( (String) firstNode.getProperty( "message" ) )
                       + ( (String) relationship.getProperty( "message" ) )
                       + ( (String) secondNode.getProperty( "message" ) );

            // START SNIPPET: transaction
        // END SNIPPET: transaction

    private void clearDb()
            FileUtils.deleteRecursively( new File( DB_PATH ) );
        catch ( IOException e )
            throw new RuntimeException( e );

    void removeData()
        Transaction tx = graphDb.beginTx();
            // START SNIPPET: removingData
            // let's remove the data
            firstNode.getSingleRelationship( RelTypes.KNOWS, Direction.OUTGOING ).delete();
            // END SNIPPET: removingData


    void shutDown()
        System.out.println( "Shutting down database ..." );
        // START SNIPPET: shutdownServer
        // END SNIPPET: shutdownServer

    // START SNIPPET: shutdownHook
    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()
            public void run()
        } );
    // END SNIPPET: shutdownHook

    public void runNeo4jInJelastic(){
        EmbeddedNeo4j hello = new EmbeddedNeo4j();


As you can see in this case our database will be stored in the home folder of Tomcat in neo4j_db directory.

2. Upload your war file to the Deployment manager.


3. Once the package is in Jelastic, deploy it to the environment you have created earlier.

neo4j44. Now you can open your application in a browser.



5. Check your application server’s log to check the results of program processing.



6. Click Config button for Tomcat and navigate to the home folder and you’ll see your Neo4j database files.


That’s all you have to do! Now you have highly agile and fast database right in the cloud. Enjoy with Neo4j and Jelastic!

Don’t forget to tell your friends about Neo4j on Jelastic:)

Subscribe to get the latest updates