35.2. Batch Graph Database

In case you already have code for data import written against the normal Neo4j API, you could consider using a batch inserter exposing that API.


This will not perform as good as using the BatchInserter API directly.

Also be aware of the following:

  • Starting a transaction or invoking Transaction.finish()/close() or Transaction.success() will do nothing.
  • Invoking the Transaction.failure() method will generate a NotInTransaction exception.
  • Node.delete() and Node.traverse() are not supported.
  • Relationship.delete() is not supported.
  • Event handlers and indexes are not supported.
  • GraphDatabaseService.getRelationshipTypes(), getAllNodes() and getAllRelationships() are not supported.

With these precautions in mind, this is how to do it:

GraphDatabaseService batchDb =
        BatchInserters.batchDatabase( "target/batchdb-example", fileSystem );
Label personLabel = DynamicLabel.label( "Person" );
Node mattiasNode = batchDb.createNode( personLabel );
mattiasNode.setProperty( "name", "Mattias" );
Node chrisNode = batchDb.createNode();
chrisNode.setProperty( "name", "Chris" );
chrisNode.addLabel( personLabel );
RelationshipType knows = DynamicRelationshipType.withName( "KNOWS" );
mattiasNode.createRelationshipTo( chrisNode, knows );

The source code of the example is found here: BatchInsertDocTest.java