Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Beziehungsmanagement mit Spring Data Neo4j

Beziehungsmanagement mit Spring Data Neo4j

Overview of Spring Data Neo4j and the Neo4j Object Graph Mapper (Neo4j-OGM) with an introduction to Neo4j and how to import data from csv, json or jdbc.
Note: Everything but the title (slide) is in English.

Gerrit Meier

January 23, 2019
Tweet

More Decks by Gerrit Meier

Other Decks in Programming

Transcript

  1. Beziehungsmanagement
    mit Spring Data Neo4j
    Gerrit Meier @meistermeier

    View Slide

  2. • About Neo4j
    • Connect to Neo4j
    • Neo4j Object Graph Mapper
    • Spring Data Neo4j
    • Import Data
    • Demo Application
    Agenda

    View Slide

  3. About Neo4j

    View Slide

  4. • Neo4j is the #1 platform for connected
    data.
    • Neo4j powers the next generation of
    applications and analytics
    • Prominent use cases are found in areas
    like machine learning, personalized
    recommendations, fraud detection, data
    governance and more.
    Neo4j: The #1 Platform for Connected Data

    View Slide

  5. Ecosystem
    Neo4j Professional Services
    300+ partners
    47,000 group members
    61,000 trained engineers
    3.5M downloads
    Mindset
    “Graph Thinking” is all about
    considering connections in
    data as important as the data
    itself.
    Native Graph Platform
    Neo4j is an internet-scale,
    native graph database which
    executes connected workloads
    faster than any other database
    management system.
    Neo4j

    View Slide

  6. View Slide

  7. The Whiteboard Model Is the Physical Model
    Hero
    PART_OF
    Team
    FIGHTS
    Bad Person

    View Slide

  8. The Whiteboard Model Is the Physical Model
    Hero
    PART_OF
    Team
    FIGHTS
    Bad Person
    {realName: “Anthony Stark”}
    {event: “Infinity War”}

    View Slide

  9. The Whiteboard Model Is the Physical Model
    Iron Man
    :PART_OF
    Avengers
    :FIGHTS
    Thanos
    Groot
    :PART_OF
    Guardians
    of the
    Galaxy
    :FIGHTS

    View Slide

  10. Cypher
    Cypher Query Language is to Neo4j what 

    SQL is for relational databases.
    Open Source http://www.opencypher.org/ and ready for
    other Graph Database technology.

    View Slide

  11. MATCH 

    (t:Team)<-[p:PART_OF]-(h:Hero)-[f:FIGHTS]->(b:Bad_Person)
    WHERE b.name=“Thanos”
    RETURN t,p,h,f,b
    Cypher

    View Slide

  12. Cypher

    View Slide

  13. Connect to Neo4j

    View Slide

  14. Transport modes
    Bolt - Binary protocol
    Embedded - Running Neo4j in the same JVM
    HTTP - First remote access, no library needed

    View Slide

  15. Bolt drivers
    Languages - Python, Java, JavaScript, C#, Go
    Seabolt - connector written in C
    Community driver - R, Rust, Elixir…

    View Slide

  16. Neo4j Object Graph Mapper

    (Neo4j-OGM)

    View Slide

  17. Spring Data Neo4j

    View Slide

  18. {name: “Emil Eifrem”}
    Spring Data Neo4j

    View Slide

  19. {name: “Emil Eifrem”} {name: “Rod Johnson”}
    Spring Data Neo4j

    View Slide

  20. {name: “Emil Eifrem”} {name: “Rod Johnson”}
    Spring Data Neo4j

    View Slide

  21. Spring Data Neo4j / Neo4j-OGM

    View Slide

  22. Spring Data Neo4j / Neo4j-OGM

    View Slide

  23. Neo4j Object Graph Mapper

    (Neo4j-OGM)

    View Slide

  24. Java Driver
    Neo4j-OGM

    View Slide

  25. Java Driver
    Neo4j-OGM
    TransactionManger
    SessionFactory

    View Slide

  26. Java Driver
    Neo4j-OGM
    Embedded Bolt HTTP

    View Slide

  27. Neo4j-OGM
    Annotations - Describe your Graph
    Configuration - Driver, Domain packages,…
    Basic Data Access - CRUD operations built-in
    Mapping - Java Object Graph <> Graph Model

    View Slide

  28. Annotations
    :Hero
    :PART_OF
    :Team

    View Slide

  29. @NodeEntity(label = "Hero")
    public class Hero {
    @Id @GeneratedValue
    private Long id;
    @Property(name = "name")
    private String superheroName;
    private String realName;
    @Relationship(type = "PART_OF",
    direction = Relationship.OUTGOING)
    private PartOf team;
    }
    :PART_OF
    :Team

    View Slide

  30. @RelationshipEntity(type = "PART_OF")
    public class PartOf {
    @Id @GeneratedValue
    private Long id;
    @StartNode
    private Hero hero;
    @EndNode
    private Team team;
    private String role;
    }
    :Hero :Team

    View Slide

  31. @NodeEntity
    public class Team {
    @Id @GeneratedValue
    private Long id;
    private String name;
    }
    :PART_OF
    :Hero

    View Slide

  32. Configuration
    Database Access - Transport mode, connection pooling,
    encryption…
    Packages to scan - NodeEntities and RelationshipEntities
    Auto Indexing - Auto-generate indexes (development)

    View Slide

  33. Basic Data Access
    Session session = sessionFactory.openSession();
    Hero hero = new Hero();
    session.save(hero);

    View Slide

  34. Basic Data Access
    Hero hero = session.load(Hero.class, heroId);
    Collection heroes = session.loadAll(Hero.class);

    View Slide

  35. Basic Data Access
    session.delete(hero);
    session.deleteAll(Hero.class);

    View Slide

  36. Basic Data Access
    Result result = session.query("MATCH (h:Hero) return h", emptyMap());

    Hero hero = session.queryForObject(Hero.class,

    “MATCH (h:Hero) return h", emptyMap());

    View Slide

  37. Basic Data Access
    Hero hero = session.queryForObject(Hero.class,

    "MATCH (h:Hero) where h.name = ${name} return h",

    singletonMap("name", "John")
    );

    View Slide

  38. Spring Data Neo4j

    View Slide

  39. Spring Data Neo4j
    Convenient Data Access - Spring Data way
    Derived Finder Methods - no Cypher needed
    Abstraction from Cypher - if wanted
    Specialisation for Graph Operations - graph-ish way to
    work with data

    View Slide

  40. public interface HeroRepository extends Neo4jRepository {}
    Spring Data Neo4j
    CRUD Methods - save, findById, delete, count…
    Graph specific - depth “second” parameter

    View Slide

  41. Spring Data Neo4j
    public interface HeroRepository extends Neo4jRepository {
    Hero findByRealName(String realName);
    }

    View Slide

  42. Spring Data Neo4j
    public interface HeroRepository extends Neo4jRepository {
    Hero findByRealName(String realName);
    }
    MATCH (h:Hero) where h.realName=realName

    View Slide

  43. Spring Data Neo4j
    public interface HeroRepository extends Neo4jRepository {
    @Query("MATCH (h:Hero)-[:FIGHTS]->(b:Bad_Person) WHERE b.name={0} return h”)
    List findAllies(String opponent);
    }

    View Slide

  44. Spring Data Neo4j 5.x (Lovelace/Moore)
    Spring Boot 2.x (only)
    Schema-based Loading
    Persistence Constructor

    View Slide

  45. The Don’ts
    Complex Custom Cypher
    Batch Operations
    Domain Model does not represent the Graph

    View Slide

  46. Import Data

    View Slide

  47. Import
    LOAD CSV - File, HTTP(S) and FTP
    APOC - Support for JSON, JDBC…
    ETL Tool - Import and map data from SQL source

    View Slide

  48. "labels(n)","n.valid_until","n.country_codes","n.countries","n.node_id","n.sourceID","n.address","n.name","n.jurisdiction_description","n.service_provider","n.jurisdiction","n.closed_date","
    n.incorporation_date","n.ibcRUC","n.type","n.status","n.company_type","n.note"
    "[""Officer""]","Appleby","MUS","Mauritius","80113134","Paradise Papers - Appleby","","Lee Mo Lin - Noel Patrick L.C.K - Mauritius","","","","","","","","","",""
    "[""Officer""]","Appleby","SGP;USA","Singapore;United States","80113142","Paradise Papers - Appleby","","Pleshko - Daniel","","","","","","","","","",""
    "[""Officer""]","Appleby","BMU;GBR","Bermuda;United Kingdom","80113143","Paradise Papers - Appleby","","Plested - John","","","","","","","","","",""
    "[""Officer""]","Appleby","AUS;SGP","Australia;Singapore","80113145","Paradise Papers - Appleby","","Plewright - Peter John","","","","","","","","","",""
    "[""Officer""]","Appleby","CYM","Cayman Islands","80113146","Paradise Papers - Appleby","","Plexman - Eric Anthony","","","","","","","","","",""
    "[""Officer""]","Appleby","BMU;FRA","Bermuda;France","80113149","Paradise Papers - Appleby","","Plianthos - Nicolas Lawrence","","","","","","","","","",""
    "[""Officer""]","Appleby","USA","United States","80113152","Paradise Papers - Appleby","","Plimpton - Tara","","","","","","","","","",""
    "[""Officer""]","Appleby","USA","United States","80113153","Paradise Papers - Appleby","","Pliska - Bernard F.","","","","","","","","","",""
    "[""Officer""]","Appleby","USA","United States","80113157","Paradise Papers - Appleby","","Plotkin - Roger B.","","","","","","","","","",""
    "[""Officer""]","Appleby","","","80113158","Paradise Papers - Appleby","","Plott - Jeffrey","","","","","","","","","",""
    "[""Officer""]","Appleby","USA","United States","80113159","Paradise Papers - Appleby","","Plotz - Michael A.","","","","","","","","","",""
    "[""Officer""]","Appleby","","","80113160","Paradise Papers - Appleby","","Plouin - Graciela","","","","","","","","","",""
    "[""Officer""]","Appleby","GRC;GBR","Greece;United Kingdom","80113161","Paradise Papers - Appleby","","PLOUGHMAN - Dale","","","","","","","","","",""
    LOAD CSV

    View Slide

  49. LOAD CSV
    LOAD CSV WITH HEADERS FROM “file:paradise.csv” AS row
    CREATE (:Officer {name: row.n.name, country: row.n.country,…});

    View Slide

  50. APOC
    WITH “https://webservice/json” AS url
    CALL apoc.load.json(url) YIELD value
    UNWIND value.items AS item
    MERGE (:Item {title=item.title}

    View Slide

  51. APOC
    WITH “jdbc:mysql://database” AS url
    CALL apoc.load.jdbc(url) YIELD value
    UNWIND value.items AS item
    MERGE (:Item {title=item.title}

    View Slide

  52. ETL Tool

    View Slide

  53. Demo

    View Slide

  54. View Slide

  55. View Slide

  56. View Slide

  57. Bloom

    View Slide

  58. More Neo4j

    View Slide

  59. View Slide

  60. Thank you!

    View Slide

  61. Neo4j Desktop - https://neo4j.com/download/
    Neo4j Sandbox - https://neo4j.com/sandbox-v2/
    ICIJ Leaks Database - https://offshoreleaks.icij.org/pages/database
    Links

    View Slide