Prev |  | Next

## 36.1. Batch Inserter Examples

Creating a batch inserter is similar to how you normally create data in the database, but in this case the low-level `BatchInserter` interface is used. As we have already pointed out, you can’t have multiple threads using the batch inserter concurrently without external synchronization.

| Tip
The source code of the examples is found here: BatchInsertDocTest.java

To get hold of a `BatchInseter`, use `BatchInserters` and then go from there:

    BatchInserter inserter = BatchInserters.inserter( "target/batchinserter-example", fileSystem );
    Label personLabel = DynamicLabel.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 = DynamicRelationshipType.withName( "KNOWS" );
    // To set properties on the relationship, use a properties map
    // instead of null as the last parameter.
    inserter.createRelationship( mattiasNode, chrisNode, knows, null );
    inserter.shutdown();

To gain good performance you probably want to set some configuration settings for the batch inserter. Read Section 23.11.3, “Batch insert example” for information on configuring a batch inserter. This is how to start a batch inserter with configuration options:

    Map<String, String> config = new HashMap<>();
    config.put( "neostore.nodestore.db.mapped_memory", "90M" );
    BatchInserter inserter = BatchInserters.inserter(
            "target/batchinserter-example-config", fileSystem, config );
    // Insert data here ... and then shut down:
    inserter.shutdown();

In case you have stored the configuration in a file, you can load it like this:

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

Copyright © 2014 Neo Technology

Prev | Up | Next
| Home |