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

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

问题描述

我已使用带有ogm驱动程序的最新spring 1.5发行版spring-data-neo4j 4.2设置了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)

这是春天的@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);
}

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

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.

推荐答案

感谢迈克尔.您的示例非常适合直接访问,此答案为我提供了通过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 Embedded运行APOC程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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