Search code examples
javaneo4jspring-data-neo4jbatch-insert

How can I get the path of my Neo4j <config storeDirectory=""> in a Batch Inserter method?


I'm using Neo4j 2.2.8 and Spring Data in a web application. I'm using xml to configure my database, like:

<neo4j:config storeDirectory="S:\Neo4j\mybase" />

But I'm trying to use a Batch Inserter to add more than 1 million of nodes sourced from a .txt file. After reading the file and setting the List of objects, my code to batch is something like:

public void batchInserter(List<Objects> objects) {

    BatchInserter inserter = null;
    try {
        inserter = BatchInserters.inserter("S:\\Neo4j\\mybase");            

        Label movimentosLabel = DynamicLabel.label("Movimentos");
        inserter.createDeferredSchemaIndex(movimentosLabel).on("documento").create();

        for (Objects objs : objects{                
            Map<String, Object> properties = new HashMap<>();
            properties.put("documento", objs.getDocumento());
            long movimento = inserter.createNode(properties, movimentosLabel);                

            DynamicRelationshipType relacionamento = DynamicRelationshipType.withName("CONTA_MOVIMENTO");
            inserter.createRelationship(movimento, objs.getConta().getId(), relacionamento, null);
        }
    } finally {
        if (inserter != null) {
            inserter.shutdown();
        }
    }
}

Is it possible to get the path of my database configured in my xml in the "inserter"? Because with the above configuration Neo4j gives me an error about multiple connections. Can I set a property to solve this error of multiple connections? Has anyone had this problem and have any idea how to solve it? Ideas are welcome.

Thanks to everyone!


Solution

  • Your question has several pieces to it:

    Error About Multiple Connections

    If you're using spring-data with a local database tied to a particular directory or file, be aware that you can't have two neo4j processes opening the same DB at the same time. This means that if you've decided to use BatchInserter against the same file/directory, this cannot happen at all while the JVM that's using the spring-data DB is running. There won't be a way I know of to get around that problem. One option would be to not use the batch inserter against the file, but to use the REST API to do inserting.

    get the path of my database configured in my xml

    Sure, there's a way to do that, you'd have to consult the relevant documentation. I can't give you the code for that because it depends on which config file your'e talking about and how it's structured, but in essence there should be a way to inject the right thing into your code here, and read the property from the XML file out of that injected object.

    But that won't help you given your "Multiple connections" issue mentioned above.

    Broadly, I think your solution is either:

    1. Don't run your spring app and your batch inserter at the same time.
    2. Run your spring app, but do insertion via the REST API or other method, so there isn't a multiple connection issue to begin with.