1 /**
2 * Licensed to Neo Technology under one or more contributor
3 * license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright
5 * ownership. Neo Technology licenses this file to you under
6 * the Apache License, Version 2.0 (the "License"); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
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 // START SNIPPET: createReltype
35 private static enum ExampleRelationshipTypes implements RelationshipType
36 {
37 EXAMPLE
38 }
39 // END SNIPPET: createReltype
40
41 public static void main( final String[] args )
42 {
43 // START SNIPPET: startDb
44 GraphDatabaseService graphDb = new EmbeddedGraphDatabase( DB_PATH );
45 registerShutdownHook( graphDb );
46 // END SNIPPET: startDb
47
48 // START SNIPPET: operationsInATransaction
49 // Encapsulate operations in a transaction
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 // END SNIPPET: operationsInATransaction
65
66 // START SNIPPET: removingData
67 // let's remove the data before committing
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 // END SNIPPET: removingData
80
81 System.out.println( "Shutting down database ..." );
82 // START SNIPPET: shutdownServer
83 graphDb.shutdown();
84 // END SNIPPET: shutdownServer
85 }
86
87 private static void registerShutdownHook( final GraphDatabaseService graphDb )
88 {
89 // Registers a shutdown hook for the Neo4j instance so that it
90 // shuts down nicely when the VM exits (even if you "Ctrl-C" the
91 // running example before it's completed)
92 Runtime.getRuntime().addShutdownHook( new Thread()
93 {
94 @Override
95 public void run()
96 {
97 graphDb.shutdown();
98 }
99 } );
100 }
101 }