After selecting the appropriate edition for your platform, embed Neo4j in your Java application by including the Neo4j library jars in your build. The following sections will show how to do this by either altering the build path directly or by using dependency management.
Get the Neo4j libraries from one of these sources:
Add the jar files to your project:
-classpath
For an overview of the main Neo4j artifacts, see Neo4j editions. The artifacts listed there are top-level artifacts that will transitively include the actual Neo4j implementation. You can either go with the top-level artifact or include the individual components directly. The examples included here use the top-level artifact approach.
Maven dependency.
<project> ... <dependencies> <dependency> <groupId>org.neo4j</groupId> <artifactId>neo4j</artifactId> <version>${neo4j-version}</version> </dependency> ... </dependencies> ... </project>
Where ${neo4j-version}
is the desired version and the artifactId
is found in Neo4j editions.
For development in Eclipse, it is recommended to install the M2Eclipse plugin and let Maven manage the project build classpath instead, see above. This also adds the possibility to build your project both via the command line with Maven and have a working Eclipse setup for development.
Make sure to resolve dependencies from Maven Central, for example using this configuration in your ivysettings.xml file:
<ivysettings> <settings defaultResolver="main"/> <resolvers> <chain name="main"> <filesystem name="local"> <artifact pattern="${ivy.settings.dir}/repository/[artifact]-[revision].[ext]" /> </filesystem> <ibiblio name="maven_central" root="http://repo1.maven.org/maven2/" m2compatible="true"/> </chain> </resolvers> </ivysettings>
With that in place you can add Neo4j to the mix by having something along these lines to your ivy.xml file:
.. <dependencies> .. <dependency org="org.neo4j" name="neo4j" rev="${neo4j-version}"/> .. </dependencies> ..
Where ${neo4j-version}
is the desired version and the name
is found in Neo4j editions.
The example below shows an example gradle build script for including the Neo4j libraries.
def neo4jVersion = "[set-version-here]" apply plugin: 'java' repositories { mavenCentral() } dependencies { compile "org.neo4j:neo4j:${neo4jVersion}" }
Where neo4jVersion
is the desired version and the name
("neo4j
" in the example) is found in Neo4j editions.
To create a new database or ópen an existing one you instantiate an EmbeddedGraphDatabase
.
graphDb = new EmbeddedGraphDatabase( DB_PATH ); registerShutdownHook( graphDb );
Note | |
---|---|
The |
To stop the database, call the shutdown()
method:
graphDb.shutdown();
To make sure Neo4j is shut down properly you can add a shutdown hook:
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 example before it's completed) Runtime.getRuntime().addShutdownHook( new Thread() { @Override public void run() { graphDb.shutdown(); } } ); }
If you want a read-only view of the database, use EmbeddedReadOnlyGraphDatabase
.
To start Neo4j with configuration settings, a Neo4j properties file can be loaded like this:
Map<String, String> config = EmbeddedGraphDatabase.loadConfigurations( pathToConfig + "neo4j.properties" ); GraphDatabaseService graphDb = new EmbeddedGraphDatabase( "target/database/location", config );
Or you could of course create you own Map<String, String>
programatically and use that instead.
For configuration settings, see Chapter 21, Configuration & Performance.
Copyright © 2012 Neo Technology