Utilisation de l'API java Neo4j
Neo4j intègre une API en java qui permet de faire tout ce qu'il est possible de faire avec Cypher depuis sa propre application.

Ce graphe a été créé grâce au code suivant :
CTRL+C pour copier, CTRL+V pour coller
1
// déclaration d'une fonction raccourcie !
2
private Node createAndConnectNode(String name, Node otherNode,
3
RelationshipType relationshipType) {
4
Node node = graphdb.createNode();
5
node.setProperty("name", name);
6
node.createRelationshipTo(otherNode, relationshipType);
7
index.index(node, "name", name);
8
return node;
9
}
10
11
//Création du graphe
12
graphdb = new EmbeddedGraphDatabase("target/neo4j");
13
index = new LuceneIndexService(graphdb);
14
15
//début de la transaction
16
Transaction tx = graphdb.beginTx();
17
try {
18
Node root = graphdb.getReferenceNode();
19
// we connect Neo with the root node, to gain an entry point to the graph
20
// not neccessary but practical.
21
neo = createAndConnectNode("Neo", root, MATRIX);
22
Node morpheus = createAndConnectNode("Morpheus", neo, KNOWS);
23
Node cypher = createAndConnectNode("Cypher", morpheus, KNOWS);
24
Node trinity = createAndConnectNode("Trinity", morpheus, KNOWS);
25
Node agentSmith = createAndConnectNode("Agent Smith", cypher, KNOWS);
26
architect = createAndConnectNode("The Architect", agentSmith, HAS_CODED);
27
// Trinity loves Neo. But he doesn't know.
28
trinity.createRelationshipTo(neo, LOVES);
29
tx.success();
30
} catch (Exception e) {
31
tx.failure();
32
} finally {
33
tx.finish();
34
}
// déclaration d'une fonction raccourcie ! private Node createAndConnectNode(String name, Node otherNode, RelationshipType relationshipType) { Node node = graphdb.createNode(); node.setProperty("name", name); node.createRelationshipTo(otherNode, relationshipType); index.index(node, "name", name); return node; } //Création du graphe graphdb = new EmbeddedGraphDatabase("target/neo4j"); index = new LuceneIndexService(graphdb); //début de la transaction Transaction tx = graphdb.beginTx(); try { Node root = graphdb.getReferenceNode(); // we connect Neo with the root node, to gain an entry point to the graph // not neccessary but practical. neo = createAndConnectNode("Neo", root, MATRIX); Node morpheus = createAndConnectNode("Morpheus", neo, KNOWS); Node cypher = createAndConnectNode("Cypher", morpheus, KNOWS); Node trinity = createAndConnectNode("Trinity", morpheus, KNOWS); Node agentSmith = createAndConnectNode("Agent Smith", cypher, KNOWS); architect = createAndConnectNode("The Architect", agentSmith, HAS_CODED); // Trinity loves Neo. But he doesn't know. trinity.createRelationshipTo(neo, LOVES); tx.success(); } catch (Exception e) { tx.failure(); } finally { tx.finish(); }
Toute opération modifiant le graphe ou nécessitant des degrés d'isolation pour les données est encapsulée dans une transaction, et ainsi la récupération et le rollback sont prêts à l'emploi.
On peut ainsi facilement faire des requêtes au graphe de la manière suivante :
CTRL+C pour copier, CTRL+V pour coller
1
for (Relationship rel : neo.getRelationships(KNOWS)) {
2
Node friend = rel.getOtherNode(neo);
3
System.out.println(friend.getProperty("name"));
4
}
for (Relationship rel : neo.getRelationships(KNOWS)) { Node friend = rel.getOtherNode(neo); System.out.println(friend.getProperty("name")); }