Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Basic Neo4j Code Examples 2008 05 08
1. Neo
some code snippets
Emil Eifrem
2008-05-08, API v1.0-rc1-SNAPSHOT
2. A few brief Neo4j code slides
The following is a few slides from a live
presentation – hopefully the code is self-
explanatory
But if it isn’t, 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
3. Example: The Matrix social graph
name = “The Architect”
name = “Morpheus”
rank = “Captain”
name = “Thomas Anderson”
occupation = “Total badass” 42
age = 29
disclosure = public
KNOWS KNOWS CODED_BY
1 KN O
7 3 WS
KN 13
S
OW name = “Cypher”
KNOW
S last name = “Reagan”
name = “Agent Smith”
disclosure = secret version = 1.0b
age = 3 days age = 6 months language = C++
2
name = “Trinity”
4. 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
5. 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
6. Traversal: Find Mr Anderson’s friends
name = “The Architect”
name = “Morpheus”
rank = “Captain”
name = “Thomas Anderson”
occupation = “Total badass” 42
age = 29
disclosure = public
KNOWS KNOWS CODED_BY
1 KN O
7 3 WS
KN 13
S
OW name = “Cypher”
KNOW
S last name = “Reagan”
name = “Agent Smith”
disclosure = secret version = 1.0b
age = 3 days age = 6 months language = C++
2
name = “Trinity”
7. What do we want to do?
We want to find all Mr Anderson’s 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 Anderson’s friends, not Mr
Anderson himself)
... but only traverse relationships of the KNOWS
type in the OUTGOING direction
8. 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" ) );
}
9. name = “The Architect”
name = “Morpheus”
rank = “Captain”
name = “Thomas Anderson”
occupation = “Total badass” 42
age = 29
disclosure = public
KNOWS KNOWS CODED_BY
1 KN O
7 3 WS
13
S
KN
KNOW
name = “Cypher”
OW last name = “Reagan”
S
name = “Agent Smith”
disclosure = secret version = 1.0b
age = 3 days age = 6 months language = C++
2
name = “Trinity”
$ bin/start-neo-example
Mr Anderson's friends:
At depth 1 => Morpheus
friendsTraverser = mrAnderson.traverse(
Traverser.Order.BREADTH_FIRST, At depth 1 => Trinity
StopEvaluator.END_OF_NETWORK, At depth 2 => Cypher
ReturnableEvaluator.ALL_BUT_START_NODE,
RelTypes.KNOWS,
At depth 3 => Agent Smith
Direction.OUTGOING ); $
10. Evolving the domain: Friends in love?
name = “The Architect”
name = “Morpheus”
rank = “Captain”
name = “Thomas Anderson”
occupation = “Total badass” 42
age = 29
disclosure = public
KNOWS KNOWS CODED_BY
1 KN O
7 3 WS
13
S
KN name = “Cypher”
KNOW
OW last name = “Reagan”
S
name = “Agent Smith”
LO disclosure = secret version = 1.0b
VE age = 6 months language = C++
S
2
name = “Trinity”
11. What do we want to do?
We’ve now extended the domain with completely
new functionality
Note how we don’t have any predefined
schemas – we could even create the new reltype
dynamically without restarting our app
Conceptually, we want to find everyone amongst
Mr Anderson’s friends who has a crush on someone
So we still want to traverse all Mr Anderson’s
friends (like last time)
But this time we only want to return the nodes that
has an OUTGOING relationship of the LOVES type
12. 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 );
13. Code (3a): Custom traverser
// Traverse the node space and print out the result
System.out.println( "Who’s in love?" );
for ( Node person : loveTraverser )
{
System.out.printf( "At depth %d => %s%n",
loveTraverser.currentPosition().getDepth(),
person.getProperty( "name" ) );
}
14. name = “The Architect”
name = “Morpheus”
rank = “Captain”
name = “Thomas Anderson”
occupation = “Total badass” 42
age = 29
disclosure = public
KNOW KNOW CODED_BY
1 K NO
S 7 S 3 S W
KN
13
name = “Cypher”
KNOW
S OW last name = “Reagan”
name = “Agent Smith”
LO
VE S disclosure = secret version = 1.0b
age = 6 months language = C++
S 2
name = “Trinity”
$ bin/start-neo-example
new ReturnableEvaluator()
Who’s in love?
{
public boolean isReturnableNode(
TraversalPosition pos)
At depth 1 => Trinity
{ $
return pos.currentNode().
hasRelationship( RelTypes.LOVES,
Direction.OUTGOING );
}
},
15. Summary
API details
http://api.neo4j.org
Feedback
http://lists.neo4j.org
Download
http://neo4j.org/download
Business
http://neotechnology.com