Neo4j - Native Java API


Advertisements

In this chapter, we are going to develop and test a Java Application using Neo4j Native Java API. We will discuss about Neo4j Cypher Java API in next chapter.

Before developing application, please refer "Neo4j Java Environment Setup" chapter to setup Eclipse IDE to develop this application

Neo4j Native Java API Example

This example demonstrates how to develop a Java application in Eclipse IDE to develop and test Neo4j Native Java API Example

Please follow all steps mentioned in"Neo4j Java Environment Setup" chapter

Step 1 - Create a Java Program in the same Java Project

Neo4j CQL Tutorial

Now start writing Neo4j Java API coding to perform Neo4j DB operations

Step 2 - Create a Neo4j Database

GraphDatabaseFactory dbFactory = new GraphDatabaseFactory();
GraphDatabaseService db= dbFactory.newEmbeddedDatabase("C:/TPNeo4jDB");

It creates a Schema/Database for us at the specified path as shown below.This is similar to "CREATE DATABASE" command of Oracle SQL.

Neo4j CQL Tutorial

Step 3 - Start Neo4j Database transaction to commit our changes

try (Transaction tx = graphDb.beginTx()) {
	// Perform DB operations				
	tx.success();
}

So for our Java Program's source code looks like

package com.tp.neo4j.java.examples;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
public class Neo4jJavaAPIDBOperation {
  public static void main(String[] args) {
	GraphDatabaseFactory dbFactory = new GraphDatabaseFactory();
	GraphDatabaseService db = dbFactory.newEmbeddedDatabase("C:/TPNeo4jDB");
	try (Transaction tx = db.beginTx()) {
		// Perform DB operations	
		tx.success();
	}	
 }
}

Step 4 - To create Nodes, we need label names. Create an Enum by implementing Neo4j Java API "Label" interface.

package com.tp.ne4oj.java.examples;
import org.neo4j.graphdb.Label;
public enum Tutorials implements Label {
	JAVA,SCALA,SQL,NEO4J;
}

Step 5 - Create Nodes and set properties to them

Create two nodes

Node javaNode = db.createNode(Tutorials.JAVA);
Node scalaNode = db.createNode(Tutorials.SCALA);

Set properties to them

javaNode.setProperty("TutorialID", "JAVA001");
javaNode.setProperty("Title", "Learn Java");
javaNode.setProperty("NoOfChapters", "25");
javaNode.setProperty("Status", "Completed");	
	
scalaNode.setProperty("TutorialID", "SCALA001");
scalaNode.setProperty("Title", "Learn Scala");
scalaNode.setProperty("NoOfChapters", "20");
scalaNode.setProperty("Status", "Completed");

Step 6 - To create Relationships, we need Relationship Types. So create an enum by implementing Neo4j "RelationshipType".

package com.tp.neo4j.java.examples;
import org.neo4j.graphdb.RelationshipType;
public enum TutorialRelationships implements RelationshipType{
	JVM_LANGIAGES,NON_JVM_LANGIAGES;
}

Step 7 - Create Relationship between nodes and set properties to it.

Create a Relationship from Java Node to Scala Node

Relationship relationship = javaNode.createRelationshipTo(scalaNode,
	TutorialRelationships.JVM_LANGIAGES);

Set properties to this relationship

relationship.setProperty("Id","1234");
relationship.setProperty("OOPS","YES");
relationship.setProperty("FP","YES");

Step 8 - Final source code.

package com.tp.neo4j.java.examples;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;

public class Neo4jJavaAPIDBOperation {
public static void main(String[] args) {
	GraphDatabaseFactory dbFactory = new GraphDatabaseFactory();
	GraphDatabaseService db= dbFactory.newEmbeddedDatabase("C:/TPNeo4jDB");
	try (Transaction tx = db.beginTx()) {

		Node javaNode = db.createNode(Tutorials.JAVA);
		javaNode.setProperty("TutorialID", "JAVA001");
		javaNode.setProperty("Title", "Learn Java");
		javaNode.setProperty("NoOfChapters", "25");
		javaNode.setProperty("Status", "Completed");				
		
		Node scalaNode = db.createNode(Tutorials.SCALA);
		scalaNode.setProperty("TutorialID", "SCALA001");
		scalaNode.setProperty("Title", "Learn Scala");
		scalaNode.setProperty("NoOfChapters", "20");
		scalaNode.setProperty("Status", "Completed");
		
		Relationship relationship = javaNode.createRelationshipTo
		(scalaNode,TutorialRelationships.JVM_LANGIAGES);
		relationship.setProperty("Id","1234");
		relationship.setProperty("OOPS","YES");
		relationship.setProperty("FP","YES");
		
		tx.success();
	}
	   System.out.println("Done successfully");
}
}

Step 9 - Before executing this Java Program, check your Neo4j is in Shutdown mode or not. If not, please click on "Stop" button to showdown it.

Neo4j CQL Tutorial

Step 10 - Execute the Java Program and observe the output in Eclipse IDE Console.

Neo4j CQL Tutorial

Select our Neo4j Database folder and click on "Start" button

Neo4j CQL Tutorial

Once This DB is started successfully, access Neo4j Browser by clicking on "http://localhost:7474" link to observe our data.

Neo4j CQL Tutorial

Step 11 - Type below command at $ prompt of Neo4j Data Browser

MATCH (a)-[r:JVM_LANGIAGES]->(b)
RETURN r
Neo4j CQL Tutorial

Click on "Java" Node to view it's properties

Neo4j CQL Tutorial

Click on "Scala" Node to view it's properties

Neo4j CQL Tutorial

Click on Relationship to view it's properties

Neo4j CQL Tutorial

NOTE -

If our Neo4j Server is up and running by referring our newly created database, then we cannot execute our program as the server is already got lock for this database.

So when we execute our previous program, we will get some error stack trace

java.io.IOException: Couldn't lock lock file C:\TPNeo4jDB\lock because another process already holds the lock.

To avoid this issue, first stop our server, then execute the program.

Because by default Neo4j DB Server accepts only one lock at a time. In Realtime applications, Ne04J DBA people will update DB properties to allow some number of locks at a time.

Advertisements