如何配置Neo4j embedded来运行apoc程序?

时间:2017-05-14 15:09:06

标签: neo4j spring-data-neo4j neo4j-ogm neo4j-embedded

我使用最新的spring 1.5版本spring-data-neo4j 4.2,使用ogm驱动程序设置了Neo4j。配置使用没有URI的嵌入式驱动程序(如此无常的数据库存储)

这是spring @Configuration bean内容:

@Bean
public org.neo4j.ogm.config.Configuration neo4jConfiguration() {
    org.neo4j.ogm.config.Configuration configuration = new org.neo4j.ogm.config.Configuration();
    configuration.driverConfiguration().setDriverClassName("org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver");
    // don't set the URI for embedded so we get an impermanent database
    return configuration;
}

@Bean
public SessionFactory getSessionFactory() {
    return new SessionFactory(
            neo4jConfiguration(),
            "xxx.yyy.springboot.neo4j.domain");
}

@Bean
public Neo4jTransactionManager transactionManager() {
    return new Neo4jTransactionManager(getSessionFactory());
}

尝试运行内置程序可以正常工作:

/**
 * Test we can call out to standard built-in procedures using cypher
 */
@Test
public void testNeo4jProcedureCalls() {

    Session session = sessionFactory.openSession();
    Result result = session.query("CALL dbms.procedures()", ImmutableMap.of());

    assertThat(result).isNotNull();
    List<Map<String, Object>> dataList = StreamSupport.stream(result.spliterator(), false)
            .collect(Collectors.toList());
    assertThat(dataList).isNotNull();
    assertThat(dataList.size()).isGreaterThan(0);
}

现在我想安装并运行我已添加到类路径中的apoc过程:

/**
 * Test we can call out to https://neo4j-contrib.github.io/neo4j-apoc-procedures
 */
@Test
public void testNeo4jApocProcedureCalls() {

    Session session = sessionFactory.openSession();
    Result result = session.query("CALL apoc.help(\"apoc\")", ImmutableMap.of());

    assertThat(result).isNotNull();
    List<Map<String, Object>> dataList = StreamSupport.stream(result.spliterator(), false)
            .collect(Collectors.toList());
    assertThat(dataList).isNotNull();
    assertThat(dataList.size()).isGreaterThan(0);
}

但是,上述操作失败,错误为Description: There is no procedure with the name 'apoc.help' registered for this database instance

我找不到任何用于注册apoc过程以在嵌入模式下运行的文档。在OGM文档中找不到任何对注册程序的引用。任何提示或片段都将不胜感激。

3 个答案:

答案 0 :(得分:5)

感谢指针迈克尔。你的例子适合直接访问,这个答案给了我通过neo4j-ogm层访问所需的详细信息:

Deploy a Procedure to Neo4J when using the embedded driver

所以这就是我最后通过spring-data-neo4j

注册程序的内容

注意:isEmbedded()检查neo4j驱动程序属性值是否包含'embedded',而Components.driver()调用是ogm层提供的静态方法。

public void registerProcedures(List<Class<?>> toRegister) {
    if(isEmbedded()) {
        EmbeddedDriver embeddedDriver = (EmbeddedDriver) Components.driver();
        GraphDatabaseService databaseService = embeddedDriver.getGraphDatabaseService();
        Procedures procedures = ((GraphDatabaseAPI) databaseService).getDependencyResolver().resolveDependency(Procedures.class);
        toRegister.forEach((proc) -> {
            try {
                procedures.registerProcedure(proc);
            } catch (KernelException e) {
                throw new RuntimeException("Error registering " + proc, e);
            }
        });
    }
}

并添加调用以在使用embedded:

运行时注册测试中的过程
@Test
public void testNeo4jApocProcedureCalls() {

    registerProcedures(asList(
            Help.class,
            Json.class,
            LoadJson.class,
            Xml.class,
            PathExplorer.class,
            Meta.class)
    );
    Session session = sessionFactory.openSession();
    Result result = session.query("CALL apoc.help('apoc')", ImmutableMap.of());

答案 1 :(得分:3)

您必须使用GraphDatabaseService手动注册它们。

请点击此处查看示例:https://github.com/neo4j-contrib/rabbithole/blob/3.0/src/main/java/org/neo4j/community/console/Neo4jService.java#L55

答案 2 :(得分:0)

随着neo4j 4.0的发布,某些事情发生了变化(值得注意的是ProceduresGlobalProcedures),这就是为什么我要分享我的解决方案。

出于测试目的,我想与neo4j一起设置嵌入式neo4j,结果如下:

  1. 由于某些原因,当从maven存储库中包含apoc时,缺少类(例如,apoc.util包仅包含一个类而不是〜20,也缺少了apoc.coll.Coll函数)。

为了解决这个问题,我必须使用以下答案:Compile Jar from Url in Gradle

然后在我的依赖项块中包含了

    testImplementation(urlFile("https://github.com/neo4j-contrib/neo4j-apoc-procedures/releases/download/4.1.0.0/apoc-4.1.0.0-all.jar", "neo4j-apoc"))
  1. 一旦所有类都注册了所需的内容,就我而言,我仅注册Coll函数:

EmbeddedNeo4jDriver.kt

val managementService = org.neo4j.dbms.api.DatabaseManagementServiceBuilder(TestConfiguration.Neo4j.directory)
       .setConfig(BoltConnector.enabled, true)
       .setConfig(BoltConnector.listen_address, SocketAddress(TestConfiguration.Neo4j.hostname, TestConfiguration.Neo4j.port))
       .build()
   

managementService.listDatabases().first()
       .let(managementService::database)
       .let { it as org.neo4j.kernel.internal.GraphDatabaseAPI }
       .dependencyResolver
       .resolveDependency(org.neo4j.kernel.api.procedure.GlobalProcedures::class.java)
       .registerFunction(apoc.coll.Coll::class.java)