首页 > 解决方案 > 如何使用java在neo4j中的数据库之间切换

问题描述

所以我使用的是装有 docker 的 neo4j-enterprise版本。

version: '3'
services:
  neo4j:
    image: neo4j:4.3.2-enterprise
    hostname: neo4j
    container_name: neo4j
    ports:
      - "7474:7474"
      - "7687:7687"
    environment:
      - NEO4J_ACCEPT_LICENSE_AGREEMENT=yes
      - NEO4J_dbms_memory_heap_maxSize=1G
      - NEO4JLABS_PLUGINS=["apoc"]
      - NEO4J_AUTH=neo4j/test
      - NEO4J_apoc_import_file_enabled=true
      - NEO4J_dbms_security_procedures_whitelist= apoc.*
      - NEO4J_dbms_security_procedures_unrestricted= apoc.*

我正在使用螺栓驱动器与 neo4j 连接。

public Neo4jSessionFactory(@ConfigProperty(name = "quarkus.neo4j.uri")String databaseUri,
                               @ConfigProperty(name = "quarkus.neo4j.authentication.username")String username,
                               @ConfigProperty(name = "quarkus.neo4j.authentication.password")String password) {
        Configuration neoConfig = new Configuration.Builder().uri(databaseUri**{bolt://localhost:7687}**).credentials(username, password)
                .useNativeTypes().build();
        sessionFactory = new SessionFactory(neoConfig, PACKAGES);
        LOGGER.info("Connection with Neo4j is successful");
    }

我能够使用"CREATE DATABASE <name>"cmd 创建一个单独的数据库。

但是我想在新创建的数据库中运行我的查询。我可以简单地称"session.query("cypher_query_to_insert")"数据将被加载到默认数据库 neo4j。我们可以在浏览器中使用:use <database name>. 但是,当 cmd 在内部运行时,session.query(":use <database_name>") 我收到以下错误:

数据插入失败。原因:org.neo4j.driver.exceptions.ClientException:无效输入':':预期(第1行,第1列(偏移量:0))“:使用neo4j”

如果有人知道解决方案,请告诉我。

标签: neo4jspring-data-neo4jneo4j-ogm

解决方案


创建会话时,您可以传递SessionConfig具有数据库名称的实例,通过以下方式构建SessionConfig.Builderhttps ://neo4j.com/docs/api/java-driver/current/org/neo4j/driver/SessionConfig.Builder.html#withDatabase -java.lang.String-


推荐阅读