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;

import java.io.File;

public class Main {
public static void main(String[] args)
{
GraphDatabaseFactory dbFactory = new GraphDatabaseFactory();
GraphDatabaseService dbService = dbFactory.newEmbeddedDatabaseBuilder(new
File(“/home/tarry/Desktop/work/test_neo4j”)).newGraphDatabase();

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

Node node1 = dbService.createNode(Label.label(“User”));
node1.setProperty(“Name”,”userC”);
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”));
rel1.setProperty(“OnlinePay”,”YES”);
tx.success();

}

try(Transaction tx = dbService.beginTx()){
Node node = dbService.findNode(Label.label(“User”),”Name”,”userC” );
tx.success();
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 =
dbService.traversalDescription().
relationships(RelationshipType.withName(“IS_FRIEND_OF”),Direction
.OUTGOING).uniqueness(Uniqueness.NODE_GLOBAL).evaluator(Evaluators.atDepth(1));
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”));
}
}
}
}

Leave a Reply

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