Ne04j Java Graph database

Ne04j Java Graph database

Neo4J is used in embedded mode to model and create any data relationship in the form of graphs.

Each graph is a collection of “nodes”, and these nodes can be connected through “edges”. The relationship between two nodes can be created/updated/read/deleted using the graph APIs.You can also maintain additional attributes inside each node and each edge as name-value pairs if needed.

This allows you to have a “generic in-memory data structure” which can be adapted with changing data structure needs.

The graph data structure also scales massively when you use it in embedded mode. When you configure graph caching, along with disk backup – you are able to scale from anywhere between 40,000 TPS to 100,000 TPS in a single instance.

Having disk backup has the advantage of data persistence when you re-start the java process.In all your Microservices, whenever you need to maintain complex data structures which go beyond simple name-value Maps – you can use the graph data structures.

One of the important use cases for graph dB can be to pre-store events/alarms and their correlations leading to some action like upscale, downscale, terminate an instance in cloud.This would be done at design time…. While at run time you will get sequence of events you  follow and get a match with one of the correlations and find the applicable action to be taken.For simpler cases, you can simply query the graph node or edge to get the values.

Sample code :
import org.neo4j.graphdb.*;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.graphdb.traversal.Evaluators;
import org.neo4j.graphdb.traversal.TraversalDescription;
import org.neo4j.graphdb.traversal.Traverser;
import org.neo4j.graphdb.traversal.Uniqueness;
import org.neo4j.kernel.impl.query.Neo4jTransactionalContext;


public class Main {
public static void main(String[] args)
GraphDatabaseFactory dbFactory = new GraphDatabaseFactory();
GraphDatabaseService dbService = dbFactory.newEmbeddedDatabaseBuilder(new

try(Transaction tx = dbService.beginTx()){

Node node1 = dbService.createNode(Label.label(“User”));
node1.setProperty(“Type”, “Gamer”);
Node node2 = dbService.createNode(Label.label(“User”));
// Adds label User to nodes
node2.setProperty(“Name”, “UserB”);
node2.setProperty(“Type”, “Gamer”);
//Creates IS_FRIEND_OF relationship from node userA to node userB
Relationship rel1 = node1.createRelationshipTo(node2, RelationshipType.withName(“IS_FRIEND_OF”));


try(Transaction tx = dbService.beginTx()){
Node node = dbService.findNode(Label.label(“User”),”Name”,”userC” );
System.out.println(“node name: ” + node.getProperty(“Name”));
System.out.println(“node name: ” + node.getProperty(“Type”));

try(Transaction tx = dbService.beginTx()){
Node nodeA = dbService.findNode(Label.label(“User”),”Name”,”userC” );
System.out.println(“nodeA: ” + nodeA.getProperty(“Name”));
TraversalDescription traversalMoviesFriendsLike =
Traverser traverser = traversalMoviesFriendsLike.traverse(nodeA);
Iterable<Node> moviesFriendsLike = traverser.nodes();
for(Node user : moviesFriendsLike)
System.out.println(“FoundUser: ” + user.getProperty(“Name”));
System.out.println(“node type:” + user.getProperty(“Type”));

Resolving technical problems:

Solve your technical problems instantly

We provide Remote Technical Support from Monday to Sunday, 7:00PM to 1:00 AM

Mail your problem details at along with your mobile numberand we will give you a call for further details. We usually attend your problems within 60 minutes and solve it in maximum 2 days.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.