Micha Kops avatar Micha Kops committed 7f1c58d

initial import

Comments (0)

Files changed (6)

+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" output="target/classes" path="src/main/java"/>
+	<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
+	<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
+	<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
+	<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
+	<classpathentry kind="output" path="target/classes"/>
+</classpath>
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>neo4j-tutorial</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>org.eclipse.m2e.core.maven2Builder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+		<nature>org.eclipse.m2e.core.maven2Nature</nature>
+	</natures>
+</projectDescription>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>com.hascode.tutorial</groupId>
+  <artifactId>neo4j-tutorial</artifactId>
+  <version>0.0.1-SNAPSHOT</version>
+  <dependencies>
+  	<dependency>
+  		<groupId>org.neo4j</groupId>
+  		<artifactId>neo4j</artifactId>
+  		<version>1.5</version>
+  	</dependency>
+  </dependencies>
+</project>

src/main/java/com/hascode/tutorial/IndexSearchExample.java

+package com.hascode.tutorial;
+
+import org.neo4j.graphdb.GraphDatabaseService;
+import org.neo4j.graphdb.Node;
+import org.neo4j.graphdb.Relationship;
+import org.neo4j.graphdb.Transaction;
+import org.neo4j.graphdb.index.Index;
+import org.neo4j.kernel.EmbeddedGraphDatabase;
+
+public class IndexSearchExample {
+	private static String DB_PATH = "/tmp/neo4j";
+
+	public static void main(final String[] args) {
+		GraphDatabaseService graphDb = new EmbeddedGraphDatabase(DB_PATH);
+		Index<Node> nodeIndex = graphDb.index().forNodes("nodes");
+		registerShutdownHook(graphDb);
+
+		Transaction tx = graphDb.beginTx();
+		try {
+			Node userNode1 = graphDb.createNode();
+			userNode1.setProperty("id", 1);
+			userNode1.setProperty("name", "Peter");
+			nodeIndex.add(userNode1, "id", 1);
+			nodeIndex.add(userNode1, "name", "Peter");
+
+			Node userNode2 = graphDb.createNode();
+			userNode2.setProperty("id", 2);
+			userNode2.setProperty("name", "Ray");
+			nodeIndex.add(userNode2, "id", 2);
+			nodeIndex.add(userNode2, "name", "Ray");
+
+			Relationship relation = userNode1.createRelationshipTo(userNode2,
+					RelTypes.KNOWS);
+			relation.setProperty("message", "knows");
+			tx.success();
+			System.out.println(String.format("%s %s %s",
+					userNode1.getProperty("name"),
+					relation.getProperty("message"),
+					userNode2.getProperty("name")));
+
+			System.out.println("searching for user with id=2..");
+			Node user = nodeIndex.get("id", 2).getSingle();
+			System.out.println("The name of the user with id=2 is: "
+					+ user.getProperty("name"));
+
+			System.out.println("searching for user with name=Peter..");
+			Node user2 = nodeIndex.get("name", "Peter").getSingle();
+			System.out.println("The id of the user with name=Peter is: "
+					+ user2.getProperty("id"));
+		} finally {
+			tx.finish();
+			graphDb.shutdown();
+		}
+
+	}
+
+	private static void registerShutdownHook(final GraphDatabaseService graphDb) {
+		// Registers a shutdown hook for the Neo4j instance so that it
+		// shuts down nicely when the VM exits (even if you "Ctrl-C" the
+		// running example before it's completed)
+		Runtime.getRuntime().addShutdownHook(new Thread() {
+			@Override
+			public void run() {
+				graphDb.shutdown();
+			}
+		});
+	}
+}

src/main/java/com/hascode/tutorial/NodeTraversalExample.java

+package com.hascode.tutorial;
+
+import org.neo4j.graphdb.Direction;
+import org.neo4j.graphdb.GraphDatabaseService;
+import org.neo4j.graphdb.Node;
+import org.neo4j.graphdb.Relationship;
+import org.neo4j.graphdb.ReturnableEvaluator;
+import org.neo4j.graphdb.StopEvaluator;
+import org.neo4j.graphdb.Transaction;
+import org.neo4j.graphdb.Traverser;
+import org.neo4j.graphdb.Traverser.Order;
+import org.neo4j.kernel.EmbeddedGraphDatabase;
+
+public class NodeTraversalExample {
+	private static String DB_PATH = "/tmp/neo4j";
+
+	public static void main(final String[] args) {
+		GraphDatabaseService graphDb = new EmbeddedGraphDatabase(DB_PATH);
+		registerShutdownHook(graphDb);
+
+		Transaction tx = graphDb.beginTx();
+		try {
+			Node peterNode = graphDb.createNode();
+			peterNode.setProperty("id", 1);
+			peterNode.setProperty("name", "Peter");
+
+			Node rayNode = graphDb.createNode();
+			rayNode.setProperty("id", 2);
+			rayNode.setProperty("name", "Ray");
+
+			Node egonNode = graphDb.createNode();
+			egonNode.setProperty("id", 3);
+			egonNode.setProperty("name", "Egon");
+
+			Node winstonNode = graphDb.createNode();
+			winstonNode.setProperty("id", 4);
+			winstonNode.setProperty("name", "Winston");
+
+			Node slimerNode = graphDb.createNode();
+			slimerNode.setProperty("id", 5);
+			slimerNode.setProperty("name", "Slimer");
+
+			Relationship rel1 = peterNode.createRelationshipTo(rayNode,
+					RelTypes.KNOWS);
+			rel1.setProperty("visibility", "public");
+
+			Relationship rel2 = rayNode.createRelationshipTo(egonNode,
+					RelTypes.KNOWS);
+			rel2.setProperty("visibility", "hidden");
+
+			Relationship rel3 = rayNode.createRelationshipTo(winstonNode,
+					RelTypes.KNOWS);
+			rel3.setProperty("visibility", "public");
+
+			Relationship rel4 = winstonNode.createRelationshipTo(slimerNode,
+					RelTypes.KNOWS);
+			rel4.setProperty("visibility", "public");
+
+			tx.success();
+
+			System.out.println("traversing nodes for Peter's acquaintances..");
+			Traverser acquaintanceTraverser = getAcquaintances(peterNode);
+			for (Node acquaintanceNode : acquaintanceTraverser.getAllNodes()) {
+				System.out.println("Peter knows "
+						+ acquaintanceNode.getProperty("name") + " (id: "
+						+ acquaintanceNode.getProperty("id") + ") at depth: "
+						+ acquaintanceTraverser.currentPosition().depth());
+			}
+		} finally {
+			tx.finish();
+		}
+
+		graphDb.shutdown();
+	}
+
+	private static Traverser getAcquaintances(final Node personNode) {
+		return personNode.traverse(Order.BREADTH_FIRST,
+				StopEvaluator.END_OF_GRAPH,
+				ReturnableEvaluator.ALL_BUT_START_NODE, RelTypes.KNOWS,
+				Direction.OUTGOING);
+	}
+
+	private static void registerShutdownHook(final GraphDatabaseService graphDb) {
+		// Registers a shutdown hook for the Neo4j instance so that it
+		// shuts down nicely when the VM exits (even if you "Ctrl-C" the
+		// running example before it's completed)
+		Runtime.getRuntime().addShutdownHook(new Thread() {
+			@Override
+			public void run() {
+				graphDb.shutdown();
+			}
+		});
+	}
+}

src/main/java/com/hascode/tutorial/RelTypes.java

+package com.hascode.tutorial;
+
+import org.neo4j.graphdb.RelationshipType;
+
+public enum RelTypes implements RelationshipType {
+	KNOWS
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.