如何与Neo4j中的本地数据库连接?

时间:2019-02-18 11:56:24

标签: java neo4j cypher

只是为了从Java代码进行一些密码查询测试,我尝试按照以下步骤操作

  1. 下载Neo4j并解压缩到Windows计算机中的目录中
  2. 此后,我运行两个命令bin\neo4j install-servicebin\neo4j start
  3. 然后在Chrome浏览器中{http://localhost:7474/browser/]验证Neo4j是否正常工作。

现在在Java代码(Junit测试用例)方面,我有以下代码

private GraphDatabaseService db;
private ExecutionEngine executionEngine;
 @Before
    public void init() {
        this.db = new ImpermanentGraphDatabase();
        this.executionEngine = new ExecutionEngine(this.db);
}

上面的代码使用了一个内部数据库,因此我修改了此代码,并尝试使用我在上面安装的Neo4j服务器

    @Before
    public void init() {
        GraphDatabaseFactory factory = new GraphDatabaseFactory();
        this.db = factory.newEmbeddedDatabase("C:\\Software\\neo4j-enterprise-3.5.1-windows\\neo4j-enterprise-3.5.1\\data\\databases");
        this.executionEngine = new ExecutionEngine(this.db);
    }

但是在运行完所有测试用例之后(其中也包括创建Node的测试用例),它在db Person2中显示了两个节点,而在Junit测试用例中,代码如下所示

 @Test
    public void executeCount() {
        final Transaction tx = db.beginTx();
        try {
            for (int i = 0; i < 10; i++) {
                db.createNode();
            }
            tx.success();
        } catch (final Exception e) {
            tx.failure();
        } finally {
            tx.finish();
        }
        final String countStatement = 
                "START n=node(*) " +
                "RETURN count(n) as nodeCount";
        final ExecutionResult result = this.executionEngine.execute(countStatement);
        Assert.assertEquals(11l, result.columnAs("nodeCount").next());
    }

Traversal API

 @Test
    public void traverse() {
        final Transaction tx = db.beginTx();
        long userId = 0l;
        try {
            final Node user = db.createNode();
            user.setProperty("name", "Timmy");
            userId = user.getId();
            final Node database = db.createNode();
            database.setProperty("name", "Neo4j");
            user.createRelationshipTo(database, Relationships.LIKES);
            final Node language = db.createNode();
            language.setProperty("name", "Cypher");
            database.createRelationshipTo(language, Relationships.USES);
            tx.success();
        } catch (final Exception e) {
            tx.failure();
        } finally {
            tx.finish();
        }
        final String countStatement = 
                "START u=node(" + userId + ") " +
                "MATCH u-[:LIKES]-db-[:USES]-l " +
                "RETURN u.name as userName, db.name as dbName, l.name as languageName";
        final ExecutionResult result = this.executionEngine.execute(countStatement);
        final Map<String,Object> resultMap = result.iterator().next();
        Assert.assertEquals("Timmy", resultMap.get("userName"));
        Assert.assertEquals("Neo4j", resultMap.get("dbName"));
        Assert.assertEquals("Cypher", resultMap.get("languageName"));
    }

有人可以告诉我如何使用安装的Neo4j Db并在运行DB而不是嵌入式DB的Neo4j中获取所有创建的节点吗?

如果必须正常完成连接,我们可以使用Driver.getConnection("URL of Server"),但是在Neo4j情况下,可以通过使用GraphDatabaseService类来实现吗?

0 个答案:

没有答案