Embed neo4j-enterprise within your java application
The Neo4j Java Reference Documentation generally describes how to embed Neo4j Community Edition within your Java application. If you are licensed for Neo4j Enterprise, this article will guide you through setting up the project to use Neo4j Enterprise embedded within your application.
Create a new project
First you will need to download IntelliJ community edition. Once done, create a New Project>Java>Command Line App.
Specify its name embedded and the base package
Configure the maven repository
Create a folder .m2 in your home directory
Create the ${HOME}/.m2/settings.xml
file as following.
Refer to the KB Dependency location
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd">
<servers>
<server>
<id>neo4j-enterprise</id>
<username>neo4j-enterprise</username>
<password>modify password there</password>
</server>
</servers>
</settings>
Create a project pom.xml file
A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project. It contains default values for most projects.
Create a new text file at the root of your project using intelliJ and name it pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!-- maven specific -->
<modelVersion>4.0.0</modelVersion>
<artifactId>embedded</artifactId>
<!-- your own project settings-->
<groupId>com.example</groupId>
<version>1.0-snapshot</version>
<name>embedded</name>
<description>Embedded demo project</description>
<!-- Where to download the artifact-->
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>neo4j-enterprise</id>
<name>Neo4j Enterprise Artifacts</name>
<url>http://m2.neo4j.com/enterprise</url>
</repository>
</repositories>
<!-- Which artifact is required-->
<dependencies>
<dependency>
<groupId>com.neo4j</groupId>
<!-- com.neo4j for enterprise, doc needs to be fixed: https://neo4j.com/docs/java-reference/3.5/tutorials-java-embedded/#editions -->
<artifactId>neo4j-enterprise</artifactId>
<version>3.5.3</version>
</dependency>
</dependencies>
<!-- to specify which version of java to compile the source code, here: java 11-->
<build>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>11</source> <!-- source code version -->
<target>11</target> <!-- binary destination version -->
</configuration>
</plugin>
</plugins>
</build>
</project>
Create the java class
In intelliJ, right-click on src and choose New > Java Class and name it EmbeddedNeo4j. You can paste the following code or follow the java reference manual
/*
* Licensed to Neo4j under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Neo4j licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.File;
import java.io.IOException;
import org.neo4j.graphdb.Direction;
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.EnterpriseGraphDatabaseFactory;
import org.neo4j.io.fs.FileUtils;
public class EmbeddedNeo4j
{
private static final File databaseDirectory = new File( "/Users/jphoulchand/neo4j/test/embedded-neo4j-enterprise/" );
public String greeting;
// tag::vars[]
GraphDatabaseService graphDb;
Node firstNode;
Node secondNode;
Relationship relationship;
// end::vars[]
// tag::createReltype[]
private enum RelTypes implements RelationshipType
{
KNOWS
}
// end::createReltype[]
public static void main( final String[] args ) throws IOException
{
EmbeddedNeo4j hello = new EmbeddedNeo4j();
hello.createDb();
hello.removeData();
hello.shutDown();
}
void createDb() throws IOException
{
FileUtils.deleteRecursively( databaseDirectory );
// tag::startDb[]
graphDb = new EnterpriseGraphDatabaseFactory().newEmbeddedDatabase( databaseDirectory );
registerShutdownHook( graphDb );
// end::startDb[]
// tag::transaction[]
try ( Transaction tx = graphDb.beginTx() )
{
// Database operations go here
// end::transaction[]
// tag::addData[]
firstNode = graphDb.createNode();
firstNode.setProperty( "message", "Hello, " );
secondNode = graphDb.createNode();
secondNode.setProperty( "message", "World!" );
relationship = firstNode.createRelationshipTo( secondNode, RelTypes.KNOWS );
relationship.setProperty( "message", "brave Neo4j " );
// end::addData[]
// tag::readData[]
System.out.print( firstNode.getProperty( "message" ) );
System.out.print( relationship.getProperty( "message" ) );
System.out.print( secondNode.getProperty( "message" ) );
// end::readData[]
greeting = ( (String) firstNode.getProperty( "message" ) )
+ ( (String) relationship.getProperty( "message" ) )
+ ( (String) secondNode.getProperty( "message" ) );
// tag::transaction[]
tx.success();
}
// end::transaction[]
}
void removeData()
{
try ( Transaction tx = graphDb.beginTx() )
{
// tag::removingData[]
// let's remove the data
firstNode.getSingleRelationship( RelTypes.KNOWS, Direction.OUTGOING ).delete();
firstNode.delete();
secondNode.delete();
// end::removingData[]
tx.success();
}
}
void shutDown()
{
System.out.println();
System.out.println( "Shutting down database ..." );
// tag::shutdownServer[]
graphDb.shutdown();
// end::shutdownServer[]
}
// tag::shutdownHook[]
private static void registerShutdownHook( final GraphDatabaseService graphDb )
{
// Registers a shutdown hook for the Neo4j instance so that it
// shuts down nicely when the VM exits (even if you "Ctrl-C" the
// running application).
Runtime.getRuntime().addShutdownHook( new Thread()
{
@Override
public void run()
{
graphDb.shutdown();
}
} );
}
// end::shutdownHook[]
}
Is this page helpful?