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
The following table outlines the available editions and their names for use with dependency management tools.
Tip | |
---|---|
Follow the links in the table for details on dependency configuration with Apache Maven, Apache Buildr, Apache Ivy, Groovy Grape, Grails, Scala SBT! |
Neo4j editions
Edition | Dependency | Description | License |
---|---|---|---|
Community | a high performance, fully ACID transactional graph database | GPLv3 | |
Enterprise | adding advanced monitoring, online backup and High Availability clustering | AGPLv3 |
Note | |
---|---|
The listed dependencies do not contain the implementation, but pulls it in transitively. |
For more information regarding licensing, see the Licensing Guide.
You can either go with the top-level artifact from the table above 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>2.0.1</version> </dependency> ... </dependencies> ... </project>
Where the artifactId
is found in the editions table.
For development in Eclipse, it is recommended to install the m2e 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="2.0.1"/> .. </dependencies> ..
Where the name
is found in the editions table above
The example below shows an example gradle build script for including the Neo4j libraries.
def neo4jVersion = "2.0.1" apply plugin: 'java' repositories { mavenCentral() } dependencies { compile "org.neo4j:neo4j:${neo4jVersion}" }
Where the coordinates (org.neo4j:neo4j
in the example) are found in the editions table above.
To create a new database or ópen an existing one you instantiate an EmbeddedGraphDatabase
.
graphDb = new GraphDatabaseFactory().newEmbeddedDatabase( 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 application). Runtime.getRuntime().addShutdownHook( new Thread() { @Override public void run() { graphDb.shutdown(); } } ); }
To start Neo4j with configuration settings, a Neo4j properties file can be loaded like this:
GraphDatabaseService graphDb = new GraphDatabaseFactory() .newEmbeddedDatabaseBuilder( storeDir ) .loadPropertiesFromFile( pathToConfig + "neo4j.properties" ) .newGraphDatabase();
Configuration settings can also be applied programmatically, like so:
GraphDatabaseService graphDb = new GraphDatabaseFactory() .newEmbeddedDatabaseBuilder( storeDir ) .setConfig( GraphDatabaseSettings.nodestore_mapped_memory_size, "10M" ) .setConfig( GraphDatabaseSettings.string_block_size, "60" ) .setConfig( GraphDatabaseSettings.array_block_size, "300" ) .newGraphDatabase();
For configuration settings, see Chapter 22, Configuration & Performance.
If you want a read-only view of the database, create an instance this way:
graphDb = new GraphDatabaseFactory().newEmbeddedDatabaseBuilder( "target/read-only-db/location" ) .setConfig( GraphDatabaseSettings.read_only, "true" ) .newGraphDatabase();
Obviously the database has to already exist in this case.
Note | |
---|---|
Concurrent access to the same database files by multiple (read-only or write) instances is not supported. |
Copyright © 2014 Neo Technology