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.