5

I have setup Neo4j using the latest spring 1.5 release, spring-data-neo4j 4.2, with ogm drivers. The configuration is using embedded driver without URI (so impermanent database store)

Here is the spring @Configuration bean content:

@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());
}

Trying to run built in procedure works fine:

/**
 * 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);
}

Now I'd like to install and run apoc procedures, which I've added to the classpath:

/**
 * 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);
}

However, the above fails with error Description: There is no procedure with the name 'apoc.help' registered for this database instance

I couldn't find any documentation for registering apoc procedures to run in embedded mode. Couldn't find any reference to registering procedures in the OGM documentation. Any tips or snippets would be appreciated.

4

3 回答 3

5

感谢您的指针迈克尔。您的示例非常适合直接访问,这个答案为我提供了通过 neo4j-ogm 层访问所需的详细信息:

使用嵌入式驱动程序时将过程部署​​到 Neo4J

所以这就是我最终通过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);
            }
        });
    }
}

并添加调用以在使用嵌入式运行时在测试中注册过程:

@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());
于 2017-05-15T16:53:46.287 回答
3

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

见这里的例子:https ://github.com/neo4j-contrib/rabbithole/blob/3.0/src/main/java/org/neo4j/community/console/Neo4jService.java#L55

于 2017-05-14T16:21:53.497 回答
0

随着 neo4j 4.0 的发布,一些事情发生了变化(明显是Proceduresvs GlobalProcedures),这就是我想分享我的解决方案的原因。

为了测试目的,我想设置嵌入式 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函数:

嵌入式Neo4jDriver.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)
于 2020-07-06T10:46:37.993 回答