Preface
Among the NoSQL databases, Neo4j is generating a lot of interest due to the following set of features: performance and scalability, robustness, its very natural and expressive graph model, and ACID transactions with rollbacks.
Neo4j is a graph database. Its model is simple and based on nodes and relationships. The model is described as follows:
Each node can have a number of relationships with other nodes
Each relationship goes from one node either to another node or the same node; therefore, it has a direction and involves either only two nodes or only one
Both nodes and relationships can have properties, and each property has a name and a value
Before Neo4j introduced Cypher as a preferred query, utilizing Neo4j in a real-world project was difficult compared to a traditional relational database. In particular, querying the database was a nightmare, and executing a complex query required the user to write an object, thereby performing a graph traversal. Roughly speaking, a traversal is an operation that specifies how to traverse a graph and what to do with the nodes and relationships found during the visit. Though it is very powerful, it works in a very procedural way (through callbacks), so its readability is poor and any change to the query means modifying the code and building it.
Cypher, instead, provides a declarative syntax, which is readable and powerful, and a rich set of graph patterns that can be recognized in the graph. Thus, with Cypher, you can write (and read) queries much more easily and be productive from the beginning. This book will guide you through learning this language from the ground up, and each topic will be explained with a real-world example.
What this book covers
Chapter 1, Querying Neo4j Effectively with Pattern Matching, describes the basic clauses and patterns to perform read-only queries with Cypher.
Chapter 2, Filter, Aggregate, and Combine Results, describes clauses and tips that can be used with patterns to elaborate results that come from pattern matching.
Chapter 3, Manipulating the Database, covers the write clauses, which are needed to modify a graph.
Chapter 4, Improving Performance, talks about tools and practices to improve performances of queries.
Chapter 5, Migrating from SQL, explains how to migrate a database to Neo4j from the ground up through an example.
Appendix, Operators and Functions, describes Cypher operators and functions in detail.
What you need for this book
First and foremost, you need Neo4j. The community edition is free and open source. It can be downloaded from http://www.neo4j.org/download.
In the initial chapters, the examples are created using embedded Neo4j. To run the Java code, you need any Java IDE and Maven.
If you read this book on a tablet with an Internet connection, another way to run the Cypher code is using the Neo4j Console (http://console.neo4j.org/); it allows you to run Cypher queries directly in your browser and lets you to see the results immediately.
Who this book is for
If you are a developer who wants to learn Cypher to interact with Neo4j and find out the capabilities of this language, this book is for you.
The first chapter assumes that you are a little familiar with the Java syntax; anyway, you don't require Java to understand Cypher examples that can be launched in the Neo4j console.
The last chapter on migration from SQL assumes you know SQL and the relational model.
Conventions
In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We can assign starting points to variables in the query using the START
keyword."
A block of code is set as follows:
START a=node(2), b=node(3) RETURN allShortestPaths((a)-[*]-(b)) AS path
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
MATCH (n:Employee {surname: {inputSurname}})
RETURN n
Any command-line input or output is written as follows:
# bin\Neo4jInstaller.bat install
New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "In the next page of the wizard, name the project, set a valid project location, and then click on Finish."
Note
Warnings or important notes appear in a box like this.
Tip
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply send an e-mail to <[email protected]>
, and mention the book title via the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Errata
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
Piracy
Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]>
with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
Questions
You can contact us at <[email protected]>
if you are having a problem with any aspect of the book, and we will do our best to address it.