320 likes | 446 Views
Square Pegs and Round Holes on the NOSQL World. Jim Webber Chief Scientist, Neo Technology @ jimwebber. Koans A free tutorial that gets you up to speed with Neo4j through hacking. http:// bit.ly /neo4j-koan https:// github.com / jimwebber /neo4j-tutorial. N ot O nly S QL.
E N D
Square Pegs and Round Holes on the NOSQL World Jim Webber Chief Scientist, Neo Technology @jimwebber
KoansA free tutorial that gets you up to speed with Neo4j through hacking http://bit.ly/neo4j-koan https://github.com/jimwebber/neo4j-tutorial
Not OnlySQL Remember that NOSQL means
http://www.xtranormal.com/watch/6995033/mongo-db-is-web-scalehttp://www.xtranormal.com/watch/6995033/mongo-db-is-web-scale
http://www.orangesmile.com/destinations/img/berlin-map-metro-big.gifhttp://www.orangesmile.com/destinations/img/berlin-map-metro-big.gif
Creating Nodes GraphDatabaseServicedb = new EmbeddedGraphDatabase("/tmp/neo"); Transaction tx = db.beginTx(); try { Node theDoctor = db.createNode(); theDoctor.setProperty("name", "the Doctor"); tx.success(); } finally { tx.finish(); }
Creating Relationships Transaction tx = db.beginTx(); try { Node theDoctor= db.createNode(); theDoctor.setProperty("name", "The Doctor"); Node susan= db.createNode(); susan.setProperty("firstname", "Susan"); susan.setProperty("lastname", "Campbell"); susan.createRelationshipTo(theDoctor, DynamicRelationshipType.withName("COMPANION_OF")); tx.success(); } finally { tx.finish(); }
http://easystreetdiscount.auctivacommerce.com/Nirvana-Smiley-Face-Music-Band-Decal-Sticker-P188162.aspxhttp://easystreetdiscount.auctivacommerce.com/Nirvana-Smiley-Face-Music-Band-Decal-Sticker-P188162.aspx
http://www.tolkienlibrary.com/press/922-Isildur_Poker_Champion.phphttp://www.tolkienlibrary.com/press/922-Isildur_Poker_Champion.php
http://www.vaccinetimes.com/wp-content/uploads/2010/12/microscope.jpghttp://www.vaccinetimes.com/wp-content/uploads/2010/12/microscope.jpg
Document Database username: Jeff1986 age: 25 friend : SallyDJ friend : Gazza username: SallyDJ age: 28 friend : Jeff1986 friend: FunkySam username: FunkySam age: 24 friend : SallyDJ username: Gazza age: 32 friend : Jeff1986
Application Layer username: SallyDJ age: 28 username: Jeff1986 age: 25 username: FunkySam age: 24 username: Gazza age: 32 Reify Document Database username: Jeff1986 age: 25 friend : SallyDJ friend : Gazza username: SallyDJ age: 28 friend : Jeff1986 friend: FunkySam username: FunkySam age: 24 friend : SallyDJ username: Gazza age: 32 friend : Jeff1986
Graph Database username: SallyDJ age: 28 FRIEND FRIEND username: Jeff1986 age: 25 username: FunkySam age: 24 FRIEND username: Gazza age: 32
Graph Database username: Gazza age: 32 username: SallyDJ age: 28 PURCHASED PURCHASED PURCHASED product: SuperCans manufacturer : Acme price : 150 product: CoolDecks manufacturer : Acme price : 599 Document Database product: SuperCans manufacturer : Acme price : 150 product: CoolDecks manufacturer : Acme price : 599 username: SallyDJ age: 28 purchased : CoolDecks purchased : SuperCans username: Gazza age: 32 purchased : SuperCans
Graph Database username: Jeff1986 age: 25 username: FunkySam age: 24 FRIEND username: Gazza age: 32 FRIEND FRIEND username: SallyDJ age: 28 PURCHASED PURCHASED PURCHASED product: SuperCans manufacturer : Acme price : 150 product: CoolDecks manufacturer : Acme price : 599
Graph Algorithms What’s the shortest path between the Doctor and the Master? Node theMaster = … Node theDoctor = … intmaxDepth = 5; PathFinder<Path> shortestPathFinder = GraphAlgoFactory.shortestPath(Traversal.expanderForAllTypes(), maxDepth); Path shortestPath= shortestPathFinder.findSinglePath(theDoctor, theMaster);
Path finding Find all the episodes where Rose Tyler fought the Daleks
Path finder code algo Node rose = ... Node daleks = ... PathFinder<Path> pathFinder = GraphAlgoFactory.pathsWithLength(Traversal.expanderForTypes(DoctorWhoUniverse.APPEARED_IN, Direction.BOTH), 2); Iterable<Path> paths = pathFinder.findAllPaths(rose, daleks); constraints fixed path length
!PURCHASED product: CoolDecks manufacturer : Acme age: < 40 PURCHASED product: SuperCans manufacturer : Acme
Why graph matching? • It’s super-powerful for looking for patterns in a data set • TW has done retail analytics PoCs (and hopefully projects) with this stuff • Higher-level abstraction than raw traversers • Uses PatternNode and PatternRelationship types to describe graph patterns • The “unbound” parts of the graph
Setting up and matching a pattern final PatternNodetheDoctor = new PatternNode(); theDoctor.setAssociation(universe.theDoctor()); final PatternNodeanEpisode = new PatternNode(); anEpisode.addPropertyConstraint("title", CommonValueMatchers.has()); anEpisode.addPropertyConstraint("episode", CommonValueMatchers.has()); final PatternNodeaDoctorActor = new PatternNode(); aDoctorActor.createRelationshipTo(theDoctor, DoctorWhoUniverse.PLAYED); aDoctorActor.createRelationshipTo(anEpisode, DoctorWhoUniverse.APPEARED_IN); aDoctorActor.addPropertyConstraint("actor", CommonValueMatchers.has()); final PatternNodetheCybermen = new PatternNode(); theCybermen.setAssociation(universe.speciesIndex.get("species", "Cyberman").getSingle()); theCybermen.createRelationshipTo(anEpisode, DoctorWhoUniverse.APPEARED_IN); theCybermen.createRelationshipTo(theDoctor, DoctorWhoUniverse.ENEMY_OF); PatternMatchermatcher = PatternMatcher.getMatcher(); final Iterable<PatternMatch> matches = matcher.match(theDoctor, universe.theDoctor());
http://www.561studios.com/blog/wp-content/uploads/2010/07/commonsense.jpghttp://www.561studios.com/blog/wp-content/uploads/2010/07/commonsense.jpg
Questions? Community: http://neo4j.org Koans: https://github.com/jimwebber/neo4j-tutorial Me: @jimwebber, jim@neotechnology.com