Preface - Learning Cypher (2014)

Learning Cypher (2014)

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.