test driven data modeling with graphs
play

Test%Driven+Data+Modeling+With+Graphs + Twi7er:+@ianSrobinson+ - PowerPoint PPT Presentation

Test%Driven+Data+Modeling+With+Graphs + Twi7er:+@ianSrobinson+ #neo4j+ + Outline+ Data+modeling+with+graphs+ Neo4j+applicaDon+architecture+opDons+ TesDng+your+data+model++ Graph+data+modeling+ Labeled+Property+Graph+ Models+


  1. Test%Driven+Data+Modeling+With+Graphs + Twi7er:+@ianSrobinson+ #neo4j+ +

  2. Outline+ • Data+modeling+with+graphs+ • Neo4j+applicaDon+architecture+opDons+ • TesDng+your+data+model++

  3. Graph+data+modeling+

  4. Labeled+Property+Graph+

  5. Models+ Purposeful+abstracDon+of+a+domain+designed+to+ saDsfy+parDcular+applicaDon/end%user+goals+ Images:+en.wikipedia.org+

  6. Example+ApplicaDon+ • Knowledge+management+ – People,+companies,+skills+ – Cross+organizaDonal+ • Find+my+professional+social+network+ – Exchange+knowledge+ – Interest+groups+ – Help+ – Staff+projects+

  7. ApplicaDon/End%User+Goals+ As#an# employee' ' I#want# to'know'who'in'the'company' has'similar'skills'to'me' ' So#that# we'can'exchange'knowledge'

  8. QuesDons+To+Ask+of+the+Domain+ As#an# employee' ' I#want# to'know'who'in'the'company' has'similar'skills'to'me' ' So#that# we'can'exchange'knowledge' Which+people,+who+work+for+the+same+company+ as+me,+have+similar+skills+to+me?+

  9. IdenDfy+EnDDes+ Which+people,+who+work+for+the+same+company+ as+me,+have+similar+skills+to+me?+ + Person+ Company+ Skill+

  10. IdenDfy+RelaDonships+Between+EnDDes+ Which+people,+who+work+for+the+same+company+ as+me,+have+similar+skills+to+me?+ + Person+WORKS_FOR+Company+ Person+HAS_SKILL+Skill+

  11. Convert+to+Cypher+Paths+ RelaDonship+ Person+WORKS_FOR+Company+ Person+HAS_SKILL+Skill+ Label+ (:Person)-[:WORKS_FOR]->(:Company), (:Person)-[:HAS_SKILL]->(:Skill)

  12. Consolidate+Paths+ (:Person)-[:WORKS_FOR]->(:Company), (:Person)-[:HAS_SKILL]->(:Skill) (:Company)<-[:WORKS_FOR]-(:Person)-[:HAS_SKILL]->(:Skill)

  13. Candidate+Data+Model+ (:Company)<-[:WORKS_FOR]-(:Person)-[:HAS_SKILL]->(:Skill)

  14. Express+QuesDon+as+Graph+Pa7ern+ Which+people,+who+work+for+the+same+company+ as+me,+have+similar+skills+to+me?+

  15. Cypher+Query+ Which+people,+who+work+for+the+same+company+ as+me,+have+similar+skills+to+me?+ MATCH (company)<-[:WORKS_FOR]-(me:Person)-[:HAS_SKILL]->(skill), (company)<-[:WORKS_FOR]-(colleague)-[:HAS_SKILL]->(skill) WHERE me.name = {name} RETURN colleague.name AS name, count(skill) AS score, collect(skill.name) AS skills ORDER BY score DESC

  16. Graph+Pa7ern+ Which+people,+who+work+for+the+same+company+ as+me,+have+similar+skills+to+me?+ MATCH (company)<-[:WORKS_FOR]-( MATCH (company)<-[:WORKS_FOR]-(me:Person me:Person)-[:HAS_SKILL]->(skill), )-[:HAS_SKILL]->(skill), (company)<-[:WORKS_FOR]-(colleague)-[:HAS_SKILL]->(skill) (company)<-[:WORKS_FOR]-(colleague)-[:HAS_SKILL]->(skill) WHERE me.name = {name} RETURN colleague.name AS name, count(skill) AS score, collect(skill.name) AS skills ORDER BY score DESC

  17. Anchor+Pa7ern+in+Graph+ Which+people,+who+work+for+the+same+company+ as+me,+have+similar+skills+to+me?+ MATCH (company)<-[:WORKS_FOR]-(me:Person me:Person)-[:HAS_SKILL]->(skill), (company)<-[:WORKS_FOR]-(colleague)-[:HAS_SKILL]->(skill) WHERE WHERE me.name me.name = {name} = {name} RETURN colleague.name AS name, count(skill) AS score, collect(skill.name) AS skills ORDER BY score DESC Search+nodes+labeled+ ‘Person’,+matching+on+ ‘name’+property+

  18. Create+ProjecDon+of+Results+ Which+people,+who+work+for+the+same+company+ as+me,+have+similar+skills+to+me?+ MATCH (company)<-[:WORKS_FOR]-(me:Person)-[:HAS_SKILL]->(skill), (company)<-[:WORKS_FOR]-(colleague)-[:HAS_SKILL]->(skill) WHERE me.name = {name} RETURN RETURN colleague.name colleague.name AS name, AS name, count(skill) AS score, count(skill) AS score, collect( collect(skill.name skill.name) AS skills ) AS skills ORDER BY score DESC ORDER BY score DESC

  19. First+Match+

  20. Second+Match+

  21. Third+Match+

  22. Running+the+Query+ +-----------------------------------+ | name | score | skills | +-----------------------------------+ | "Lucy" | 2 | ["Java","Neo4j"] | | "Bill" | 1 | ["Neo4j"] | +-----------------------------------+ 2 rows

  23. From+User+Story+to+Model+and+Query+ As#an# employee' MATCH (company)<-[:WORKS_FOR]-(me:Person)-[:HAS_SKILL]->(skill), ' (company)<-[:WORKS_FOR]-(colleague)-[:HAS_SKILL]->(skill) I#want# to'know'who'in'the'company' WHERE me.name = {name} has'similar'skills'to'me' RETURN colleague.name AS name, ' count(skill) AS score, So#that# we'can'exchange'knowledge' collect(skill.name) AS skills ORDER BY score DESC ? Which#people,#who#work#for#the#same# company#as#me,#have#similar#skills#to#me? Person+WORKS_FOR+Company+ Person+HAS_SKILL+Skill (:Company)<-[:WORKS_FOR]-(:Person)-[:HAS_SKILL]->(:Skill)

  24. TesDng+

  25. Why+Test?+ • Ensure+model+is+fit+for+queries+ – Rapid+feedback+ • Ensure+correctness+of+queries+ • Document+your+understanding+of+your+domain+ – Including+corner+cases+and+excepDons+ • Provide+a+regression+test+suite+ – Allows+you+to+change+and+evolve+model+and+ queries+

  26. Method+ • Develop+queries,+or+classes+that+encapsulate+ queries,+using+ unit'tests' • Use+ small,'well7understood' datasets+in+each+test+ – Create+data+in+test+setup+ – Test+dataset+expresses+your+understanding+of+(part+of)+ the+domain+ • Inject+ in7memory'graph'database' (or+Cypher+ engine)+into+object+under+test+ • The+exact+strategy+you+use+depends+on+your+ applicaDon+architecture…+

  27. ApplicaDon+Architectures+ • Embedded+ • Server+ • Server+with+Extensions+

  28. ApplicaDon+Architectures+ • Embedded+ ApplicaDon+ – Host+in+Java+process+ Java+APIs+ – Access+to+Java+APIs+ • Server+ • Server+with+Extensions+

  29. ApplicaDon+Architectures+ • Embedded+ ApplicaDon+ REST+Client+ • Server+ Write+LB+ Read+LB+ – HTTP/JSON+interface+ – Server+wraps+embedded+ REST+API+ REST+API+ REST+API+ instance+ • Server+with+Extensions+

  30. ApplicaDon+Architectures+ • Embedded+ REST+API+ Extensions+ • Server+ • Server+with+Extensions+ – Execute+complex+logic+on+server+ – Control+HTTP+request/response+format+

  31. Embedded+Example+ • Company+social+network+ • Find+colleagues+with+similar+skills+ • Encapsulate+query+in+a+ ColleagueFinder

  32. Unit+Test+Fixture+ public class ColleagueFinderTest { private GraphDatabaseService db; private ColleagueFinder finder; @Before public void init() { db = new TestGraphDatabaseFactory().newImpermanentDatabase(); ExampleGraph.populate( db ); finder = new ColleagueFinder( new ExecutionEngine( db ) ); } @After public void shutdown() { db.shutdown(); } }

  33. Create+Database+ public class ColleagueFinderTest { private private GraphDatabaseService GraphDatabaseService db db; private ColleagueFinder finder; @Before public void init() { db db = new = new TestGraphDatabaseFactory TestGraphDatabaseFactory(). ().newImpermanentDatabase newImpermanentDatabase(); (); ExampleGraph.populate( db ); finder = new ColleagueFinder( new ExecutionEngine( db ) ); } @After public void shutdown() { db.shutdown(); } }

  34. Populate+Graph+ public class ColleagueFinderTest { private GraphDatabaseService db; private ColleagueFinder finder; @Before public void init() { db = new TestGraphDatabaseFactory().newImpermanentDatabase(); ExampleGraph.populate ExampleGraph.populate( ( db db ); ); finder = new ColleagueFinder( new ExecutionEngine( db ) ); } @After public void shutdown() { db.shutdown(); } }

  35. Create+Object+Under+Test+ public class ColleagueFinderTest { private GraphDatabaseService db; private private ColleagueFinder ColleagueFinder finder; finder; @Before public void init() { db = new TestGraphDatabaseFactory().newImpermanentDatabase(); ExampleGraph.populate( db ); finder = new ColleagueFinder finder = new ColleagueFinder( new ( new ExecutionEngine ExecutionEngine( ( db db ) ); ) ); } Inject++ @After ExecuDonEngine+ public void shutdown() { db.shutdown(); } }

  36. ImpermanentGraphDatabase+ • In%memory+ • For+tesDng+only,+not+producDon!+ <dependency> <groupId>org.neo4j</groupId> <artifactId>neo4j-kernel</artifactId> <version>${project.version}</version> <type>test-jar</type> <scope>test</scope> </dependency>

  37. Create+Sample+Data+ public static void populate( GraphDatabaseService db ) { ExecutionEngine engine = new ExecutionEngine( db ); String cypher = "CREATE ian:Person VALUES {name:'Ian'},\n" + " bill:Person VALUES {name:'Bill'},\n" + " lucy:Person VALUES {name:'Lucy'},\n" + " acme:Company VALUES {name:'Acme'},\n" + // Cypher continues... " (bill)-[:HAS_SKILL]->(neo4j),\n" + " (bill)-[:HAS_SKILL]->(ruby),\n" + " (lucy)-[:HAS_SKILL]->(java),\n" + " (lucy)-[:HAS_SKILL]->(neo4j)"; engine.execute( cypher ); }

  38. Unit+Test+ @Test public void shouldFindColleaguesWithSimilarSkills() throws Exception { // when Iterator<Map<String, Object>> results = finder.findColleaguesFor( "Ian" ); // then assertEquals( "Lucy", results.next().get( "name" ) ); assertEquals( "Bill", results.next().get( "name" ) ); assertFalse( results.hasNext() ); }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend