0

Instead of creating my Neo4j DBs using the Neo4j Desktop, I create it in a Java app from scratch. I usually have two csv files: a nodes file and a relationships file. So, I create my DBs in two steps: first I create all nodes with a Cypher query and then I create all relationships between these nodes with another Cypher query (I use the 'execute' method from the 'GraphDatabaseService' class). My problem is that now I need to create dynamic types for these relationships, so I need to use the APOC library (concretely, 'CALL apoc.merge.relationship'). I know that this library must be installed using the Neo4j Desktop, and then you can create your DB by typing a Cypher query on it. But I need to create a DB from scratch in my Java code using the 'CALL apoc.merge.relationship'.

Thank you :)

Vicente
  • 35
  • 5

1 Answers1

0

Firstly you need to import the APOC jar in your project :

<dependency>
    <groupId>org.neo4j.procedure</groupId>
    <artifactId>apoc</artifactId>
    <version>3.4.0.1</version>
</dependency>

And then you need to register the APOC procedure manually you want to use :

            GraphDatabaseService db = ...
            Procedures procedures = ((GraphDatabaseAPI) db).getDependencyResolver().resolveDependency(Procedures.class);
            List<Class<?>> apocProcedures = asList(Coll.class, apoc.map.Maps.class, Json.class, Create.class, apoc.date.Date.class, FulltextIndex.class, apoc.lock.Lock.class, LoadJson.class,
                    Xml.class, PathExplorer.class, Meta.class, GraphRefactoring.class);
            apocProcedures.forEach((proc) -> {
                try {
                    procedures.register(proc);
                } catch (KernelException e) {
                    throw new RuntimeException("Error registering "+proc,e);
                }
});

Code from here https://github.com/neo4j-contrib/rabbithole/blob/87f6a386027bdd236c37b74afa986b188dc6c69a/src/main/java/org/neo4j/community/console/Neo4jService.java#L51-L65

logisima
  • 7,340
  • 1
  • 18
  • 31
  • Thanks for your answer :) I am using this library in Eclipse: 'neo4j-desktop-3.1.3.jar' and the APOC library you mentioned: 'apoc-3.4.0.1' (I don't use Maven) and I am getting this errors: Exception in thread "main" java.lang.RuntimeException: Error starting org.neo4j.kernel.impl.factory.GraphDatabaseFacadeFactory ... Caused by: java.lang.ClassNotFoundException: org.neo4j.scheduler.JobScheduler I create my DB with this code: GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(new File(RUTA_GRAFO)); Thanks! – Vicente Jul 10 '18 at 17:49