简体   繁体   中英

Neo4j: How to open an existing database in neo4j using java api?

How to use the existing database in neo4j using java api? I already created a neo4j database and wanted to use it for several queries. However, when I opened the existing database I created and did some query, it returned nothing. The fragment of code I am using is as follows:

private static final String DB_PATH = "c:/Users/Reed/workspace/test/target1/ttldb";
GraphDatabaseService db = new GraphDatabaseFactory().newEmbeddedDatabase( DB_PATH );


ExecutionEngine engine = new ExecutionEngine( db );

ExecutionResult result;

try ( Transaction tx = db.beginTx(); )
{

    result = engine.execute( "match (n) return n" );

    Iterator<Node> n_column = result.columnAs( "n" );
    for ( Node node : IteratorUtil.asIterable( n_column ) )
    {

        nodeResult = node + ": " + node.getProperty( "name" );

        System.out.println(nodeResult);
    }
    tx.success();

}

Any suggestions? Thank you in advance.

If you are using Neo4j in embedded mode ie it runs within the same JVM as your application, you can access it using:

GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DBPATH)

where DBPATH is the path to the database you created using Webadmin. You can find that path in your neo4j install directory/conf/neo4j-server.properties (The property name is org.neo4j.server.database.location)

Once you have instantiated your graphDb, you can Execute Cypher queries from Java

If you are not using Neo4j in embedded mode and want to connect to the existing server running on port 7474, you can use the java rest binding: https://github.com/neo4j/java-rest-binding/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM