简体   繁体   中英

playing neo4j in embedded database way

I am pretty new to neo4j, i want to create a application.

By remote server mode, it seems only REST API can be used to connect to the neo4j. So I decide to use embedded database as I want to use JAVA API provided by neo4j.

There is an example in the tutorial to create the connection:

private static final String DB_PATH = "C:/Users/Hao/Documents/Neo4j/TGI_test_backup";

public static void main( String[] args ) throws IOException
{
    FileUtils.deleteRecursively( new File( DB_PATH ) );
    GraphDatabaseService database = new GraphDatabaseFactory().newEmbeddedDatabase( DB_PATH );
    GraphDatabaseService database = new GraphDatabaseFactory().newEmbeddedDatabase( DB_PATH );
    TraversalExample example = new TraversalExample( database );
    Node joe = example.createData();
    example.run( joe );
}

public TraversalExample( GraphDatabaseService db )
{
    this.db = db;
    // START SNIPPET: basetraverser
    friendsTraversal = db.traversalDescription()
            .depthFirst()
            .relationships( Rels.KNOWS )
            .uniqueness( Uniqueness.RELATIONSHIP_GLOBAL );
    // END SNIPPET: basetraverser
}

private Node createData()
{
    String query = "CREATE (joe {name: 'Joe'}), (sara {name: 'Sara'}), "
       + "(lisa {name: 'Lisa'}), (peter {name: 'PETER'}), (dirk {name: 'Dirk'}), "
                   + "(lars {name: 'Lars'}), (ed {name: 'Ed'}),"
       + "(joe)-[:KNOWS]->(sara), (lisa)-[:LIKES]->(joe), "
       + "(peter)-[:KNOWS]->(sara), (dirk)-[:KNOWS]->(peter), "
       + "(lars)-[:KNOWS]->(drk), (ed)-[:KNOWS]->(lars), "
       + "(lisa)-[:KNOWS]->(lars) "
       + "RETURN joe";
    Result result = db.execute( query );
    Object joe = result.columnAs( "joe" ).next();
    if ( joe instanceof Node )
    {
        return (Node) joe;
    }
    else
    {
        throw new RuntimeException( "Joe isn't a node!" );
    }
}

It seems every time I run this code, it creates a new database instance, and all existed data will be overwritten.

But data needs to be saved and I will not import all data into database in the code...

How to resolve this problem? What I need is get a connection to the database and use the existed data.

Thanks.

You see the first line of your main , that says FileUtils.deleteRecursively( new File( DB_PATH ) ); ? It creates a File object for your DBPATH and then it deletes recursively everything there. That's equivalent to selecting the database directory in your file explorer and deleting it. So first thing is to remove that line from your code and see if instead of deleting and recreating your database each time it will connect to the existing database.

I just add my two cents for your question.

You can understand REST API for Neo4j server as transport layer. You can use it and also you can use drivers. Those drivers are using REST API underneath, but you don't know about it. Also you can use JDBC.

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