neo4j - 如何使用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”
如果有人知道解决方案,请告诉我。
解决方案
创建会话时,您可以传递SessionConfig
具有数据库名称的实例,通过以下方式构建SessionConfig.Builder
:https ://neo4j.com/docs/api/java-driver/current/org/neo4j/driver/SessionConfig.Builder.html#withDatabase -java.lang.String-。
推荐阅读
- java - spring boot parent 防止资源:resources
- javascript - 如何在 JSX div 的 id 中添加 if 条件?
- swift - 如何在功能字段中使用父级?
- database-design - sql中的存储技术
- hibernate - 如何用 JPA 和 hibernate 管理一个简单的博客表结构?
- python - 根据 ID 状态转换数据帧
- javascript - 检查数组是否为空时,我应该使用 array.length <= 0 还是 array.length === 0?
- sorting - 在记事本++中的某个字符之后对行进行排序
- python - Holoviews:如何将多个 RGB 图像包装到数据集中并正确显示它们?
- python - 如何在烧瓶中一一处理请求?