Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Building web applications with Python and Neo4j
Building web applications with Python and Neo4j

Building web applications with Python and Neo4j: Develop exciting real-world Python-based web applications with Neo4j using frameworks such as Flask, Py2neo, and Django

eBook
$26.99 $29.99
Paperback
$38.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Table of content icon View table of contents Preview book icon Preview Book

Building web applications with Python and Neo4j

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:

Comparing SQL and Cypher

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:

Comparing SQL and Cypher

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:

Evolving graph structures from SQL models

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:

Evolving graph structures from SQL models

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:

Evolving graph structures from SQL models

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/.
    Installing Neo4J Community Edition on Linux/Unix

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:
    Using the Neo4j shell

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=
    Authorization and authentication

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:
    CRUD operations

    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:
    CRUD operations

    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:
    Running queries from the Neo4j browser

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.

Left arrow icon Right arrow icon

Description

If you are a Python developer and want to expand your understanding of Python-based web applications over Neo4j graph data models, this is the book for you.

Who is this book for?

If you are a Python developer and want to expand your understanding of Python-based web applications over Neo4j graph data models, this is the book for you.

What you will learn

  • Understand the licensing and installation of the Neo4j database and work with its various tools and utilities
  • Learn the intricacies of Cypher as a graph query language
  • Work with Cypher to create and modify graph data models
  • Integrate Python and Neo4j using Py2neo
  • Develop RESTbased services over social network data using Flask and object graph models over Neo4j
  • Integrate Djangobased web applications over graph data models using Neomodel
  • Explore different deployment models and their applicability with existing applications
Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jul 16, 2015
Length: 184 pages
Edition : 1st
Language : English
ISBN-13 : 9781783983988
Languages :
Tools :

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

Publication date : Jul 16, 2015
Length: 184 pages
Edition : 1st
Language : English
ISBN-13 : 9781783983988
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 120.97
Neo4j Cookbook
$48.99
Building web applications with Python and Neo4j
$38.99
Neo4j Graph Data Modelling
$32.99
Total $ 120.97 Stars icon

Table of Contents

8 Chapters
1. Your First Query with Neo4j Chevron down icon Chevron up icon
2. Querying the Graph with Cypher Chevron down icon Chevron up icon
3. Mutating Graph with Cypher Chevron down icon Chevron up icon
4. Getting Python and Neo4j to Talk Py2neo Chevron down icon Chevron up icon
5. Build RESTful Service with Flask and Py2neo Chevron down icon Chevron up icon
6. Using Neo4j with Django and Neomodel Chevron down icon Chevron up icon
7. Deploying Neo4j in Production Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.3
(3 Ratings)
5 star 33.3%
4 star 66.7%
3 star 0%
2 star 0%
1 star 0%
Natester Sep 06, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I found this book quite helpful in learning Neo4j. I've not worked with graph databases before but needed to get up-to-speed with how graph databases work and I found the examples in the book help point me in the right direction. For any Python people looking to better understand graph databases (in this case, Neo4j,) I recommend this book.The first half of the book focuses on Neo4j fundamentals. Gupta's comparison to RMDB analogies were helpful to me coming from a RMDBS background.After about the first half of the book, the Python examples begin. (This makes sense considering the thrust of the book.) The web app examples for Python use the Flask package, which is great for getting up and running without having to delve too much into a framework.One other helpful area covered by the book is the py2neo package. There are quite a few things that can be done with the py2neo package, but the examples shared in this book can help get you pointed in the right direction with the py2neo package.
Amazon Verified review Amazon
Lets go! Sep 20, 2017
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Was able to build a web site using Python and several other tools suggested by the author. Fairly brief and too the point by highlighting the essential issues to getting the job done.
Amazon Verified review Amazon
Tim Crothers Sep 11, 2015
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
The book does a good job of covering how graph databases differ from relational databases and how to utilize them in Python based web applications via Flask and Neo4j. The book is clearly intended for experienced developers as it wastes no time on basics and drops right into the particulars of graph databases. Coverage of the breadth of each topic is also good. Overall a very good value for the money and solid treatment of the subject matter.I didn't give it five stars for a couple reasons. Firstly it reads much like a man page to me. The authors style feels very terse and I found myself having to reread fairly frequently to get the full gist of the content. This is unusual in my experience as I read dozens of technical books a year. The second reason is because of the lack of more robust examples. There are lots of examples but they tend to be very short. This lack of more complete examples coupled with the terse style meant it was a lot more work to really gain the underlying technology than I normally find.These couple difficulties aside the book really does a good job tackling a broad subject overall. Especially given the lack of books on this particular topic, if you have a need for using Neo4j from Python this book is a must have for the library.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the digital copy I get with my Print order? Chevron down icon Chevron up icon

When you buy any Print edition of our Books, you can redeem (for free) the eBook edition of the Print Book you’ve purchased. This gives you instant access to your book when you make an order via PDF, EPUB or our online Reader experience.

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
Modal Close icon
Modal Close icon