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

Cypher Sleuthing: Tips and Tricks for Querying a Graph

Cypher Sleuthing: Tips and Tricks for Querying a Graph

While many messages tell us graphs are easier to understand, writing and structuring graph queries is often different than our current skills. Join us as we dive into the structure of a graph database and understand how queries operate. We will cover tips and tricks, as well as pitfalls, to help us avoid future problems. See how to navigate graph data with confidence to retrieve accurate and performant results!

Jennifer Reif

February 24, 2022
Tweet

More Decks by Jennifer Reif

Other Decks in Technology

Transcript

  1. Jennifer Reif
    Email: [email protected]
    Twitter: @JMHReif
    LinkedIn: linkedin.com/in/jmhreif
    Github: GitHub.com/JMHReif
    Website: jmhreif.com
    Cypher Sleuthing:
    Tips and Tricks for Querying a Graph

    View Slide

  2. Who Am I?
    • Developer, Advocate

    • Continuous learner

    • Conference speaker

    • Blogger

    • Geek
    Jennifer Reif
    Email: [email protected]
    Twitter: @JMHReif
    LinkedIn: linkedin.com/in/jmhreif
    Github: GitHub.com/JMHReif
    Website: jmhreif.com

    View Slide

  3. What is a graph?

    View Slide

  4. Data storage with relationships!
    • Stores relationships with entities

    • Faster read queries

    • Easily connect multiple entities together

    • Mimic real-world data organization

    View Slide

  5. Pick a domain
    Event/Conference
    Event
    Speakers
    Sessions/
    Trainings
    Venue
    Registration/
    Attendee
    Sponsors
    Promotion

    View Slide

  6. Solution: why graph?
    • Understand -> by relating known to unknown

    • Connect -> disparate domains

    • Mirror -> real-life domain

    • Increase -> business agility

    • Reduce -> complexity

    • Address -> shortcomings in existing models
    Photo by Javier Ezpeleta on Unsplash

    View Slide

  7. Disclaimer!
    • Graph is not a silver bullet

    • Some use cases do not
    fi
    t

    • That’s a separate session!
    Photo by Julius Drost on Unsplash

    View Slide

  8. Learn FAST!
    And break things?

    View Slide

  9. Tackling a solution…
    • Trial-and-error

    • At the mercy of the language

    • Whack-a-mole
    fi
    xes

    • What if…. Photo by Michal Vrba on Unsplash

    View Slide

  10. Understanding is the key
    • How to wield the tool brings mastery

    • Knowing when and how —>

    • E
    ffi
    ciency

    • Better results
    Credit: unsplash.com

    View Slide

  11. Cypher query language

    View Slide

  12. • Graph query language

    • Created by Neo4j ~10yrs ago

    • Open sourced

    • Widely supported

    • Part of GQL standardization

    • opencypher.org
    What is Cypher?
    Photo by Eugene Chystiakov on Unsplash

    View Slide

  13. • Follow path of connected data

    • Help understand data patterns

    • Surface hidden connections
    What does it do?
    Photo by ConvertKit on Unsplash

    View Slide

  14. Quick lesson on Cypher syntax

    View Slide

  15. Functional and visual
    • Designed to store and retrieve data in a graph

    • Based on ASCII-art

    • Declarative query language

    • Focus on what to retrieve, not how
    A B
    LIKES
    MATCH ( A ) - [ : LIKES] - > ( B )

    View Slide

  16. Cypher in 20 seconds…
    Node
    (var:Label)
    (var:Label {key: value})
    -[var:REL_TYPE]->
    HAS_TYPE
    -[var:REL_TYPE {key: value}]->
    (n1:Label)
    Node
    -[r1:REL_TYPE]->
    HAS_TYPE
    (n2:Label)
    Node
    (n3:Label)
    Node
    -[r2:REL_TYPE]->
    HAS_TYPE

    View Slide

  17. Here’s what I’ve learned

    View Slide

  18. Cypher CASE

    View Slide

  19. Pitfalls (mine, at least)
    • Logic like other languages (e.g. Java)

    • Variable-setting

    • Complex conditionals
    Photo by Wander Fleur on Unsplash

    View Slide

  20. Examples
    //Bad syntax


    LOAD CSV…AS row


    MERGE (c:Company {companyId: row.Id})


    WITH row, c,


    CASE row.type


    WHEN 'P' THEN row.type = 'Public'


    WHEN 'R' THEN row.type = 'Private'


    WHEN 'G' THEN row.type = 'Government'


    ELSE row.type = 'Other' END


    SET c.businessType = row.type


    RETURN *
    //Bad syntax 2


    LOAD CSV…AS row


    WITH row,


    CASE row.BusinessType


    WHEN 'P' THEN type = 'Public'


    WHEN 'R' THEN type = 'Private'


    WHEN 'G' THEN type = 'Government'


    ELSE type = 'Other' END


    RETURN row.CompanyId, row.CompanyName, type

    View Slide

  21. Correct Logic
    • 1. Expression comparison against multiple values

    MATCH (p:Person)-[r:IS_MANAGED_BY]->(m:Manager)-[r2:OVERSEES]->(d:Department)


    RETURN p.name,


    CASE p.role


    WHEN 'management' THEN d.departmentPhone


    WHEN 'business' THEN p.businessPhone


    WHEN 'technical' THEN p.emailAddress


    ELSE d.departmentEmail END as personContact


    • 2. Multiple conditional statements expressed

    MATCH (p:Person)


    RETURN p.name,


    CASE


    WHEN dateHired is null THEN 'candidate'


    WHEN dateHired > date(‘2020–09–17') THEN 'newHire'


    ELSE 'employee' END as personStatus,


    CASE


    WHEN dateFired is null THEN dateHired


    WHEN dateHired is null THEN entryDate


    ELSE 'n/a' END as leadDate

    View Slide

  22. In Action!

    View Slide

  23. Cypher Temporals

    View Slide

  24. Pitfalls
    • Date format

    • ISO 8601

    • Date: 2022-02-24

    • Datetime: 2022-02-24T13:00:15Z

    • Pesky literal 'T'

    • Durations

    • Precision

    • Groups
    Credit: unsplash.com

    View Slide

  25. Translating to ISO 8601
    • Cypher accepts:

    • ISO 8601

    • Strings in ISO 8601 format

    • What if you have ANYTHING else?

    • Error!

    View Slide

  26. APOC to the rescue!

    View Slide

  27. What is APOC?
    • Utility library for Neo4j

    • Widely used, broadly applicable

    • 550+ procedures and functions
    Photo by Alexis Fauvet on Unsplash

    View Slide

  28. Epoch time
    • Unix system date/time

    • Processes, logs, etc.

    • Seconds since 1970-01-01T00:00:00Z

    • Example: 1645729200 (end time for this session)

    • Handled with apoc.date.format() or apoc.date.toISO8601()
    https://www.epochconverter.com/

    View Slide

  29. In Action!

    View Slide

  30. Date Strings
    • Process:

    • Temporal string -> ISO 8601 string -> Neo4j temporal

    • Any temporal string with speci
    fi
    ed format

    • Handled with apoc.date.convertFormat() or apoc.temporal.toZonedTemporal()

    View Slide

  31. In Action!

    View Slide

  32. Cypher Temporal Durations

    View Slide

  33. Durations
    • Distance in time measurements

    • Literal ‘P’ and ’T’ in syntax

    • P3M5DT2H17M0S

    • Groups - months, days, seconds

    • Duration functions (between, inMonths, inDays, inSeconds) Photo by Randy Fath on Unsplash

    View Slide

  34. In Action!

    View Slide

  35. Duration conversions
    • Duration functions: inMonths, inDays, inSeconds

    • Most components -> whole values only, no remainders

    • Convert between units: functions + components

    • Component groups

    View Slide

  36. In Action!

    View Slide

  37. Eager operator

    View Slide

  38. What is Eager?
    • Eager loading

    • Consistency and con
    fl
    icts

    • Operations occur to all rows before continuing

    • Avoid read/write con
    fl
    icts
    Credit: unsplash.com

    View Slide

  39. What’s the difference?
    Non-Eager
    Eager
    Row-by-row
    Operation-by-operation

    View Slide

  40. Does it matter?
    • Low memory/heap

    • Large dataset

    • Query performance
    Credit: unsplash.com

    View Slide

  41. In Action!

    View Slide

  42. How to avoid eager?
    • Might not be an issue for smaller operations/data sets

    • Separate operations

    • Avoids situations where con
    fl
    icts might occur

    • Use PROFILE/EXPLAIN on queries

    • Having trouble? Ask for help!

    • dev.neo4j.com/forum -> Cypher channel

    View Slide

  43. Recap!
    • CASE statement

    • Not a programming language

    • Compare values OR multiple conditionals

    • Cypher temporal types

    • Format, transformation

    • Durations and groups

    • Eager operator

    • Separate operations

    • Inspect queries with PROFILE/EXPLAIN

    View Slide

  44. Resources
    • Blog posts: CASE, temporals, eager, and more!

    • jmhreif.com/blog/

    • Repository:

    • github.com/JMHReif/cypher-sleuthing

    • Ask for help!

    • dev.neo4j.com/forum -> Cypher channel
    Credit: unsplash.com
    Jennifer Reif
    Email: [email protected]
    Twitter: @JMHReif
    LinkedIn: linkedin.com/in/jmhreif
    Github: GitHub.com/JMHReif
    Website: jmhreif.com

    View Slide