16

Click here to load reader

Basic Neo4j Code Examples 2008 05 08

Embed Size (px)

DESCRIPTION

Neo4J Graph Database presentation, Daren Dewjew, BarcampSaigon 2009

Citation preview

Page 1: Basic Neo4j Code Examples 2008 05 08

Neo

Emil Eifrem2008-05-08, API v1.0-rc1-SNAPSHOT

some code snippets

Page 2: Basic Neo4j Code Examples 2008 05 08

A few brief Neo4j code slidesThe 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

Page 3: Basic Neo4j Code Examples 2008 05 08

Example: The Matrix social graph

name = “Thomas Anderson”age = 29

11

name = “The Architect”

4242

CODED_BY

disclosure = public

name = “Cypher”last name = “Reagan”

disclosure = secretage = 6 months

name = “Agent Smith”version = 1.0blanguage = C++

33

1313

KNOWS KNOWS

name = “Morpheus”rank = “Captain”occupation = “Total badass”

age = 3 days

name = “Trinity”

77

22

KNOWS

KNOWS

KN

OW

S

Page 4: Basic Neo4j Code Examples 2008 05 08

Code (1): Building a node spaceNeoService neo = ... // Get factory

// Create Thomas 'Neo' AndersonNode mrAnderson = neo.createNode();mrAnderson.setProperty( "name", "Thomas Anderson" );mrAnderson.setProperty( "age", 29 );

// Create MorpheusNode 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 othermrAnderson.createRelationshipTo( morpheus, RelTypes.KNOWS );// ...create Trinity, Cypher, Agent Smith, Architect similarly

Page 5: Basic Neo4j Code Examples 2008 05 08

Code (1): Building a node spaceNeoService neo = ... // Get factoryTransaction tx = neo.beginTransaction();

// Create Thomas 'Neo' AndersonNode mrAnderson = neo.createNode();mrAnderson.setProperty( "name", "Thomas Anderson" );mrAnderson.setProperty( "age", 29 );

// Create MorpheusNode 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 othermrAnderson.createRelationshipTo( morpheus, RelTypes.KNOWS );// ...create Trinity, Cypher, Agent Smith, Architect similarly

tx.commit(); // Pseudo code, obviously wrap it in try-finally

Page 6: Basic Neo4j Code Examples 2008 05 08

Traversal: Find Mr Anderson’s friends

name = “Thomas Anderson”age = 29

11

name = “The Architect”

4242

CODED_BY

disclosure = public

name = “Cypher”last name = “Reagan”

disclosure = secretage = 6 months

name = “Agent Smith”version = 1.0blanguage = C++

33

1313

KNOWS KNOWS

name = “Morpheus”rank = “Captain”occupation = “Total badass”

age = 3 days

name = “Trinity”

77

22

KNOWS

KNOWS

KN

OW

S

Page 7: Basic Neo4j Code Examples 2008 05 08

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

Page 8: Basic Neo4j Code Examples 2008 05 08

Code (2): Traversing a node space

// Instantiate a traverser that returns Mr Anderson's friendsTraverser 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 resultSystem.out.println( "Mr Anderson's friends:" );for ( Node friend : friendsTraverser ){

System.out.printf( "At depth %d => %s%n",friendsTraverser.currentPosition().getDepth(),friend.getProperty( "name" ) );

}

Page 9: Basic Neo4j Code Examples 2008 05 08

$ bin/start-neo-exampleMr Anderson's friends:

At depth 1 => MorpheusAt depth 1 => TrinityAt depth 2 => CypherAt depth 3 => Agent Smith$

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

name = “Thomas Anderson”age = 29

name = “Morpheus”rank = “Captain”occupation = “Total badass”

name = “The Architect”

disclosure = public

age = 3 days

name = “Trinity”

name = “Cypher”last name = “Reagan”

disclosure = secretage = 6 months

name = “Agent Smith”version = 1.0blanguage = C++

77

22

33

1313

4242

11KNOWS KNOWS CODED_BYKNOWS

KNOWSK

NO

WS

Page 10: Basic Neo4j Code Examples 2008 05 08

Evolving the domain: Friends in love?

name = “Thomas Anderson”age = 29

name = “Morpheus”rank = “Captain”occupation = “Total badass”

name = “The Architect”

disclosure = public

name = “Trinity”

name = “Cypher”last name = “Reagan”

disclosure = secretage = 6 months

name = “Agent Smith”version = 1.0blanguage = C++

77

22

33

1313

4242

11KNOWS KNOWS CODED_BYKNOWS

KNOWS

KN

OW

S

LOVES

Page 11: Basic Neo4j Code Examples 2008 05 08

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

Page 12: Basic Neo4j Code Examples 2008 05 08

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 );

Page 13: Basic Neo4j Code Examples 2008 05 08

Code (3a): Custom traverser

// Traverse the node space and print out the resultSystem.out.println( "Who’s in love?" );for ( Node person : loveTraverser ){

System.out.printf( "At depth %d => %s%n",loveTraverser.currentPosition().getDepth(),person.getProperty( "name" ) );

}

Page 14: Basic Neo4j Code Examples 2008 05 08

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

$ bin/start-neo-exampleWho’s in love?

At depth 1 => Trinity$

name = “Thomas Anderson”age = 29

name = “Morpheus”rank = “Captain”occupation = “Total badass”

name = “The Architect”

disclosure = public

name = “Trinity”

name = “Cypher”last name = “Reagan”

disclosure = secretage = 6 months

name = “Agent Smith”version = 1.0blanguage = C++

77

22

33

1313

4242

11KNOWS

KNOWS

CODED_BYKNOWS

KNOWS

KN

OW

SLOVES

Page 15: Basic Neo4j Code Examples 2008 05 08

SummaryAPI details

http://api.neo4j.org

Feedback

http://lists.neo4j.org

Download

http://neo4j.org/download

Business

http://neotechnology.com

Page 16: Basic Neo4j Code Examples 2008 05 08

www.neo4j.org