How to create Neo4j Graph Database Nodes and Relationship using Java API.

How to create Neo4j Graph Database Nodes and Relationship using Java API:

1. Download Neo4j Community Edition and place in a Root Folder (Ex. C:\Program Files (X86)\Neo4j Community\)
2. Create a Maven Project using Eclipse (I used Luna IDE) and add the dependecy as Below.

<dependencies>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j</artifactId>
<version>2.1.7</version>
</dependency>
</dependencies>

Create Maven Project:

Create Maven Project

6

2

4

5

Maven Dependencies details you can find from http://neo4j.com/docs/stable/tutorials-java-embedded-setup.html

3. I have just created a simple Node to Node relationship and given the details in the HelloWorldMaven.java program in Comments. Please follow the Comments given inside the Program.


package com.neo4j;

import org.neo4j.graphdb.*;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;

/**
* Created by Varatharajan Giri Ramanathan on 3/22/2015.
*/

public class HelloWorldMaven {
//First the Neo4j DB path is specified
private static final String Neo4j_path=“/Users/gfp2ram/workspace/neo4j”;

//Creating Nodes, RelationShip, GraphDBService
Node first;
Node second;
Relationship relation;
GraphDatabaseService graphDataService;

//List of RelationShipts between the Nodes
private static enum RelTypes implements RelationshipType
{
KNOWS
}

public static void main(String[] args) {

//Creating an Instance to make calls for the functions we have written below.
HelloWorldMaven hello = new HelloWorldMaven();

//Function calls
hello.createDatabase();
hello.removeData();
hello.shutdown();

}
//Always create the Database
void createDatabase() {

//Step : 1 == > Create GraphDatabaseService
graphDataService = new GraphDatabaseFactory().newEmbeddedDatabase(Neo4j_path);

//Step : 2 == > Begin Transaction
Transaction transaction = graphDataService.beginTx();

try {
//Step : 3 == > Creation of Node and Set the Properties
//createNode(), setProperty are the method

first = graphDataService.createNode();
first.setProperty(“name”,”Jackson Hewitt”);

second = graphDataService.createNode();
second.setProperty(“name”,”H&R”);

//Step : 4 ==>; Create Relationship

relation = first.createRelationshipTo(first,RelTypes.KNOWS);
relation.setProperty(“relationship-type”,”knows”);

//Printing out the relationship between first and second nodes
//System.out.println(first.getProperty(“name”).toString());
//System.out.println(relation.getProperty(“relationship-type”).toString());
//System.out.println(second.getProperty(“name”).toString());
System.out.println(first.getProperty(“name”).toString()+”–>”+relation.getProperty(“relationship-type”).toString()+ ” — >” + second.getProperty(“name”).toString());

//Step : 5 ==> ; Success the transaction
transaction.success();
}
finally {
//Step 6: ==>; Finish Transaction
transaction.finish();
}
}

//Once the database is created, the data has to be removed
void removeData() {
//Step 1 : Again create the transaction
Transaction transaction = graphDataService.beginTx();

try {
//Delete the Outgoing RelationShip first
first.getSingleRelationship(RelTypes.KNOWS, Direction.OUTGOING).delete();
System.out.println(“Nodes are Removed Successfully”);
first.delete();
second.delete();
transaction.success();
} finally {
//Finish the Transaction
transaction.finish();
}
}

//The database instance has also be shutdown once created
void shutdown() {
//Shutdown the graphDataService
graphDataService.shutdown();
System.out.println(“Neo4j DB is shutdown successfully”);
}
}

Compile the Program and See the Output:

7

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s