Neo4j Nosqllive

38

description

Peter Neubauer presents Neo4j at NOSQL Live in Boston.

Transcript of Neo4j Nosqllive

Page 1: Neo4j Nosqllive
Page 2: Neo4j Nosqllive

No SQL?

Image credit: http://browsertoolkit.com/fault-tolerance.png

Page 3: Neo4j Nosqllive

Neo4jthe benefits of

graph databases

Peter NeubauerCOO, Neo Technology

#neo4j@[email protected]

Page 4: Neo4j Nosqllive

Why NoSQL 2009?

Trend 1: Size.

Trend 2: Connectivity.

Trend 3: Semi-structure.

Trend 4: Architecture.

Page 5: Neo4j Nosqllive

NOSQL data models

Bigtable clones

Key-value stores

Document databases

Graph databases

Data complexity

Dat

a si

ze

Page 6: Neo4j Nosqllive

NOSQL data models

Data complexity

Dat

a si

ze

Bigtable clones

Key-value stores

Document databases

90%of

usecases

(This is still bi l l i ons ofnodes & relationships)

Graph databases

Page 7: Neo4j Nosqllive

Graph DBs& Neo4j intro

Page 8: Neo4j Nosqllive

The Graph DB model: representationCore abstractions:

NodesRelationships between nodesProperties on both

name = “Emil”age = 29sex = “yes”

type = KNOWStime = 4 years

type = carvendor = “SAAB”model = “95 Aero”

11 22

33

Page 9: Neo4j Nosqllive

Example: The Matrix

name = “Thomas Anderson”age = 29

11

name = “The Architect”

4 24 2

CODED_BY

disclosure = public

name = “Cypher”last name = “Reagan”

disclosure = secretage = 6 months

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

331313

KNOWS KNOWS

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

age = 3 days

name = “Trinity”

77

22

KNOWS

KNOWS

KNO

WS

Page 10: Neo4j Nosqllive

Code (1): Building a node spaceGraphDatabaseService graphDb = ... // Get factory

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

// Create MorpheusNode morpheus = graphDb.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 11: Neo4j Nosqllive

Code (1): Building a node spaceGraphDatabaseService graphDb = ... // Get factoryTransaction tx = graphdb.beginTx();

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

// Create MorpheusNode morpheus = graphDb.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 similarlytx.commit();

Page 12: Neo4j Nosqllive

Code (1b): Defi ning RelationshipTypes// In package org.neo4j.graphdbpublic interface RelationshipType{ String name();}

// In package org.yourdomain.yourapp// Example on how to roll dynamic RelationshipTypesRelationshipsType myRelType = DynamicRelatoinshipType.

withName(“myRelType”);

// Example on how to kick it, static-RelationshipType-likeenum MyStaticRelTypes implements RelationshipType{ KNOWS, WORKS_FOR,}

Page 13: Neo4j Nosqllive

Whiteboard friendly

Björn Big Car

DayCare

Björn

owns

drivesbuild

Page 14: Neo4j Nosqllive
Page 15: Neo4j Nosqllive

The Graph DB model: traversalTraverser framework for high-performance traversing across the node space

name = “Emil”age = 31sex = “yes”

type = KNOWStime = 4 years

type = carvendor = “SAAB”model = “95 Aero”

11 22

33

Page 16: Neo4j Nosqllive

Example: Mr Anderson’s friends

name = “Thomas Anderson”age = 29

11

name = “The Architect”

4 24 2

CODED_BY

disclosure = public

name = “Cypher”last name = “Reagan”

disclosure = secretage = 6 months

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

331313

KNOWS KNOWS

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

age = 3 days

name = “Trinity”

77

22

KNOWS

KNOWS

KNO

WS

Page 17: Neo4j Nosqllive

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_GRAPH,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 18: Neo4j Nosqllive

$ 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_GRAPH, 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

331313

4 24 2

11KNOWS KNOWS CODED_BYKNOWS

KNOWSKN

OW

S

Page 19: Neo4j Nosqllive

Example: 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

331313

4 24 2

11KNOWS KNOWS CODED_BYKNOWS

KNOWS

KNO

WS

LOVES

Page 20: Neo4j Nosqllive

Code (3a): Custom traverser

// Create a traverser that returns all “friends in love”Traverser loveTraverser = mrAnderson.traverse(

Traverser.Order.BREADTH_FIRST,StopEvaluator.END_OF_GRAPH,new ReturnableEvaluator(){

public boolean isReturnableNode( TraversalPosition pos ){

return pos.currentNode().hasRelationship( RelTypes.LOVES, Direction.OUTGOING );

}},RelTypes.KNOWS,Direction.OUTGOING );

Page 21: Neo4j Nosqllive

Code (3a): Custom traverser

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

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

}

Page 22: Neo4j Nosqllive

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

$ bin/start-neo-exampleWho’s a lover?

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

331 31 3

4242

11KNOWS KNOWS CODED_BYKNOWS

KNOWSKN

OW

SLOVES

Page 23: Neo4j Nosqllive

Domain layer frameworksSpring (SpringDM, Grails, Griffon etc), Sling, KarafRails, DjangoQi4j (www.qi4j.org)

Framework for doing DDD in pure Java5Defi nes Entities / Associations / Properties

Sound familiar? Nodes / Rel’s / Properties!Neo4j is an “EntityStore” backend

Jo4neo (http://code.google.com/p/jo4neo)Annotation drivenWeaves Neo4j-backed persistence into domain objects at runtime

Page 24: Neo4j Nosqllive

Code (4): Groovy/Grails

import grails.plugins.neo4j.*@Neo4jEntityclass Author {

@Neo4jIndex String name}

Page 25: Neo4j Nosqllive

Result: Groovy/Grails

Page 26: Neo4j Nosqllive

Code (5): JRuby

require "rubygems"require "neo4j"

class Person include Neo4j::NodeMixin

property :name, :salary, :age, :country

has_n :friends

index :name, :salary, :age, :countryend

neo = Person.new :name => 'Neo'morpheus = Person.new :name => 'Morpheus'neo.friends << morpheus

Page 27: Neo4j Nosqllive

Neo4j system characteristicsDisk-based

Native graph storage engine with custom binary on-disk format

Robust6+ years in 24/7 production

Fast1-2 million nodes per second

TransactionalJTA/JTS, XA, 2PC, Tx recovery, deadlock detection, MVCC, etc

Scales up

Many bi l l i ons of nodes/rels/props on single JVM

Page 28: Neo4j Nosqllive

What do I use this stuff for?Modeling, productivity, stability

Bindings, migrations, robustnessFast graph algos on critical path

Shortest path (Spatial, GIS, Social Web, Routing)Fast traversals

Hierarchies, Networks (Automation, ERP, Intelligence, Finance)Recommendation engines (social, bioinfo)Multidimensional indexes (BI, Analytics, versioning)Activity Streams, Write Load -> Read LoadRDF, SAIL, SemWeb

Page 29: Neo4j Nosqllive

Query languagesSPARQL – “SQL for linked data”

Ex: ”SELECT ?person WHERE { ?person neo4j:KNOWS ?friend . ?friend neo4j:KNOWS ?foe . ?foe neo4j:name “Larry Ellison” . }”

Gremlin – “perl for graphs”Ex: ”./outE[@label='KNOWS']/inV[@age > 30]/@name”

Page 30: Neo4j Nosqllive

The Neo4j ecosystemNeo4j-kernel is an embedded database

Tiny teeny lil jar fi leComponent ecosystem

IndexRESTmeta-modelgraph-matching remote-graphdbsparql-engine...

See http://components.neo4j.org

Page 31: Neo4j Nosqllive

Neo4j-RDF triple/quad store

Example: Neo4j-RDF

Neo4j

RDFMetamodel Graph

match

SPARQLOWL

REST

Page 32: Neo4j Nosqllive

Language bindingsNeo4j.py – bindings for Jython and CPython

http://components.neo4j.org/neo4j.py

Neo4jrb – bindings for JRuby (incl RESTful API)http://wiki.neo4j.org/content/Ruby

Clojurehttp://wiki.neo4j.org/content/Clojure

Scala (incl RESTful API)http://wiki.neo4j.org/content/Scala

PHP

http://wiki.neo4j.org/content/PHPErlang, Compojure ...

Page 33: Neo4j Nosqllive

Scale out – replicationRolling out Neo4j HA... soon :)Master-slave replication, 1st configuration

MySQL style... ishExcept all instances can write, synchronously between writing slave & master (strong consistency)

Updates are asynchronously propagated to the other slaves (eventual consistency)

This can handle billions of entities...… but not 100B

Page 34: Neo4j Nosqllive

Scale out – partitioningSharding possible today

… but you have to do manual work… just as with MySQLGreat option: shard on top of resilient, scalable OSS app server , see: www.codecauldron.org

Transparent partitioning? Neo4j 2.0100B? Easy to say. Sliiiiightly harder to do.Fundamentals: BASE & eventual consistencyGeneric clustering algorithm as base case, but give lots of knobs for developers

Cross-shard edges->Node migration/replication->Partition schemes

Page 35: Neo4j Nosqllive

ConclusionGraphs && Neo4j => teh awesome!Available NOW under AGPLv3 / commercial license

AGPLv3: “if you’re open source, we’re open source”If you have proprietary software? Must buy a commercial licenseBut the first one is free!

Downloadhttp://neo4j.org

Feedbackhttp://lists.neo4j.org

Page 36: Neo4j Nosqllive

NoSQLSQL

Looking ahead: polyglot persistence

&&

Page 37: Neo4j Nosqllive

Questions?

Image credit: lost again! Sorry :(

Page 38: Neo4j Nosqllive

http://neotechnology.com