在Neo4j图形数据库中创建/设置RootNode?

时间:2013-08-25 20:56:15

标签: neo4j neo4jclient

如何将树中的一个节点设置为根节点? 假设我的索引从115开始,但是当我使用点网应用程序中的Neo4jClient连接到数据库时,我将根节点设为null?是否可以将任何节点设置为根节点?

2 个答案:

答案 0 :(得分:1)

使用标准API是不可能的,但这里有一个小技巧,假设你可以运行一些Java代码。它允许您创建新的根节点,我认为没有办法更改节点ID。

public class RootNodeCreator {

    /**
     * Create the root node. Make sure the database is stopped when running this.
     *
     * @param pathToDatabase path to the database.
     */
    public void createRoot(String pathToDatabase) {
        BatchInserter inserter = BatchInserters.inserter(pathToDatabase);
        inserter.createNode(0, new HashMap<String, Object>());
        inserter.shutdown();
    }
}

和测试:

@Test
public void verifyRootCreation() throws IOException {
    TemporaryFolder temporaryFolder = new TemporaryFolder();
    temporaryFolder.create();

    GraphDatabaseService database = new GraphDatabaseFactory().newEmbeddedDatabase(temporaryFolder.getRoot().getAbsolutePath());
    Transaction tx = database.beginTx();
    try {
        database.getNodeById(0).delete();
        tx.success();
    }
    finally {
        tx.finish();
    }


    try {
        database.getNodeById(0);
        fail();
    } catch (NotFoundException e) {
        //ok
    }

    database.shutdown();

    new RootNodeCreator().createRoot(temporaryFolder.getRoot().getAbsolutePath());

    database = new GraphDatabaseFactory().newEmbeddedDatabase(temporaryFolder.getRoot().getAbsolutePath());
    assertNotNull(database.getNodeById(0));
}

答案 1 :(得分:0)

这不可能通过REST API实现,因此Neo4jClient无法支持。