如何配置Neo4j内嵌运行apoc程序? [英] How to configure Neo4j embedded to run apoc procedures?

查看:49
本文介绍了如何配置Neo4j内嵌运行apoc程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用最新的 spring 1.5 版本 spring-data-neo4j 4.2 和 ogm 驱动程序设置了 Neo4j.配置是使用没有URI的嵌入式驱动程序(所以临时数据库存储)

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)

这是spring@Configuration bean的内容:

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

现在我想安装并运行 apoc 程序,我已将其添加到类路径中:

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

但是,上面的失败并出现错误描述:没有为这个数据库实例注册名称为apoc.help"的过程

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

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

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.

推荐答案

感谢 Michael 的指点.您的示例适用于直接访问,此答案为我提供了通过 neo4j-ogm 层访问所需的详细信息:

Thanks for the pointer Michael. Your example is good for direct access, and this answer gave me the details needed to access through the neo4j-ogm layer:

使用嵌入式时将过程部署​​到 Neo4J司机

所以这就是我通过 spring-data-neo4j 注册过程的结果

so here's what I ended up with to register procedures through spring-data-neo4j

注意:isEmbedded()检查neo4j驱动属性值是否包含'embedded',Components.driver()调用是由ogm 层.

Note: isEmbedded() checks the neo4j driver property value contains 'embedded', and the Components.driver() call is static method provided by the ogm layer.

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

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

and add the call to register the procedures in the test when running with 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());

这篇关于如何配置Neo4j内嵌运行apoc程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆