35.1. Batch Inserter Examples

Initial import

To bulk load data using the batch inserter you’ll need to write a Java application which makes use of the low level BatchInserter interface.

[Tip]Tip

You can’t have multiple threads using the batch inserter concurrently without external synchronization.

You can get hold of an instance of BatchInserter by using BatchInserters. Here’s an example of the batch inserter in use:

BatchInserter inserter = null;
try
{
    inserter = BatchInserters.inserter( tempStoreDir );

    Label personLabel = Label.label( "Person" );
    inserter.createDeferredSchemaIndex( personLabel ).on( "name" ).create();

    Map<String, Object> properties = new HashMap<>();

    properties.put( "name", "Mattias" );
    long mattiasNode = inserter.createNode( properties, personLabel );

    properties.put( "name", "Chris" );
    long chrisNode = inserter.createNode( properties, personLabel );

    RelationshipType knows = RelationshipType.withName( "KNOWS" );
    inserter.createRelationship( mattiasNode, chrisNode, knows, null );
}
finally
{
    if ( inserter != null )
    {
        inserter.shutdown();
    }
}

When creating a relationship you can set properties on the relationship by passing in a map containing properties rather than null as the last parameter to createRelationship.

It’s important that the call to shutdown is inside a finally block to ensure that it gets called even if exceptions are thrown. If he batch inserter isn’t cleanly shutdown then the consistency of the store is not guaranteed.

[Tip]Tip

The source code for the examples on this page can be found here: BatchInsertDocTest.java

Setting configuration options

You can pass custom configuration options to the BatchInserter. (See the section called “Batch insert example” for information on the available options.) e.g.

Map<String, String> config = new HashMap<>();
config.put( "dbms.memory.pagecache.size", "512m" );
BatchInserter inserter = BatchInserters.inserter(
        new File( "target/batchinserter-example-config" ).getAbsoluteFile(), config );
// Insert data here ... and then shut down:
inserter.shutdown();

Alternatively you could store the configuration in a file:

batchinsert-config 

dbms.memory.pagecache.size=8m

You can then refer to that file when initializing BatchInserter:

try ( FileReader input = new FileReader( new File( "target/docs/batchinsert-config" ).getAbsoluteFile() ) )
{
    Map<String, String> config = MapUtil.load( input );
    BatchInserter inserter = BatchInserters.inserter(
            new File( "target/docs/batchinserter-example-config" ), config );
    // Insert data here ... and then shut down:
    inserter.shutdown();
}

Importing into an existing database

Although it’s a less common use case, the batch inserter can also be used to import data into an existing database. However, you will need to ensure that the existing database is shut down before you write to it.

[Warning]Warning

Since the batch importer bypasses transactions there is a possibility of data inconsistency if the import process crashes midway. We would strongly suggest you take a backup of your existing database before using the batch inserter against it.