如何通过Bolt暴露基于OGM的嵌入式Neo4J数据库并使用Neo4J Browser访问?
问题场景
我正在使用搭配OGM的Neo4J嵌入式数据库,通过OGM SessionFactory在指定目录创建数据库服务,代码如下:
Configuration configuration = new Configuration.Builder() .uris("C:\\neoEmbeddedDb") .build(); factory = new SessionFactory(configuration, packages);
这段代码运行正常,但我希望用Neo4J Browser工具浏览已创建的数据库。查资料了解到需要通过Bolt协议暴露数据库才能实现访问,在Neo4J嵌入式文档里,直接使用GraphDatabaseService并配置Bolt驱动就能暴露端口:
GraphDatabaseService graphDb = new GraphDatabaseFactory() .newEmbeddedDatabaseBuilder( DB_PATH ) .setConfig( bolt.type, "BOLT" ) .setConfig( bolt.enabled, "true" ) .setConfig( bolt.address, "localhost:7687" ) .newGraphDatabase();
但OGM的SessionFactory没有对应的配置选项。我尝试过用Configuration Builder设置多个URI:
Configuration configuration = new Configuration.Builder() .uris(new String[]{this.databasePath.toUri().toString(), "localhost:7687"}) .build();
结果发现这种方式忽略了第一个文件路径URI,转而在临时位置创建数据库,调试日志显示:
Creating temporary file store: file:/C:/Temp/neo4jTmpEmbedded.db2736315981519762299/database/
请问该如何通过Bolt暴露我的嵌入式数据库,或者用其他方式让Neo4J Browser访问它?
解决方案
在meistermeier的帮助下,我成功创建了带Bolt暴露的嵌入式数据库,并让OGM连接到它。按照文档添加Bolt连接选项后,数据库既会创建在指定路径,也能通过Bolt正常暴露,现在可以用Neo4J Desktop的Windows Browser连接访问了。最终代码如下:
BoltConnector boltConnector = new BoltConnector(_BOLT_CONNECTION_STRING); GraphDatabaseService graphDb = new GraphDatabaseFactory() .newEmbeddedDatabaseBuilder(databasePath.toFile()) .setConfig(boltConnector.type, "BOLT" ) .setConfig(boltConnector.enabled, "true" ) .setConfig(boltConnector.listen_address, "localhost:7687" ) .setConfig(GraphDatabaseSettings.auth_enabled, "false") .newGraphDatabase(); registerShutdownHook(graphDb); // 连接OGM SessionFactory到嵌入式数据库 EmbeddedDriver driver = new EmbeddedDriver(graphDb); final String[] packages = new String[] { "Entity domain package", }; factory = new SessionFactory(driver, packages);
内容的提问来源于stack exchange,提问作者Christian Huber




