Cypher is a declarative language to query graph databases. As graph databases are becoming more mainstream, there is a dearth of content and guidance for developers to leverage the database capabilities fully. This book is an attempt to fill that information gap. It is well suited to describe graph traversal patterns in a simple and readable manner. While it is easy to learn and understand, it can be very difficult to master.
This book provides a guided tour of Cypher language from understanding the syntax of the language, building a graph data model, loading the data into graphs, building queries, and profiling the queries for best performance. It introduces APOC utilities that can augment the Cypher queries to build complex queries. It also introduces visualization tools such as Bloom to get the most out of the graph when presenting the results to the end users.
By the end of this book, you should be a seasoned Cypher query developer with a good understanding of the query language and how to get the best performance out of it.
This book is targeted at database administrators, database developers, graph database developers, and graph database architects. This book will also help someone migrate from a DBA role to a graph data engineer or data scientist role.
If you are working with graph databases and need to learn Cypher, or are a basic Cypher developer who wants to get better at data modeling and tuning queries to build performant Cypher queries, then this is the book for you.
Chapter 1, Introduction to Neo4j and Cypher, introduces Cypher and Neo4j. We discuss what the Cypher language is and how it is used to query the Neo4j graph database. We also take a look at how Cypher is different from other query languages and what sets it apart for querying graph databases.
Chapter 2, Components of Cypher, introduces the Cypher syntax with some examples. We will review the important aspects of the Cypher syntax and semantics in building graph traversal queries. We will discuss important keywords and the role they play in building the queries. We will take a look at the graph data model and how Cypher queries follow the data connections.
Chapter 3, Loading Data with Cypher, explains how to load the data into Neo4j using Cypher. We will discuss the various options available to create/update/delete the nodes, labels, relationships, and properties. We will discuss loading CSV, text and JSON files into Neo4j using Cypher.
Chapter 4, Querying Graph, discusses how querying works with Cypher. It discusses leveraging indexes on nodes and relationships to anchors and traversals. It also discusses conditional traversals, using multiple relationship types, returning paths, nodes, relationships, and so on. It also talks about returning the data as column-formatted data.
Chapter 5, Filtering, Sorting, and Aggregations, discusses how to filter the data using where conditions on nodes and/or relationship properties, and how to sort the data and use aggregation functions such as SUM, AVG, COUNT, and so on.
Chapter 6, List Expressions, UNION, and Subqueries, talks about using list expressions to reduce and process lists. It also explores how UNIONs can be used to return combined results from multiple queries along with leveraging subqueries to filter and process data.
Chapter 7, Working with Lists and Maps, explains how lists and maps are core elements in Cypher. This chapter discusses how we can handle lists and maps both as an input and as an output. It shows how easy it is to handle lists and maps as part of the querying process. It shows how to handle basic lists with Strings and also how to access individual elements at random or iterate through them.
Chapter 8, Advanced Query Patterns, discusses advanced concepts such as OPTIONAL MATCH to handle scenarios where the graph path may or may not exist. It also discusses the query chain using WITH and UNWIND. It also talks about executing subqueries using the CALL keyword and handling the responses in the main query.
Chapter 9, Query Tuning, talks about options to tune the Cypher queries. It talks about working with EXPLAIN PLAN to understand the query execution plan. It also talks about using PROFILE to understand how the query is executing and the amount of data being processed to understand the reasons for bottlenecks.
Chapter 10, Using APOC Utilities, talks about using APOC utilities to extend the built-in capabilities of Cypher. It gives more options to be able to load CSV and JSON data, schedule timers, make ad hoc batch data modifications, and so on.
Chapter 11, Cypher Ecosystem, talks about the Cypher ecosystem. It introduces you to tools and packages available to do more advanced data processing along with visualizing the results as graphs, tables, and so on.
Chapter 12, Tips and Tricks, talks about the best practices to get the most out of Cypher queries, which includes how to leverage data modeling and patterns. It also discusses the tips and tricks to identify performance bottlenecks and how to go about addressing them.
There is no need to have RDBMS querying experience or a good understanding of graphs to get the most out of this book. Familiarity with databases and SQL will help in understanding some of the Cypher concepts and tuning approaches.
Software/hardware covered in the book |
Operating system requirements |
Neo4j Desktop and Neo4j Database Server |
Windows, macOS, or Linux |
Note
All the Cypher code used in this book has been tested on Neo4j version 4.4.8 but should work with the latest Neo4j 5.0 release.
If you are using the digital version of this book, we advise you to type the code yourself or access the code from the book’s GitHub repository (a link is available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.
You can join the Neo4j community at https://community.neo4j.com to get answers to most Cypher or Neo4j-related questions. There is a huge developer community that answers all the basic syntax to modeling-related questions.
You can download the example code files for this book from GitHub at https://github.com/PacktPublishing/Cypher-Querying. If there’s an update to the code, it will be updated in the GitHub repository.
We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
There are several text conventions used throughout this book.
Code in text: Indicates code words in the text, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: “we are using the apoc procedure to add EncounterClass to an encounter node. Since we are trying to add labels dynamically, we have to use the apoc option.”
A block of code is set as follows:
CREATE (p {name: 'Tom'}) RETURN p
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold: “The Cypher query looks like this:
MATCH (d:Drug)<-[:HAS_DRUG]-()<-[:HAS_ENCOUNTER]-(p) WITH DISTINCT d, p WITH d.description as drug, count(p) as patients WHERE patients > 100 RETURN drug, patients
Bold: Indicates a new term, an important word, or words that you see onscreen. For instance, words in menus or dialog boxes appear in bold. Here is an example: “In particular, the addition of a new Encounter node and the HAS_END relationship are a bit different from how we approach data in the RDBMS world.”
Tips or important notes
Appear like this.
Feedback from our readers is always welcome.
General feedback: If you have questions about any aspect of this book, email us at [email protected] and mention the book title in the subject of your message.
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata and fill in the form.
Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Once you’ve read Graph Data Processing with Cypher, we’d love to hear your thoughts! Please click here to go straight to the Amazon review page for this book and share your feedback.
Your review is important to us and the tech community and will help us make sure we’re delivering excellent quality content.
Thanks for purchasing this book!
Do you like to read on the go but are unable to carry your print books everywhere? Is your eBook purchase not compatible with the device of your choice?
Don’t worry, now with every Packt book you get a DRM-free PDF version of that book at no cost.
Read anywhere, any place, on any device. Search, copy, and paste code from your favorite technical books directly into your application.
The perks don’t stop there, you can get exclusive access to discounts, newsletters, and great free content in your inbox daily
Follow these simple steps to get the benefits:
https://packt.link/free-ebook/9781804611074
44.211.24.175