Blazingly Fast Neo4j Gragh Database in the Cloud

| March 21, 2013 | DevOps PaaS | , , , , , , , , , , ,
neo4j14

Neo4j 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 jelastic.com 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.

ftp1

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.

neo4j1

Wait just a minute for your environment to be created.

neo4j2

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:

neo4j13

Here's the code:

package com.example.neo4jjelastic;
import java.io.File;
import java.io.IOException;
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
{
KNOWS
}
// END SNIPPET: createReltype
void createDb()
{
clearDb();
// START SNIPPET: startDb
graphDb = new GraphDatabaseFactory().newEmbeddedDatabase( DB_PATH );
registerShutdownHook( graphDb );
// END SNIPPET: startDb
// START SNIPPET: transaction
Transaction tx = graphDb.beginTx();
try
{
// 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
tx.success();
}
finally
{
tx.finish();
}
// END SNIPPET: transaction
}
private void clearDb()
{
try
{
FileUtils.deleteRecursively( new File( DB_PATH ) );
}
catch ( IOException e )
{
throw new RuntimeException( e );
}
}
void removeData()
{
Transaction tx = graphDb.beginTx();
try
{
// START SNIPPET: removingData
// let's remove the data
firstNode.getSingleRelationship( RelTypes.KNOWS, Direction.OUTGOING ).delete();
firstNode.delete();
secondNode.delete();
// END SNIPPET: removingData
tx.success();
}
finally
{
tx.finish();
}
}
void shutDown()
{
System.out.println();
System.out.println( "Shutting down database ..." );
// START SNIPPET: shutdownServer
graphDb.shutdown();
// 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()
{
@Override
public void run()
{
graphDb.shutdown();
}
} );
}
// END SNIPPET: shutdownHook
public void runNeo4jInJelastic(){
EmbeddedNeo4j hello = new EmbeddedNeo4j();
hello.createDb();
hello.removeData();
hello.shutDown();
}
}

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.

neo4j3

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

neo4j4

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

neo4j5
configure database neo4j

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

neo4j7
neo4j gragh database configuration

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

neo4j db deploy in cloud

That's all you have to do! Now you have highly agile and fast database right in the cloud. Enjoy with Neo4j and Jelastic!
Not sure it’s that efficient? Try now to install your own high-speed graph database Neo4j in the cloud and you will no longer have any doubts.

In case you face any issues feel free to appeal for our technical expert’s assistance at Stackoverflow.