1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.neo4j.examples;
21
22 import org.neo4j.graphdb.Direction;
23 import org.neo4j.graphdb.GraphDatabaseService;
24 import org.neo4j.graphdb.Node;
25 import org.neo4j.graphdb.RelationshipType;
26 import org.neo4j.graphdb.Transaction;
27 import org.neo4j.kernel.EmbeddedGraphDatabase;
28
29 public class EmbeddedNeo4j
30 {
31 private static final String DB_PATH = "neo4j-store";
32 private static final String NAME_KEY = "name";
33
34
35 private static enum ExampleRelationshipTypes implements RelationshipType
36 {
37 EXAMPLE
38 }
39
40
41 public static void main( final String[] args )
42 {
43
44 GraphDatabaseService graphDb = new EmbeddedGraphDatabase( DB_PATH );
45 registerShutdownHook( graphDb );
46
47
48
49
50 Transaction tx = graphDb.beginTx();
51 try
52 {
53 Node firstNode = graphDb.createNode();
54 firstNode.setProperty( NAME_KEY, "Hello" );
55 Node secondNode = graphDb.createNode();
56 secondNode.setProperty( NAME_KEY, "World" );
57
58 firstNode.createRelationshipTo( secondNode,
59 ExampleRelationshipTypes.EXAMPLE );
60
61 String greeting = firstNode.getProperty( NAME_KEY ) + " "
62 + secondNode.getProperty( NAME_KEY );
63 System.out.println( greeting );
64
65
66
67
68 firstNode.getSingleRelationship( ExampleRelationshipTypes.EXAMPLE,
69 Direction.OUTGOING ).delete();
70 firstNode.delete();
71 secondNode.delete();
72
73 tx.success();
74 }
75 finally
76 {
77 tx.finish();
78 }
79
80
81 System.out.println( "Shutting down database ..." );
82
83 graphDb.shutdown();
84
85 }
86
87 private static void registerShutdownHook( final GraphDatabaseService graphDb )
88 {
89
90
91
92 Runtime.getRuntime().addShutdownHook( new Thread()
93 {
94 @Override
95 public void run()
96 {
97 graphDb.shutdown();
98 }
99 } );
100 }
101 }