配置嵌入式 Neo4j 到 运行 APOC 程序

Configuration of embedded Neo4j to run APOC procedures

我有使用 APOC 函数的密码查询。如果 运行 直接使用应用程序,它可以正常工作,但我也想测试这些查询。我尝试使用 following 方法但出现异常 Unknown function 'apoc.coll.toSet'

我的样本测试class:

public class ApocTest {
    private static Neo4j neo4j;
    private static Driver driver;

    @BeforeAll
    static void initializeNeo4j() {
        // Make sure that the plugins folder is listed in -cp
        Path pluginDirContainingApocJar = Paths.get("src/main/resources/neo4j-plugins/");

        if (!Files.exists(pluginDirContainingApocJar)) {
            throw new IllegalArgumentException("Invalid path to plugins directory");
        }

        neo4j = Neo4jBuilders
                .newInProcessBuilder()
                .withDisabledServer()
                .withFixture("CREATE (p1:Person)-[:knows]->(p2:Person)-[:knows]->(p3:Person)")
                .withConfig(GraphDatabaseSettings.plugin_dir, pluginDirContainingApocJar)
                .withConfig(GraphDatabaseSettings.procedure_unrestricted, List.of("apoc.*"))
                .build();
        driver = GraphDatabase.driver(neo4j.boltURI(), AuthTokens.none());
    }

    @AfterAll
    static void stopNeo4j() {
        driver.close();
        neo4j.close();
    }

    @Test
    public void testApoc(){
        String query = "MATCH path=()-[:knows*2]->()\n" +
                       "RETURN apoc.coll.toSet(nodes(path)) AS nodesSet";
        List<Object> nodesSet = driver.session()
                .beginTransaction()
                .run(query)
                .single()
                .get("nodesSet")
                .asList();
        assertEquals(3, nodesSet.size());
    }
}

知道如何解决这个问题吗?

This sample project on the github

版本:

更新:

所以我尝试更新:

Path pluginDirContainingApocJar = new File(
                ApocConfig.class.getProtectionDomain().getCodeSource().getLocation().toURI())
                .getParentFile().toPath();

这意味着我不需要使用 apoc jars 进行操作,对吧? 但我仍然遇到错误:

Caused by: org.neo4j.kernel.lifecycle.LifecycleException: Component 'org.neo4j.procedure.impl.GlobalProceduresRegistry@27dc627a' was successfully initialized, but failed to start. Please see the attached cause exception "Unable to set up injection for procedure `CypherProcedures`, the field `cypherProceduresHandler` has type `class apoc.custom.CypherProceduresHandler` which is not a known injectable component.".
    at org.neo4j.kernel.lifecycle.LifeSupport$LifecycleInstance.start(LifeSupport.java:463)
    at org.neo4j.kernel.lifecycle.LifeSupport.start(LifeSupport.java:110)
    at org.neo4j.graphdb.facade.DatabaseManagementServiceFactory.startDatabaseServer(DatabaseManagementServiceFactory.java:189)
    ... 58 more
Caused by: org.neo4j.kernel.api.exceptions.ComponentInjectionException: Unable to set up injection for procedure `CypherProcedures`, the field `cypherProceduresHandler` has type `class apoc.custom.CypherProceduresHandler` which is not a known injectable component.
    at org.neo4j.procedure.impl.FieldInjections.createInjector(FieldInjections.java:98)
    at org.neo4j.procedure.impl.FieldInjections.setters(FieldInjections.java:81)
    at org.neo4j.procedure.impl.ProcedureCompiler.compileProcedure(ProcedureCompiler.java:264)
    at org.neo4j.procedure.impl.ProcedureCompiler.compileProcedure(ProcedureCompiler.java:226)
    at org.neo4j.procedure.impl.ProcedureJarLoader.loadProcedures(ProcedureJarLoader.java:114)
    at org.neo4j.procedure.impl.ProcedureJarLoader.loadProceduresFromDir(ProcedureJarLoader.java:85)
    at org.neo4j.procedure.impl.GlobalProceduresRegistry.start(GlobalProceduresRegistry.java:342)
    at org.neo4j.kernel.lifecycle.LifeSupport$LifecycleInstance.start(LifeSupport.java:442)
    ... 60 more

更新 2 - 正在处理 4.0:

出于某种原因降级到 Neo4j 4.0,与 recommended 中相同的版本足以使其正常工作。现在我不会花更多的时间在 Neo4j 4.1/4.2 上尝试 运行 它。

My code

可能是创建插件目录路径的方式。这里有一个来自 Michael Simons 的示例,它解释了如何使用 neo4j 类加载器:https://github.com/michael-simons/neo4j-examples-and-tips/blob/master/examples/testing-ogm-against-embedded-with-apoc/src/test/java/org/neo4j/tips/testing/testing_ogm_against_embedded_with_apoc/ApplicationTests.java#L53