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

What the Graph?

What the Graph?

Neo4j and Spring Data Neo4j presentation given at #springio_19

Gerrit Meier

May 16, 2019
Tweet

More Decks by Gerrit Meier

Other Decks in Programming

Transcript

  1. Gerrit Meier
    @meistermeier
    What the Graph?

    View full-size slide

  2. 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 full-size slide

  3. Graph Databases

    View full-size slide

  4. Graph Databases
    Hero

    View full-size slide

  5. Graph Databases
    Label
    Type

    View full-size slide

  6. Graph Databases
    Person
    KNOWS
    {

    name: “Mr. X”
    dob: 26.02.1982

    }
    {

    met: “Spring IO”
    contact: 16.05.2019

    }

    View full-size slide

  7. Graph Databases
    Person
    KNOWS
    Person
    Topic
    INTERESTED_IN INTERESTED_IN
    {

    term: “Spring Data”
    description: “ … “

    }

    View full-size slide

  8. 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 full-size slide

  9. Cypher
    :Person
    :KNOWS
    :Person
    :Topic
    :INTERESTED_IN :INTERESTED_IN

    View full-size slide

  10. Cypher
    Person
    KNOWS
    Person
    Topic
    INTERESTED_IN INTERESTED_IN
    MATCH 

    (person:Person)-[:INTERESTED_IN]"->(t:Topic)"<-[:INTERESTED_IN]-(otherPerson:Person)
    WHERE 

    (person)-[:KNOWS]-(otherPerson)

    RETURN

    otherPerson

    View full-size slide

  11. Connect to Neo4j
    •Bolt - Binary protocol
    •HTTP - First remote access, no library needed
    •Embedded - Running Neo4j in the same JVM

    View full-size slide

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

    View full-size slide

  13. Neo4j-OGM
    Java Driver
    Neo4j-OGM
    Embedded Bolt HTTP

    View full-size slide

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

    View full-size slide

  15. Neo4j-OGM
    :Person :Topic
    :INTERESTED_IN

    View full-size slide

  16. Neo4j-OGM
    :Topic
    :INTERESTED_IN
    @NodeEntity
    public class Person {
    @Id @GeneratedValue
    private Long id;
    @Property
    private String name;
    @Relationship(type = “INTERESTED_IN")
    private Set topic;
    }

    View full-size slide

  17. Neo4j-OGM
    :INTERESTED_IN
    @NodeEntity
    public class Topic {
    @Id @GeneratedValue
    private Long id;
    private String term;
    private String description;
    }
    :Person

    View full-size slide

  18. Neo4j-OGM
    :Person :Topic
    :INTERESTED_IN
    Session session = sessionFactory.openSession();
    Person person = new Person(“Mr. X”);
    person.addTopic(new Topic(“Spring”));
    session.save(person);

    View full-size slide

  19. Neo4j-OGM
    :Person :Topic
    :INTERESTED_IN
    Person person = session.load(Person.class, personId);
    Collection people = session.loadAll(Person.class);

    View full-size slide

  20. Neo4j-OGM
    :Person :Topic
    :INTERESTED_IN
    Person person = session.queryForObject(Person.class,

    "MATCH (p:Person) where p.name = ${name} return p",

    singletonMap("name", “Mr. X”)
    );

    View full-size slide

  21. Spring Data Neo4j

    View full-size slide

  22. Spring Data Neo4j
    •Convenient Data Access - Spring Data’s Domain-Driver-Design way
    •Derived Finder Methods - no Cypher needed
    •Abstraction from Cypher - if wanted
    •Specialisation for Graph Operations - graph-ish way to work with data

    View full-size slide

  23. Spring Data Neo4j
    •CRUD Methods - save, findById, delete, count…
    •Graph specific - depth as “second” parameter
    interface PersonRepository extends Neo4jRepository {}

    View full-size slide

  24. Spring Data Neo4j
    interface PersonRepository extends Neo4jRepository {
    Person findByName(String name);
    }

    View full-size slide

  25. Spring Data Neo4j
    interface PersonRepository extends Neo4jRepository {
    Person findByName(String name);
    }
    MATCH 

    (person:Person)-[int:INTERESTED_IN]"->(topic:Topic)
    WHERE 

    person.name=$name 

    RETURN

    person, int, topic

    View full-size slide

  26. Spring Data Neo4j
    interface PersonRepository extends Neo4jRepository {
    @Query("MATCH (p:Person)-[:INTERESTED_IN]->(t:Topic) WHERE t.term={0} return p”)
    List customFindByInterest(String interest);
    }

    View full-size slide

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

    View full-size slide

  28. The Don’ts

    View full-size slide

  29. The Don’ts
    •Complex Custom Cypher
    •Batch Operations
    •Domain Model does not represent the Graph
    •Unwrapping the Session / Driver all the time

    View full-size slide

  30. Getting started

    View full-size slide

  31. Import
    •LOAD CSV - File, HTTP(S) or FTP
    •APOC - Support for JSON, JDBC…
    •ETL Tool - Import and map data from SQL source
    WITH “jdbc:mysql://database” AS url
    CALL apoc.load.jdbc(url) YIELD value
    UNWIND value.items AS item
    MERGE (:Item {title=item.title}
    LOAD CSV WITH HEADERS 

    FROM “file:paradise.csv” AS row
    CREATE 

    (:Officer {name: row.n.name, country: row.n.country,…

    View full-size slide

  32. Spring Data Neo4j ⚡ RX*
    *working title

    View full-size slide

  33. SDN⚡RX
    •Complete rewrite of Spring Data Neo4j & Neo4j-OGM
    •(not only) Reactive Support
    •(Reactive)Neo4jClient
    •Cypher DSL
    •real Immutable Mapping
    •new Spring Boot Autoconfiguration

    View full-size slide

  34. SDN⚡RX
    Node personNode = node("Person");
    Statement statement =
    match(personNode)
    .where(personNode.property("name").isEqualTo(literalOf("Mr.X")))
    .returning(personNode)
    .build();

    View full-size slide

  35. SDN⚡RX
    Node personNode = node("Person");
    Statement statement =
    match(personNode)
    .where(personNode.property("name").isEqualTo(literalOf("Mr.X")))
    .returning(personNode)
    .build();
    ReactiveNeo4jClient client = ReactiveNeo4jClient.create(driver);
    Flux people = client
    .newQuery(renderer.render(statement))
    .fetchAs(Person.class)
    .mappedBy(record !-> {!/*!..!*/}) !// optional mapping
    .all()
    .subscribe(person !-> {!/*!..!*/});

    View full-size slide

  36. SDN⚡RX
    public class Person {
    @Id
    private final Long id;
    private final String name;
    private Person(Long id, String name) {
    this.id = id;
    this.name = name;
    }
    public Person withId(Long newId) {
    return new Person(newId, this.name);
    }
    public Person withName(String newName) {
    return new Person(this.id, newName);
    }
    }
    @AllArgsConstructor
    public class PersonWithAllConstructor {
    @Id
    private final Long id;
    private final String name;
    private final String firstName;
    private final String sameValue;
    private final Boolean cool;
    private final Long personNumber;
    private final LocalDate bornOn;
    }

    View full-size slide

  37. SDN⚡RX
    @Node
    data class KotlinPerson(@Id val id: Long, val name: String)

    View full-size slide

  38. Links
    •Neo4j Desktop - https://neo4j.com/download
    •Neo4j Sandbox - https://neo4j.com/sandbox-v2/
    •This Talk - https://speakerdeck.com/meistermeier
    •Free Graph Algorithms book - https://neo4j.com/graph-algorithms-book

    View full-size slide

  39. Gerrit Meier
    @meistermeier
    Thanks!

    View full-size slide