Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Neo4j Cookbook

You're reading from   Neo4j Cookbook Harness the power of Neo4j to perform complex data analysis over the course of 75 easy-to-follow recipes

Arrow left icon
Product type Paperback
Published in May 2015
Publisher
ISBN-13 9781783287253
Length 226 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Ankur Goel Ankur Goel
Author Profile Icon Ankur Goel
Ankur Goel
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Getting Started with Neo4j FREE CHAPTER 2. Connecting to Neo4j 3. The Cypher Query Language 4. Data Modeling and Leveraging with Neo4j 5. Mining the Social Treasure 6. Developing Location-based Services with Neo4j 7. Visualization of Graphs 8. Industry Usages of Neo4j 9. Neo4j Administration and Maintenance 10. Scaling Neo4j Index

Creating your first graph with Neo4j

After the successful setup of Neo4j on an operating system of our choice, now it's time to say Hello World to Neo4j, which means it's time to create our first graph by using Neo4j.

We know that any graph consists of nodes and edges, where edges represent the relationships between nodes.

Consider an example where there are two persons, Alice and Bob, who know each other. So, in graph terminology, Alice will be node A and Bob will be node B. The technical representation of this example can be done as follows:

  • Nodes: A and B
  • Edges: A----------- knows -------------B
    Creating your first graph with Neo4j

The preceding diagram shows nodes and edges, where edges represent the properties between the nodes.

Getting ready

To get started with this recipe, install Neo4j by using the earlier recipes of this chapter.

How to do it...

There are many ways to create a graph with Neo4j. However, in order to create our first graph, we will use the Neo4j shell that comes with Neo4j by default and can be intuitively operated from both the command line and the shell.

For our first graph, consider a scenario where London and Paris are two cities that are connected by the following flights:

  • Airline X, which connects London to Paris daily (start time: 1400 hours)
  • Airline Y, which connects Paris to London daily (start time: 2300 hours)

Let's gets started to create our first graph using the Neo4j shell. To do so, perform the following steps:

  1. Start the Neo4j server by using the following command:
    ${NEO4J_ROOT}/bin/neo4j start
    

    The detailed steps to start the Neo4j server has been described in the previous recipes.

  2. The Neo4j shell can be invoked by two methods. The first method is to simply type in the following command (under the same <neo4J_Home_Directory>/bin directory):
    ${NEO4J_ROOT}/bin/neo4j-shell
    

    The output of this command is shown as follows:

    How to do it...

    The nodes are created using the mknode command as follows:

    neo4j-sh (0) $ mknode London
    neo4j-sh (0) $ mknode Paris
    
  3. Let's create a node and enter this node by using the cd option with mknode:
    neo4j-sh (0) $ mknode --cd --np "{'name':'London'}"
    

    The np option can be used to specify as many properties as you want with that node.

  4. Now, we will create another node with the name Paris:
    neo4j-sh (0) $ mknode  --np "{'name':Paris}" -v
    
  5. Next, we will create a relationship between them by executing the following commands from the command line:
    neo4j-sh (London,2)$  mkrel -d OUTGOING -t CONNECTED <nodeid from preceding command> --rp "{'Airline':'X','Start-Time':'1400'}"
    neo4j-sh (London,2)$  ls
    *name =[London]
    (me)-[:CONNECTED]->(Paris,3)
    

    The mkrel command is used to create a relationship. To see the options in detail, type man mkrel in the Neo4j shell.

    Let's create another relationship, as demonstrated by the following commands:

    neo4j-sh (London,2)$  mkrel -d INCOMING -t CONNECTED <nodeid> --rp "{'Airline':'Y','Start-Time':'2300'}"
    neo4j-sh (London,2)$  cd 3
    neo4j-sh (Paris,3)$  ls
    *name =[Paris]
    (me)<-[:CONNECTED]-(London,2)
    
  6. Let's visualize our first graph in the browser. For this, go to the Neo4j webadmin URL and then click on Data Browser; you will see something similar to the following screenshot:
    How to do it...

We can see two nodes, 2 and 3, in the data visualization, which are connected to each other.

How it works...

The Neo4j shell comes with the handy utilities of mknode to create new nodes with properties and with mkrel to create relationships among them.

Nodes in Neo4j are analogous to files in the Unix filesystem, except with one major difference. The difference is that when you create a file in any directory, a relationship automatically gets created between the parent directory and the file. Using this relationship, we can browse the filesystem, whereas mknode in Neo4j creates disjointed nodes that cannot be browsed, as they don't have any relationship between them.

There's more…

To study more about the mknode and mkrel commands, use the man pages under the Neo4j shell. If you want to delete an entire graph that you have just created, the following are the steps to do so:

  1. Stop the Neo4j server by using the following command:
    $ ./neo4j stop
  2. Delete the graph.db file under the data directory (assuming that you are using the default configuration):
    $ rm –rf data/graph.db

    Once deleted, the data is not recoverable.

You have been reading a chapter from
Neo4j Cookbook
Published in: May 2015
Publisher:
ISBN-13: 9781783287253
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at AU $24.99/month. Cancel anytime