0% found this document useful (0 votes)
58 views

Basic Neo4j Code Examples 2008-05-08

This document contains code snippets demonstrating how to build and traverse a graph database using Neo4j. The code first shows how to create nodes and relationships to build a social network graph of "The Matrix" characters. It then demonstrates traversing the graph to find "Mr. Anderson's friends." Finally, the domain is evolved to find friends who are "in love" by customizing the traversal logic.

Uploaded by

mmyoung2
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Basic Neo4j Code Examples 2008-05-08

This document contains code snippets demonstrating how to build and traverse a graph database using Neo4j. The code first shows how to create nodes and relationships to build a social network graph of "The Matrix" characters. It then demonstrates traversing the graph to find "Mr. Anderson's friends." Finally, the domain is evolved to find friends who are "in love" by customizing the traversal logic.

Uploaded by

mmyoung2
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

some code snippets

Neo

Emil Eifrem

2008-05-08, API v1.0-rc1-SNAPSHOT

A few brief Neo4j code slides


The following is a few slides from a live presentation hopefully the code is selfexplanatory But if it isnt, please join the discussion on the mailing list @ http://lists.neo4j.org First: how to create a node space Second: how to traverse that node space

Example: The Matrix social graph


name = The Architect name = Morpheus rank = Captain occupation = Total badass disclosure = public name = Thomas Anderson age = 29

42

KNOWS

7
S KNOW

KNOWS

KN O WS

CODED_BY

KN

OW

name = Cypher last name = Reagan disclosure = secret age = 6 months

13
name = Agent Smith version = 1.0b language = C++

age = 3 days

2
name = Trinity

Code (1): Building a node space


NeoService neo = ... // Get factory // Create Thomas 'Neo' Anderson Node mrAnderson = neo.createNode(); mrAnderson.setProperty( "name", "Thomas Anderson" ); mrAnderson.setProperty( "age", 29 ); // Create Morpheus Node morpheus = neo.createNode(); morpheus.setProperty( "name", "Morpheus" ); morpheus.setProperty( "rank", "Captain" ); morpheus.setProperty( "occupation", "Total bad ass" ); // Create a relationship representing that they know each other mrAnderson.createRelationshipTo( morpheus, RelTypes.KNOWS ); // ...create Trinity, Cypher, Agent Smith, Architect similarly

Code (1): Building a node space


NeoService neo = ... // Get factory Transaction tx = neo.beginTransaction(); // Create Thomas 'Neo' Anderson Node mrAnderson = neo.createNode(); mrAnderson.setProperty( "name", "Thomas Anderson" ); mrAnderson.setProperty( "age", 29 ); // Create Morpheus Node morpheus = neo.createNode(); morpheus.setProperty( "name", "Morpheus" ); morpheus.setProperty( "rank", "Captain" ); morpheus.setProperty( "occupation", "Total bad ass" ); // Create a relationship representing that they know each other mrAnderson.createRelationshipTo( morpheus, RelTypes.KNOWS ); // ...create Trinity, Cypher, Agent Smith, Architect similarly tx.commit(); // Pseudo code, obviously wrap it in try-finally

Traversal: Find Mr Andersons friends


name = The Architect name = Morpheus rank = Captain occupation = Total badass disclosure = public name = Thomas Anderson age = 29

42

KNOWS

7
S KNOW

KNOWS

KN O WS

CODED_BY

KN

OW

name = Cypher last name = Reagan disclosure = secret age = 6 months

13
name = Agent Smith version = 1.0b language = C++

age = 3 days

2
name = Trinity

What do we want to do?


We want to find all Mr Andersons transitive friends So conceptually, we want to traverse, starting from the Mr Anderson node... ... breadth first (closest friends first) ... until the end of the network (ALL friends) ... returning all nodes we visit, except the first one (only Mr Andersons friends, not Mr Anderson himself) ... but only traverse relationships of the KNOWS type in the OUTGOING direction

Code (2): Traversing a node space


// Instantiate a traverser that returns Mr Anderson's friends Traverser friendsTraverser = mrAnderson.traverse( Traverser.Order.BREADTH_FIRST, StopEvaluator.END_OF_NETWORK, ReturnableEvaluator.ALL_BUT_START_NODE, RelTypes.KNOWS, Direction.OUTGOING ); // Traverse the node space and print out the result System.out.println( "Mr Anderson's friends:" ); for ( Node friend : friendsTraverser ) { System.out.printf( "At depth %d => %s%n", friendsTraverser.currentPosition().getDepth(), friend.getProperty( "name" ) ); }

name = The Architect name = Morpheus rank = Captain occupation = Total badass disclosure = public

name = Thomas Anderson age = 29

42

KNOWS

7
KNOW S

KNOWS

KN O WS

CODED_BY

KN

OW

name = Cypher last name = Reagan disclosure = secret age = 6 months

13
name = Agent Smith version = 1.0b language = C++

age = 3 days

2
name = Trinity

$ bin/start-neo-example Mr Anderson's friends: At At At At $ depth depth depth depth 1 1 2 3 => => => => Morpheus Trinity Cypher Agent Smith

friendsTraverser = mrAnderson.traverse( Traverser.Order.BREADTH_FIRST, StopEvaluator.END_OF_NETWORK, ReturnableEvaluator.ALL_BUT_START_NODE, RelTypes.KNOWS, Direction.OUTGOING );

Evolving the domain: Friends in love?


name = The Architect name = Morpheus rank = Captain occupation = Total badass disclosure = public name = Thomas Anderson age = 29

42

KNOWS

7
S KNOW

KNOWS

KN O WS

CODED_BY

KN LO

OW

name = Cypher last name = Reagan disclosure = secret age = 6 months

13
name = Agent Smith version = 1.0b language = C++

VE

2
name = Trinity

What do we want to do?


Weve now extended the domain with completely new functionality Note how we dont have any predefined schemas we could even create the new reltype dynamically without restarting our app Conceptually, we want to find everyone amongst Mr Andersons friends who has a crush on someone So we still want to traverse all Mr Andersons friends (like last time) But this time we only want to return the nodes that has an OUTGOING relationship of the LOVES type

Code (3a): Custom traverser


// Create a traverser that returns all friends in love Traverser loveTraverser = mrAnderson.traverse( Traverser.Order.BREADTH_FIRST, StopEvaluator.END_OF_NETWORK, new ReturnableEvaluator() { public boolean isReturnableNode( TraversalPosition pos ) { return pos.currentNode().hasRelationship( RelTypes.LOVES, Direction.OUTGOING ); } }, RelTypes.KNOWS, Direction.OUTGOING );

Code (3a): Custom traverser


// Traverse the node space and print out the result System.out.println( "Whos in love?" ); for ( Node person : loveTraverser ) { System.out.printf( "At depth %d => %s%n", loveTraverser.currentPosition().getDepth(), person.getProperty( "name" ) ); }

name = The Architect name = Morpheus rank = Captain occupation = Total badass disclosure = public

name = Thomas Anderson age = 29

42

1
S LO VE

KNOW S
KN

7
KNOW S

KNOW S

K NO W S

CODED_BY

OW

name = Cypher last name = Reagan disclosure = secret age = 6 months

13
name = Agent Smith version = 1.0b language = C++

2
name = Trinity

new ReturnableEvaluator() { public boolean isReturnableNode( TraversalPosition pos) { return pos.currentNode(). hasRelationship( RelTypes.LOVES, Direction.OUTGOING ); } },

$ bin/start-neo-example Whos in love? At depth 1 => Trinity $

Summary
API details http://api.neo4j.org Feedback http://lists.neo4j.org Download http://neo4j.org/download Business http://neotechnology.com

www.neo4j.org

You might also like