Home Web Development Building web applications with Python and Neo4j

Building web applications with Python and Neo4j

By Sumit Gupta
books-svg-icon Book
eBook $29.99 $20.98
Print $38.99
Subscription $15.99 $10 p/m for three months
$10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
BUY NOW $10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
eBook $29.99 $20.98
Print $38.99
Subscription $15.99 $10 p/m for three months
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
About this book
Publication date:
July 2015
Publisher
Packt
Pages
184
ISBN
9781783983988

 

Chapter 1. Your First Query with Neo4j

Neo4j is a graph database and has been in commercial development for over a decade. It comes with several flavors, supporting a wide variety of use cases and requirements imposed by start-ups, large enterprises, and Fortune 500 customers. It is a fully transactional database; it supports Atomicity, Consistency, Isolation, Durability (ACID) and is also well equipped to handle the complexities introduced by various kinds of systems—web-based, online transaction processing (OLTP), data-warehousing, analytics, and so on.

This chapter will help you to understand the paradigm, applicability, various aspects, and characteristics of Neo4j as a graph database. It will guide you through the installation process, starting right from downloading and running your first Cypher query leveraging various interfaces/tools/utilities exposed by Neo4j against your fully-working instance.

At the end of this chapter, your work environment will be fully functional, and you will be able to write your first Cypher query to insert/fetch the data from the Neo4j database.

This chapter will cover the following points:

  • Thinking in graphs for SQL developers

  • Licensing and configuring – Neo4j

  • Using the Neo4j shell

  • Introducing the Neo4j REST interface

  • Running queries from the Neo4j browser

 

Thinking in graphs for SQL developers


Some might say that it is difficult for SQL developers to understand the paradigm of graphs, but it is not entirely true. The underlying essence of data modeling does not change. The focus is still on the entities and the relationship between these entities. Having said that, let's discuss the pros/cons, applicability, and similarity of the relational models and graph models.

The relational models are schema-oriented. If you know the structure of data in advance, it is easy to ensure that data conforms to it, and at the same time, it helps in enforcing stronger integrity. Some examples include traditional business applications, such as flight reservations, payroll, order processing, and many more.

The graph models are occurrence-oriented—Probabilistic model. They are adaptive and define a generic data structure that is evolving and works well with scenarios where the schema is not known in advance. The graph model is perfectly suited to store, manage, and extract highly-connected data.

Let's briefly discuss the disadvantages of the SQL databases, which led to the evolution of the graph databases:

  • It is difficult to develop efficient models for evolving data, such as social networks

  • The focus is more on the structure of data than the relationships

  • They lack an efficient mechanism for performing recursions

All the preceding reasons were sufficient to design a different data structure, and as a result, the graph data structures were introduced.

The objective of the graph databases was specifically to meet the disadvantages of the SQL databases. However, Neo4j as a graph database, also leveraged the advantages of the SQL databases wherever possible and applicable. Let's see a few of the similarities between the SQL and graph databases:

  • Highly Consistent: At any point in time, all nodes contain the same data at the same time

  • Transactional: All insert or update operations are within a transaction where they are ACID

Having said that, it is not wrong to say that the graph databases are more or less the next generation of relational databases.

Comparing SQL and Cypher

Every database has its own query languages; for example, RDBMS leverages SQL and conforms to SQL-92 (http://en.wikipedia.org/wiki/SQL-92). Similarly, Neo4j also has its own query language—Cypher. The syntax of Cypher has similarities with SQL, though it still has its own unique characteristics, which we will discuss in the upcoming sections.

Neo4j leveraged the concept of patterns and pattern matching, and introduced a new declarative graph query language, Cypher, for the Neo4j graph database. Patterns and pattern matching are the essence and core of Neo4j, so let's take a moment to understand them. We will then talk about the similarities between SQL and Cypher.

Patterns are a given sequence or occurrence of tokens in a particular format. The act of matching patterns within a given sequence of characters or any other compatible input form is known as pattern matching. Pattern matching should not be confused with pattern recognition, which usually produces the exact match and does not have any concept of partial matches.

Pattern matching is the heart of Cypher and a very important component of the graph databases. It helps in searching and identifying a single or a group of nodes by walking along the graph. Refer to http://en.wikipedia.org/wiki/Pattern_matching for more information on the importance of pattern matching in graphs. Let's move forward and talk about Cypher, and it's similarities with SQL.

Cypher is specifically designed to be a human query language, which is focused on making things simpler for developers. Cypher is a declarative language and implements "What to retrieve" and not "how to retrieve", which is in contrast to the other imperative languages, such as Java and Gremlin (refer to http://gremlin.tinkerpop.com/).

Cypher borrows much of its structure from SQL, which makes it easy to use/understand for SQL developers. "SQL familiarity" is another objective of Cypher.

Let's refer to the following illustration, which defines the Cypher constructs and the similarity of Cypher with SQL constructs:

The preceding diagram defines the mapping of the common SQL and Cypher constructs. It also depicts the examples stating the usage of these constructs.

For instance, FROM is similar to MATCH or START and produces the same results. Although the way they are used is different but the objective and concept remains the same.

We will talk about Cypher in detail in Chapter 2, Querying the Graph with Cypher and Chapter 3, Mutating Graph with Cypher, but without getting into the nitty-gritty and syntactical details. The following is one more illustration that briefly describes the similarities between the Cypher and SQL constructs:

In the preceding illustration, we are retrieving the data using Cypher pattern matching. In the statement shown in the preceding diagram, we are retrieving all the nodes that are labeled with FEMALE in our Neo4j database. This statement is very similar to the SQL statement where we want to retrieve some specific rows of a table based on a given criteria, such as the following query:

SELECT * from EMPLOYEE where GENDER = 'FEMALE'

The preceding examples should be sufficient to understand that SQL developers can learn Cypher in no time.

Let's take one more example where we want to retrieve the total number of employees in the company X:

  • SQL syntax: Select count (EMP-ID) from Employee where COMPANY_NAME='X'

  • Cypher syntax: match (n) where n.CompanyName='X' return count(n);

The preceding Cypher query shows the usage of aggregations such as count, which can also be replaced by sum, avg, min, max, and so on.

Note

Refer to http://neo4j.com/docs/stable/query-aggregation.html for further information on aggregations in Cypher.

Let's move forward and discuss the transformation of the SQL data structures into the graph data structures.

Evolving graph structures from SQL models

The relational models are the simplest models to depict and define the entities and the relationship between those entities. It is easy to understand and you can quickly whiteboard with your colleagues and domain experts.

A graph model is similar to a relational model as both models are focused on the domain and use case. However, there is a substantial difference in the way they are created and defined. We will discuss the way the graph models are derived from the relational models, but before that, let's look at the important components of the graph models:

  • Nodes: This component represents entities such as people, businesses, accounts, or any other item you might want to keep track of.

  • Labels: This component is the tag that defines the category of nodes. There can be one or more labels on a node. A label also helps in creating indexes, which further help in faster retrievals. We will discuss this in Chapter 3, Mutating Graph with Cypher.

  • Relationship: This component is the line that defines the connection between the two nodes. Relationship can further have its own properties and direction.

  • Properties: This component is pertinent information that relates to the nodes. This can be applied to a node or the relationship.

Let's take an example of a relational model, which is about an organization, and then understand the process of converting this into a graph model:

In the preceding relational model, we have employee, department, and title as entities, and Emp-Dept and Emp-Title as the relationship tables.

Here is sample data within this model:

The preceding screenshot depicts the sample data within the relational structures. The following are the guidelines to convert the preceding relational model into the graph model:

  • The entity table is represented by a label on nodes

  • Each row in an entity table is a node

  • The columns on these tables become the node properties

  • The foreign keys and the join tables are transformed into relationships; columns on these tables become the relationship properties

Now, let's follow the preceding guidelines and convert our relational model into the graph model, which will look something like the below image:

The preceding illustration defines the complete process and the organization of data residing in the relational models into the graph models. We can use the same guidelines for transforming a variety of relational models into the graph structures.

In this section, we discussed the similarities between SQL and Cypher. We also talked and discussed about the rules and processes of transforming the relational models into graph models. Let's move forward and understand the licensing and installation procedure of Neo4j.

 

Licensing and configuring – Neo4j


Neo4j is an open source graph database, which means all its sources are available to the public (currently on GitHub at https://github.com/neo4j/neo4j). However, Neo Technology, the company behind Neo4j, distributes the latter in two different editions—the Community edition and Enterprise edition. Let's briefly discuss the licensing policy for the Community and Enterprise editions, and then we will talk about the installation procedures on the Unix/Linux operating systems.

Licensing – Community Edition

Community Edition is a single node installation licensed under General Public License (GPL) Version 3 (http://en.wikipedia.org/wiki/GNU_General_Public_License) and is used for the following purposes:

  • Preproduction environments, such as development or QA for fast paced developments

  • Small to medium scale applications where it is preferred to embed the database within the existing application

  • Research and development where advanced monitoring and high performance is not the focus

You can benefit from the support of the whole Neo4j community on Stack Overflow, Google Groups, and Twitter.

Note

If you plan to ask a question on Stack Overflow, do not forget to tag your question with the #Neo4j hashtag.

Licensing – Enterprise Edition

Enterprise Edition comes with three different kinds of subscription options and provides the distributed deployment of the Neo4j databases, along with various other features, such as backup, recovery, replication, and so on.

  • Personal license: It is free of charge and may look very similar to Community Edition. It targets students, as well as small businesses.

  • Startup program: Starting from this plan, you can benefit from the enterprise support. A startup license allows workday support hours—10 hours per 5 business days.

  • Enterprise subscriptions: With this plan, you can benefit from 24/7 support and emergency custom patches if needed. At this scale, your company will have to directly contact Neo Technology to assess the cost of your required setup.

    Note

    The license defines instance as the Java Virtual Machine hosting a Neo4j server.

Each of the subscription is subject to its own license and pricing. Visit http://neo4j.com/subscriptions/ for more information about available subscriptions with Enterprise Edition.

Installing Neo4J Community Edition on Linux/Unix

In this section, we will talk about the Neo4j installation on the Linux/Unix operating system. At the end of this section, you will have a fully-functional Neo4j instance running on your Linux/Unix desktop/server.

Let's perform the following common steps involved in the Neo4j installation on Linux/Unix:

  1. Download and install Oracle Java 7 (http://www.oracle.com/technetwork/java/javase/install-linux-self-extracting-138783.html) or open JDK 7 (https://jdk7.java.net/download.html).

  2. Set JAVA_HOME as an environment variable and the value of this variable will be the file system path of your JDK installation directory:

    export JAVA_HOME=<Path of Java install Dir>
  3. Download the stable release of the Linux distribution, neo4j-community-2.2.0-RC01-unix.tar.gz, from http://neo4j.com/download/other-releases/.

Neo4j can be installed and executed as a Linux service, or it can also be downloaded as the .tar file, where, after installation, it needs to be started manually.

Let's talk about the steps involved in installing Neo4j as a service, and then we will also talk about the standalone archive.

Installing as a Linux tar / standalone application

Architects have always preferred to install critical applications as a Linux service, but there can be reasons, such as insufficient privileges, which restrict you from installing software as a Linux service. So, whenever you cannot install software as a Linux service, there is another way in which you can download Neo4j, perform manual configuration, and start using it.

Let's perform the following steps to install Neo4j as a Linux tar / standalone application:

  1. Once you have downloaded the Neo4j archive, browse the directory from where you want to extract the Neo4j server and untar the Linux/Unix archive: tar –xf <location of Archive file>. Let's refer to the top-level extracted directory as $NEO4J_HOME.

  2. Open the Linux shell or console and execute the following commands for starting the sever:

    • <$NEO4J_HOME>/bin/neo4j - start: This command is used for running the server in a new process

    • <$NEO4J_HOME>/bin/neo4j - console: This command is used for running the server in the same process or window without forking a new process

    • <$NEO4J_HOME>/bin/neo4j - restart: This command is used for restarting the server

  3. Browse http://localhost:7474/browser/ and you will see the login screen of the Neo4j browser.

  4. Enter the default username/password as neo4j/neo4j and press Enter. The next screen will ask you to change the default password.

  5. Change the password and make sure that you remember it. We will use this new password in the upcoming examples.

  6. Stop the server by pressing Ctrl + C or by typing <$NEO4J_HOME>/bin/neo4j - stop.

Installing as a Linux service

This is the most preferred procedure for installing Neo4j in all kinds of environments, whether it's production, development, or QA. Installing Neo4j as a Linux service helps a Neo4j server and database to be available for use at server start-up and also survive user logons/logoffs. It also provides various other benefits such as ease of installation, configuration, and up-gradation.

Let's perform the following steps and install Neo4j as a Linux service:

  1. Once the Neo4j archive is downloaded, browse the directory from where you want to extract the Neo4j server and untar the Linux/Unix archive: tar –xf <location of Archive file>. Let's refer to the top-level extracted directory as $NEO4J_HOME.

  2. Change the directory to $NEO4J_HOME; and execute the command, sudo bin/neo4j neo4j-installer install; and follow the steps as they appear on the screen.

    Note

    The installation procedure will provide an option to select the user that will be used to run the Neo4j server. You can supply any existing or new Linux user (defaults to Neo4j). If a user is not present, it will be created as a system account and the ownership of <$NEO4J_HOME>/data will be moved to that user.

  3. Once the installation is successfully completed, execute sudo service neo4j-service start on the Linux console for starting the server and sudo service neo4j-service stop for gracefully stopping the server.

  4. Browse http://localhost:7474/browser/ and you will see the login screen of the Neo4j browser.

  5. Enter the default username/password as neo4j/neo4j and press Enter. The next screen will ask you to change the default password.

  6. Change the password and make sure that you remember it. We will use this new password in the upcoming examples.

    Note

    To access the Neo4j browser on remote machines, enable and modify org.neo4j.server.webserver.address in neo4j-server.properties and restart the server.

Installing Neo4j Enterprise Edition on Unix/Linux

High availability, fault tolerance, replication, backup, and recovery are a few of the notable features provided by Neo4j Enterprise Edition. Setting up a cluster of Neo4j nodes is quite similar to the single node setup, except for a few properties which need to be modified for the identification of node in a cluster.

Let's perform the following steps for installing Neo4j Enterprise Edition on Linux:

  1. Download and install Oracle Java 7 (http://www.oracle.com/technetwork/java/javase/install-linux-self-extracting-138783.html) or open JDK 7 (https://jdk7.java.net/download.html).

  2. Set JAVA_HOME as the environment variable and the value of this variable will be the file system path of your JDK installation directory:

    export JAVA_HOME=<Path of Java install Dir>
  3. Download the stable release of the Linux distribution, neo4j-community-2.2.0-RC01-unix.tar.gz from http://neo4j.com/download/other-releases/.

  4. Once downloaded, extract the archive into any of the selected folders and let's refer to the top-level extracted directory as $NEO4J_HOME.

  5. Open <$NEO4J_HOME>\conf\neo4j-server.properties and enable/modify the following properties:

    • org.neo4j.server.database.mode=HA: Keep this value as HA, which means high availability. You can run it as a standalone too by providing the value as SINGLE.

    • org.neo4j.server.webserver.address=0.0.0.0: This property enables and provides the IP of the node for enabling remote access.

  6. Open <$NEO4J_HOME>\conf\neo4j.properties and enable/modify the following properties:

    • ha.server_id=: This property is the unique ID of each node that will participate in the cluster. It should be an integer (1, 2, or 3).

    • ha.cluster_server=192.168.0.1:5001: This property is the IP address and port for communicating the cluster status information with other instances.

    • ha.server=192.168.0.1:6001: This property is the IP address and port for the node for communicating the transactional data with other instances.

    • ha.initial_hosts=192.168.0.1:5001,192.168.0.2:5001: This property is a comma-separated list of host:port (ha.cluster_server) where all nodes will be listening. This will be the same for all the nodes participating in the same cluster.

    • remote_shell_enabled=true: Enable this property for connecting the server remotely through the shell.

    • remote_shell_host=127.0.0.1: This property enables and provides an IP address where remote shell will be listening.

    • remote_shell_port=1337: This property enables and provides the port at which shell will listen. You can keep it as default in case the default port is not being used by any other process.

  7. Open <$NEO4J_HOME>/bin, execute ./neo4j start and you are done. Stop the server by pressing Ctrl + C or by typing ./neo4j stop.

  8. Browse http://<IP>:7474/browser/ for interactive shell, and on the login screen, enter the default username/password as neo4j/neo4j and press Enter.

  9. The next screen will ask you to change the default password. Change the password and make sure that you remember it. We will use this new password in the upcoming examples.

 

Using the Neo4j shell


The Neo4j shell is a powerful interactive shell for interacting with the Neo4j database. It is used for performing the CRUD operations on graphs.

The Neo4j shell can be executed locally (on the same machine on which we have installed the Neo4j server) or remotely (by connecting the Neo4j shell to a remote sever).

By default, the Neo4j shell (<$NEO4J_HOME>/bin/neo4j-shell) can be executed on the same machine on which the Neo4j server is installed, but the following configuration changes are required in <$NEO4J_HOME>/conf/neo4j.properties to enable the connectivity of the Neo4j database from the remote machines:

  • remote_shell_enabled=true: This configuration enables the property

  • remote_shell_host=127.0.0.1: This configuration enables and provides the IP address of the machine on which the Neo4j server is installed

  • remote_shell_port=1337: This configuration enables and defines the port for incoming connections

Let's talk about various other options provided by the Neo4j shell for connecting to the local Neo4j server:

  • neo4j-shell -path <PATH>: This option shows the path of the database directory on the local file system. A new database will be created in case the given path does not contain a valid Neo4j database.

  • neo4j-shell -pid <PID>: This option connects to a specific process ID.

  • neo4j-shell -readonly: This option connects to the local database in the READ ONLY mode.

  • neo4j-shell -c <COMMAND>: This option executes a single Cypher statement and then the shell exits.

  • neo4j-shell -file <FILE >: This option reads the contents of the file (multiple Cypher CRUD operations), and then executes it.

  • neo4j-shell –config - <CONFIG>: This option reads the given configuration file (such as neo4j-server.properties) from the specified location, and then starts the shell.

The following are the options for connecting to the remote Neo4j server:

  • neo4j-shell -port <PORT>: This option connects to the server running on a port different to the default port (1337)

  • neo4j-shell -host <HOST>: This option shows the IP address or domain name of the remote host on which the Neo4j server is installed and running.

Let's move forward and get our hands dirty with the system.

To begin with and to make it simple, first we will insert the data, and then try to fetch the same data through the Neo4j shell.

Let's perform the following steps for running our Cypher queries in the Neo4j shell:

  1. Open your UNIX shell/console and execute <$NEO4J_HOME>/bin/neo4j start. This will start your Neo4j server in another process.

  2. In the same console, execute <$NEO4J_HOME>/bin/neo4j-shell to start the Neo4j shell.

  3. Next, execute the following set of statements on the console:

    CREATE (movies:Movie {Name:"Noah", ReleaseYear:"2014"});
    MATCH (n) return n;
    MATCH (n) delete n;
  4. You will see something like the following image on your console:

Yes, that's it…we are done!

We will dive deep into the details of the Cypher statements in the upcoming chapters, but let's see the results of each of the preceding Cypher statements:

  • CREATE (movies:Movie {Name:"Noah", ReleaseYear:"2014"});: This statement creates a node with two attributes, Name:"Noah" and ReleaseYear:"2014", and a label, Movie

  • MATCH (n) return n;: This statement searches the Neo4j database and prints all the nodes and their associated properties on the console

  • MATCH (n) delete n;: This statement searches the Neo4j database and deletes all the selected nodes

 

Introducing the Neo4j REST interface


Neo4j exposes a variety of REST APIs for performing the CRUD operations. It also provides various endpoints for the search and graph traversals. Neo4j 2.2.x provides the additional feature of securing the REST endpoints.

Let's move forward and see a step-by-step process to access and execute the REST APIs for performing the CRUD operations.

Authorization and authentication

In order to prevent unauthorized access to endpoints, Neo4j 2.2.x, by default, provides token-based authorization and authentication for all the REST endpoints.

Therefore, before running any CRUD operations, we need to get the security token so that every request is authenticated and authorized by the Neo4j server.

Let's perform the following steps for getting the token:

  1. Open your UNIX shell/console and execute <$NEO4J_HOME>/bin/neo4j start to start your Neo4j server, in case it is not running.

  2. Download any tool such as SOAP-UI (http://www.soapui.org/), which provides the creation and execution of the REST calls.

  3. Open your tool and execute the following request and parameters for creating data in the Neo4j database:

    • Request method type: POST

    • Request URL: http://localhost:7474/authentication

    • Request headers: Accept: application/json; charset=UTF-8 and Content-Type: application/json

    • Additional HTTP header: Authorization= Basic <base64(username:password)>

  4. In the preceding request, replace <base64(username:password)> with the base64 encoded string for username:password. This username is the default username, neo4j, and the password is the real password, which was provided/changed when you accessed your Neo4j browser for the first time.

  5. For example, the base64 encoded string for username, neo4j, and password, sumit, will be bmVvNGo6c3VtaXQ=, so now your additional HTTP header will be something like the following:

    Authorization = Basic bmVvNGo6c3VtaXQ=

The preceding screenshot shows the format of the request along with all the required parameters for authorizing the REST-based request to the Neo4j server.

You can also switch off the authentication by modifying dbms.security.authorization_enabled=true in $NEO4J_HOME/conf/neo4j-server.propoerties. Restart your server after modifying the property.

Now, as we have a valid token, let's move ahead and execute various CRUD operations.

Note

For converting in base64, you can use the online utility at http://www.motobit.com/util/base64-decoder-encoder.asp or you can also use the Python base64 library at https://docs.python.org/2/library/base64.html.

CRUD operations

Create, read, update, and delete are the four basic and most common operations for any persistence storage. In this section, we will talk about the process and syntax leveraged by Neo4j to perform all these basic operations.

Perform the following steps for creating, searching, and deleting data in the Neo4j database:

  1. Download any tool such as SOAP-UI (http://www.soapui.org/), which provides the creation and execution of the REST calls.

  2. Open your tool and execute the following request and parameters for creating data in the Neo4j database:

    • Request method type: POST

    • Request URL: http://localhost:7474/db/data/transaction

    • Request headers: Accept: application/json; charset=UTF-8 and Content-Type: application/json

    • JSON-REQUEST: {"statements": [{"statement" : "CREATE (movies:Movie {Name:"Noah", ReleaseYear:"2014"});"}]}

    • Additional HTTP header: Authorization = Basic <base64(username:password)>

  3. Replace <base64(username:password)> with the actual base64 token, which we generated in the Authorization and Authentication section, and execute the request. You will see no errors and the output will look something like the following screenshot:

    In the preceding screenshot, the CREATE request created a label Movie with two attributes, Name and ReleaseYear

  4. Next, let's search the data, which we created in the previous example. Open your tool and execute the following request and parameters for searching data in the Neo4j database:

    • Request method type: POST

    • Request URL: http://localhost:7474/db/data/transaction

    • Request Headers: Accept: application/json; charset=UTF-8 and Content-Type: application/json

    • JSON-REQUEST: {"statements": [{"statement" : "MATCH (n) return n;"}]}

    • Additional HTTP Header: Authorization = Basic <base64(username:password)>

  5. Replace <base64(username:password)> with the actual base64 token, which we generated in the Authorization and Authentication section and execute the request. You will see no errors and the output will look something like the following screenshot:

    In the preceding screenshot, the MATCH request searched the complete database and returned all the nodes and their associated properties.

  6. Next, let's delete the data, which we searched in the preceding step. Open your tool and execute the following request and parameters for search, and then delete the data from the Neo4j database in a single Cypher statement:

    • Request method type: POST

    • Request URL: http://localhost:7474/db/data/transaction/commit

    • Request headers: Accept: application/json; charset=UTF-8 and Content-Type: application/json

    • JSON-REQUEST: {"statements": [{"statement" : "MATCH (n) delete n;"}]}

    • Header-Parameter: Authorization = Basic realm="Neo4j" <BASE64-ENCODED-TOKEN>

  7. Replace <BASE64-ENCODED-TOKEN> with the actual base64 token, which we generated in the Authorization and Authentication section, and execute the request. The response of the delete request will be same as the Create request.

In this section, we walked through the process of executing the Cypher queries with one of the REST endpoints, /db/data/transaction/commit, which is known as Transactional Cypher HTTP Endpoint. There are various other REST endpoints exposed by Neo4j for performing traversals, search, CRUD, administration, and a health check of the Neo4j server. Refer to http://neo4j.com/docs/stable/rest-api.html for a complete list of available endpoints, or you can also execute another REST point exposed by Neo4j, /db/data, which is known as the service root and the starting point to discover the REST API. It contains the basic starting points for the database along with some version and extension information.

Note

Linux users can also use the curl command to create and retrieve the data using the Neo4j REST APIs (http://neo4j.com/blog/the-neo4j-rest-server-part1-get-it-going/).

 

Running queries from the Neo4j browser


In the previous sections, we saw the results of our Cypher queries in the console (the Neo4j console) and JSON (REST) format, but both of these formats do not provide enough visualization. Also, as data grows, it becomes even more difficult to analyze the nodes and their relationships.

How about having a rich user interface for visualizing data in a graph format—a series of connected nodes? It will be awesome…correct?

Neo4j provides a rich graphical and interactive user interface for fetching and visualizing the Neo4j graph data, the Neo4j browser. The Neo4j browser not only provides the data visualization, but, at the same time, it also provides insights into the health of the Neo4j system and its configurations.

Let's perform the following steps for executing a Cypher search query from our Neo4j browser, and then visualize the data:

  1. Assuming that your Neo4j server is running, open any browser such as IE, Firefox, Mozilla, or Safari on the same system on which your Neo4j server is installed, and enter the URL, http://localhost:7474/browser in the browser navigation bar. Now press Enter.

  2. Next, enter the server username and password on the login screen (which we created/changed during the Neo4j installation), and click Submit.

  3. Now, click on the star sign in the panel on the extreme left-hand side, and click Create a node from the provided menu.

  4. Next, enter the following Cypher query to create data in the box provided below the browser's navigation bar (besides $): CREATE (movies:Movie {Name:"Noah", ReleaseYear:"2014"});. Now click on the right arrow sign at the extreme right corner, just below the browser's navigation bar.

  5. Click on Get some data from the panel on the left-hand side, and execute the following Cypher query to retrieve the data from the Neo4j database: "MATCH (n) return n;. You will see the following results:

And we are done! You can also execute the REST APIs by clicking on the REST API or see the relationships by clicking on What is related, and how.

There are many other rich, interactive, and intuitive features of the Neo4j browser. For the complete list of features, execute :play intro in the same window where you executed your Cypher query and it will walk you through the various features of the Neo4j browser.

 

Summary


In this chapter, we learned about the similarity and ease of learning Neo4j for SQL developers. We also went through the various licensing and step-by-step installation processes of various flavors of Neo4j. Finally, we also executed the CRUD operations using Cypher in the Neo4j shell, REST, and Neo4j browser.

In the next chapter, we will dive deep into the Cypher constructs pattern and pattern matching for querying Neo4j.

About the Author
  • Sumit Gupta

    Sumit Gupta is a seasoned professional, innovator, and technology evangelist with over 100 months of experience in architecting, managing, and delivering enterprise solutions revolving around a variety of business domains, such as hospitality, healthcare, risk management, insurance, and more. He is passionate about technology and has an overall hands-on experience of over 16 years in the software industry. He has been using big data and cloud technologies over the last 5 years to solve complex business problems. Sumit has also authored Neo4j Essentials, Building Web Applications with Python, and Neo4j, Real-Time Big Data Analytics, and Learning Real-time Processing with Spark Streaming, all by Packt. You can find him on LinkedIn at sumit1001.

    Browse publications by this author
Latest Reviews (1 reviews total)
Auf ein Ebook über 2 Wochen warten ist sehr frustrierend.
Building web applications with Python and Neo4j
Unlock this book and the full library FREE for 7 days
Start now