After selecting the appropriate edition for your platform, embed Neo4j in your Java application by including the Neo4j library jars in your build.
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 Table 1.2, “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 intended version and the artifactId
is found in Table 1.2, “Neo4j editions”.
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 intended version and the name
is found in Table 1.2, “Neo4j editions”.
To create a new database or ópen an existing one you instantiate an EmbeddedGraphDatabase
.
GraphDatabaseService 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 2, Configuration & Performance.
Copyright © 2011 Neo Technology