Database Reference
In-Depth Information
Java code API and embedding the Neo4j database
Neo4j provides a rich set of Java APIs that help in embedding the Neo4j database within
the Java/J2EE application as a headless application. Let's see how it works:
1. Shut down the Neo4j server by executing $NEO4J_HOME/bin/neo4j stop .
Now using any IDE, create a new Java project and define the following dependen-
cies to your project:
◦ JDK 1.7
◦ All JAR files in $NEO4J_HOME/lib/
2. Create a new Java file and add the following code:
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseBuilder;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.tooling.GlobalGraphOperations;
public class FetchAllData {
public static void main(String[] args) {
//Complete path of the database files on local
System
//Use the same path "$NEO4J_HOME/data/graph.db"
String location = "$NEO4J_HOME/data/graph.db";
//Create instance of Graph Database.
GraphDatabaseFactory fac = new
GraphDatabaseFactory();
GraphDatabaseBuilder build =
fac.newEmbeddedDatabaseBuilder(location);
GraphDatabaseService dbService =
build.newGraphDatabase();
//Starting a Transaction...All CRUD operations
should be in Transaction
try (Transaction tx = dbService.beginTx()) {
GlobalGraphOperations operations =
Search WWH ::




Custom Search