【问题标题】:using relative paths for embedded neo4j in spring-boot application在spring-boot应用程序中使用嵌入式neo4j的相对路径
【发布时间】:2019-10-11 17:29:19
【问题描述】:

我正在尝试设置示例 spring-boot-neo4j 应用程序,但使用嵌入式数据库。我按照herehere 的建议添加了所需的依赖项和配置。虽然这适用于绝对路径

spring.data.neo4j.uri=file://var/tmp/graph.db

我似乎找不到指定相对路径的方法。我试过file://graph.dbfile://./graph.dbfile://~/graph.db 但得到了这个

Caused by: java.lang.IllegalArgumentException: URI has an authority component
    at java.io.File.<init>(File.java:423) ~[na:1.8.0_112]
    at org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver.createPermanentFileStore(EmbeddedDriver.java:211) ~[neo4j-ogm-embedded-driver-2.0.5.jar:na]
    at org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver.configure(EmbeddedDriver.java:102) ~[neo4j-ogm-embedded-driver-2.0.5.jar:na]

尝试删除file:// 并使用graph.db./graph.db 但得到URI is not absolute

尝试了file:./graph.db,但得到了URI is not hierarchical

我想使用相对于项目位置(或主文件夹)的路径,有人可以指出如何指定此类路径。我正在使用 neo4j-ogm 2.0.5、gradle 2.14、spring-boot 1.4.2 和 jdk 1.8。

谢谢

【问题讨论】:

    标签: java spring-boot neo4j uri spring-data-neo4j


    【解决方案1】:

    看看强大的跨平台解决方案:

      @Value("${spring.data.neo4j.uri:default}")
      private String neoUri;
    
      @Bean
      public Configuration neoConfig() throws URISyntaxException, IOException {
        String path = neoUri;
        Configuration.Builder builder = new Configuration.Builder();
        if (!"default".equals(path)) {
          LOG.info("Configured database uri: " + path);
          path = path.replace('\\', '/');
          URI uri = new URI(path);
          if ("file".equals(uri.getScheme())) {
            File file = new File(uri.getSchemeSpecificPart());
            path = "file:///" + file.getCanonicalFile().getAbsolutePath().replace('\\', '/');
            LOG.info("Absolute database path: " + path);
          }
          builder.uri(path);
        }
        // TODO: process other neo4j properties
        return builder.build();
      }
    

    限制:不能使用反斜杠作为目录名的一部分;)

    【讨论】:

      【解决方案2】:

      目前,正如 Thomas 正确提到的那样,您不能对嵌入式数据存储和 OGM 使用相对路径。

      你可以:

      • 使用环境变量(Thomas 提到)。请参阅the documentation,了解有关如何传递此信息的各种选项。
      • 指定org.neo4j.ogm.config.Configuration 类型的@Bean 并通过调用Paths.get(".").toAbsolutePath().normalize().toString(); 之类的方法设置URI 以获取当前目录,然后附加所需的相对目录。

      【讨论】:

        【解决方案3】:

        您可以使用 ${user.dir} 来解析绝对路径。

        uri=file:${user.dir}/target/mock_data/neo4j_${random.uuid}/graph.db
        uri=file:${user.dir}/../neo4j_${random.uuid}/graph.db
        

        【讨论】:

        • 不幸的是,这不起作用(至少在 Windows 中不起作用)——java.net.URISyntaxException: Illegal character in opaque part at index 7: file:D:\src\neo4j-loader\graph.db。我试过file:/file://,不同的错误信息但没有运气。 : 字符似乎是问题所在。将在linux中尝试
        • 这只适用于linux!发布跨平台解决方案。
        • ${user.dir} 替换为#{T(java.lang.System).getProperty('user.dir').replace('\\','/')} 我认为您需要使用SpEL 开发自己的Windows 解决方案。因为 Windows 有自己的方式来处理路径和“花哨的字符”,如反斜杠、空格、...See details。另请注意 digx1 提到的 @Bean 解决方案。你可以用 SpEL 做一些令人惊奇的事情,但编写纯 Java 代码会更容易。
        猜你喜欢
        • 2018-09-13
        • 1970-01-01
        • 1970-01-01
        • 2017-05-28
        • 2017-11-08
        • 2017-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多