Graph databases and Neo4J

Graph databases are the best way to represent and query connected data, irrespective of any size and value. Graph theory itself is not a new concept. Euler gave this theory in the 18th century, and it has been actively improved since then. However, it is only in the last few years that graph theory and graph thinking have been applied to information management. Graph databases have been proven to solve some of the more relevant data management challenges of today, including important problems in the areas of social networking, master data management, recommendations engines, and more.


Neo4J is a graph database having below properties.
1. ACID (Atomic, Consistent, Isolated and Durable) properties are supported in Neo4J
2. High Availability – High availability can be ensured by replicating Neo4J master database on several slave databases.
3. Scalability - A graph can scale in size and complexity as the application evolves, with minimal impact on performance. Scaling is supported through horizontal scaling.
4. Performance - A single server instance can handle a graph of billions of nodes and relationships. If performance degrades, the graph database can be distributed among multiple servers in a high availability configuration.

A graph database store data in graphs. A graph has nodes, relationships and properties. Graph stores data in nodes and relationships both of which can have properties.

Let us try to understand Neo4J through HelloWorld program. Below are steps to write and run the HelloWorld program.

Step1 – Download Neo4J from http://www.neo4j.org/download_thanks?edition=community&release=1.8.1&platform=windows
Once downloaded, extract it and install it using command Neo4j.bat install

Step2- Create a java project and add jar file present at <Neo4J_installation_directory>/lib and <Neo4J_installation_directory>/system/lib in the build path of java project.

Step3 - Write HelloWorld program like below under the newly created java project

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.graphdb.index.Index;

public class HelloWorld {
GraphDatabaseService graphDB;
Node firstNode;
Node secondNode;
Index<Node> indexMessage;
Relationship relationShip;

HelloWorld()
{
// replace this path by path of Neo4J installed on your machine
graphDB = new GraphDatabaseFactory().newEmbeddedDatabase
("D:\\neo4j-community-1.8.1\\data\\graph_new.db");
Transaction tx = graphDB.beginTx();
try
{
// index for storing nodes
indexMessage = graphDB.index().forNodes("message");
firstNode = graphDB.createNode();
firstNode.setProperty( "message", "Hello, " );
secondNode = graphDB.createNode();
secondNode.setProperty( "message", "World!" );
indexMessage.add(firstNode, "name", "Hello, ");
indexMessage.add(secondNode, "name", "World!");

// set relationship between first node and second node
relationShip = firstNode.createRelationshipTo( secondNode, RelTypes.KNOWS );
relationShip.setProperty( "message", "brave Neo4j " );
   tx.success();

}
finally
{
   tx.finish();
}
}

public static void main(String args[])
{
HelloWorld hello = new HelloWorld();

}

private static enum RelTypes implements RelationshipType
{
   KNOWS
}

}

Step4- Run this program through IDE like Eclipse. Make sure that Neo4J instance is not running at that time otherwise you will get error like “Exception in thread "main" java.lang.IllegalStateException: Unable to lock store [D:\neo4j-community-1.8.1\data\graph_new.db\neostore], this is usually a result of some other Neo4j kernel running using the same store.

Step5- Run Neo4J using command Neo4j.bat start. Please make sure that Neo4J is installed using command Neo4j.bat install



Step6-Once Neo4J run successfully, open the web browser using url ‘http://localhost:7474’. If you want to change the port number, you can do it in neo4j-server.properties present at conf folder of installed directory. DB path can also be changed through this file.

Step7- Run below cypher query in data browser tab.
START root=node(*) // Start with the reference node
RETURN root        // and return it.


Step8-Click on Node1 and then the Graph icon as circled below in blue color.


Add a new profile under Style --> Profiles --> New profile as show below.




You should be able to see the relationship of Node1 and Node2 as shown below







Comments

Popular posts from this blog

Brief guide to ecommerce

CQ - How to set replication automatically from Author instance to publisher instance

New age career opportunities in Information Technology after graduation