Panzarino | Learning Cypher | E-Book | www.sack.de
E-Book

E-Book, Englisch, 162 Seiten

Panzarino Learning Cypher

Write powerful and efficient queries for Neo4j with Cypher, its official query language
1. Auflage 2025
ISBN: 978-1-78328-776-5
Verlag: De Gruyter
Format: PDF
Kopierschutz: Adobe DRM (»Systemvoraussetzungen)

Write powerful and efficient queries for Neo4j with Cypher, its official query language

E-Book, Englisch, 162 Seiten

ISBN: 978-1-78328-776-5
Verlag: De Gruyter
Format: PDF
Kopierschutz: Adobe DRM (»Systemvoraussetzungen)



An easy-to-follow guide full of tips and examples of real-world applications. In each chapter, a thorough example will show you the concepts in action, followed by a detailed explanation.

This book is intended for those who want to learn how to create, query, and maintain a graph database, or who want to migrate to a graph database from SQL. It would be helpful to have some familiarity with Java and/or SQL, but no prior experience is required.

Panzarino Learning Cypher jetzt bestellen!

Autoren/Hrsg.


Weitere Infos & Material


Chapter 1. Querying Neo4j Effectively with Pattern Matching


Querying a graph database using the Java API can be very tedious; you would need to visit the whole graph and skip nodes that don't match what you are searching for. Any changes to the query will result in rethinking the code, changing it, and building it all over again. Why? The reason is that we are using an imperative language to do pattern matching, and traditional imperative languages don't work well in this task. Cypher is the declarative query language used to query a Neo4j database. Declarative means that it focuses on the aspects of the result rather than on methods or ways to get the result so that it is human-readable and expressive.

In this chapter, we will cover the following topics:

  • Setting up a Neo4j database
  • Querying the database in a simpler way than using the Java API

Setting up a new Neo4j database


If you already have experience in creating a Neo4j database, you can skip this and jump to the next section.

Neo4j is a graph database, which means that it does not use tables and rows to represent data logically; instead, it uses nodes and relationships. Both nodes and relationships can have a number of properties. While relationships must have one direction and one type, nodes can have a number of labels. For example, the following diagram shows three nodes and their relationships, where every node has a label (language or graph database), while relationships have a type (QUERY_LANGUAGE_OF and WRITTEN_IN).

The properties used in the graph shown in the following diagram are name, type, and from. Note that every relation must have exactly one type and one direction, whereas labels for nodes are optional and can be multiple.

Neo4j running modes


Neo4j can be run in two modes:

  • An embedded database in a Java application
  • A standalone server via REST

In any case, this choice does not affect the way you query and work with the database. It's an architectural choice driven by the nature of the application (whether a standalone server or a client server), performance, monitoring, and safety of data.

Neo4j Server


Neo4j Server is the best choice for interoperability, safety, and monitoring. In fact, the REST interface allows all modern platforms and programming languages to interoperate with it. Also, being a standalone application, it is safer than the embedded configuration (a potential crash in the client wouldn't affect the server), and it is easier to monitor. If we choose to use this mode, our application will act as a client of the Neo4j server.

To start Neo4j Server on Windows, download the package from the official website (http://www.neo4j.org/download/windows), install it, and launch it from the command line using the following command:

C:\Neo4jHome\bin\Neo4j.bat

You can also use the frontend, which is bundled with the Neo4j package, as shown in the following screenshot:

To start the server on Linux, you can either install the package using the Debian package management system, or you can download the appropriate package from the official website (http://www.neo4j.org/download) and unpack it with the following command:

# tar -cf

After this, you can go to the new directory and run the following command:

# ./bin/neo4j console

Anyway, when we deploy the application, we will install the server as a Windows service or as a daemon on Linux. This can be done easily using the Neo4j installer tool.

On the Windows command launch interface, use the following command:

# bin\Neo4jInstaller.bat install

When installing it from the Linux console, use the following command:

# neo4j-installer install

To connect to Neo4j Server, you have to use the REST API so that you can use any REST library of any programming language to access the database. Though any programming language that can send HTTP requests can be used, you can also use online libraries written in many languages and platforms that wrap REST calls, for example, Python, .NET, PHP, Ruby, Node.js, and others.

An embedded database


An embedded Neo4j database is the best choice for performance. It runs in the same process of the client application that hosts it and stores data in the given path. Thus, an embedded database must be created programmatically. We choose an embedded database for the following reasons:

  • When we use Java as the programming language for our project
  • When our application is standalone

For testing purposes, all Java code examples provided with this book are made using an embedded database.

Preparing the development environment

The fastest way to prepare the IDE for Neo4j is using Maven. Maven is a dependency management as well as an automated building tool. In the following procedure, we will use NetBeans 7.4, but it works in a very similar way with the other IDEs (for Eclipse, you will need the m2eclipse plugin). The procedure is described as follows:

  1. Create a new Maven project as shown in the following screenshot:
  2. In the next page of the wizard, name the project, set a valid project location, and then click on Finish.
  3. After NetBeans has created the project, expand Project Files in the project tree and open the file. In the tag, insert the following XML code:

This code informs Maven about the dependency we are using on our project, that is, Neo4j. The version we have used here is 2.0.1. Of course, you can specify the latest available version.

If you are going to use Java 7, and the following section is not present in the file, then you'll need to add the following code to instruct Maven to compile Java 7:

Once saved, the Maven file resolves the dependency, downloads the JAR files needed, and updates the Java build path. Now, the project is ready to use Neo4j and Cypher.

Creating an embedded database

Creating an embedded database is straightforward. First of all, to create a database, we need a class, which can be done with the following code:

GraphDatabaseFactory graphDbFactory = new GraphDatabaseFactory();

Then, we can invoke the method with the following code:

GraphDatabaseService graphDb = graphDbFactory .newEmbeddedDatabase("data/dbName");

Now, with the class, we can fully interact with the database, create nodes, create relationships, and set properties and indexes.

Configuration

Neo4j allows you to pass a set of configuration options for performance tuning, caching, logging, file system usage, and other low-level behaviors. The following code sets the size of the memory allocated for mapping the node store to 20 MB:

import org.neo4j.graphdb.factory.GraphDatabaseSettings; // ... GraphDatabaseService db = graphDbFactory .newEmbeddedDatabaseBuilder(DB_PATH) .setConfig(GraphDatabaseSettings .nodestore_mapped_memory_size, "20M") .newGraphDatabase();

You will find all the available configuration settings in the class (they are all static final members).

Note that the same result can be achieved using the file. Clearly, reading the configuration settings from a file comes in handy when the application is deployed because any modification to the configuration won't require a new build. To replace the preceding code, create a file and name it, for example, . Open it with a text editor and write the following code in...


Panzarino Onofrio :

Onofrio Panzarino is a programmer with 15 years experience working with various languages (mostly with Java), platforms, and technologies. Before obtaining his Master of Science degree in Electronics Engineering, he worked as a digital signal processor programmer. Around the same time, he started working as a C++ developer for embedded systems and PCs. Currently, he is working with Android, ASP.NET or C#, and JavaScript for Wolters Kluwer Italia. During these years, he gained a lot of experience with graph databases, particularly with Neo4j.Onofrio resides in Ancona, Italy. His Twitter handle is (@onof80). He is a speaker in the local Java user group and also a technical writer, mostly for Scala and NoSQL. In his spare time, he loves playing the piano with his family and programming with functional languages.



Ihre Fragen, Wünsche oder Anmerkungen
Vorname*
Nachname*
Ihre E-Mail-Adresse*
Kundennr.
Ihre Nachricht*
Lediglich mit * gekennzeichnete Felder sind Pflichtfelder.
Wenn Sie die im Kontaktformular eingegebenen Daten durch Klick auf den nachfolgenden Button übersenden, erklären Sie sich damit einverstanden, dass wir Ihr Angaben für die Beantwortung Ihrer Anfrage verwenden. Selbstverständlich werden Ihre Daten vertraulich behandelt und nicht an Dritte weitergegeben. Sie können der Verwendung Ihrer Daten jederzeit widersprechen. Das Datenhandling bei Sack Fachmedien erklären wir Ihnen in unserer Datenschutzerklärung.