Introduction to Graph Database Neo4j in Python for Beginners (for Mac OS X)

I'm a beginner myself, so I'm sorry to say "Introduction to Graph Database Neo4j in Python for Beginners". Introducing the environment construction to operate Neo4j with Python and the sample to play with the data.

1. Introduction of Neo4j

Let's start with how to deploy Neo4J on Mac OS X. Since my environment is Yosemite 10.10.2, I would be grateful if you could let me know in the comments etc. if there are any errors due to the environment difference.

First of all, you need a JDK, but it seems that it does not work well with Java that is included in the Mac from the beginning, so Oracle JDK 7 http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260. Introduce from html.

next, http://neo4j.com/download/ Go to and download Neo4j's Community Edition (free version).

neo4j-community-2.2.0-unix-2.tar will be downloaded, so unzip it and put it in the folder.

tar zvf neo4j-community-2.2.0-unix-2.tar
cd neo4j-community-2.2.0

Then start it.

./bin/neo4j start

It's very easy to install! There is a user interface that can be accessed from a browser, so let's try using it. The access destination is http://localhost:7474/browser/ is. You will be asked to set the ID and Password at the first startup, so set them appropriately.

2. Connect to Neo4j with Python

There seems to be some Python libraries that can connect to Neo4j, but here I decided to use Neo4j RestClient to connect from Python to Neo4j. I will.

pip install neo4jrestclient

pip is great, it's really easy to install! : satisfied:

Follow the neo4j RestClient Tutorial to try it out.

from neo4jrestclient.client import GraphDatabase

url = "http://<User ID>:<Password>@localhost:7474/db/data/"
gdb = GraphDatabase(url)

In the and fields, write the one you set earlier. This will connect you to Neo4j from Python.

Next, add two Nodes. They are alice and bob of the same age.

alice = gdb.nodes.create(name="Alice", age=30)
bob = gdb.nodes.create(name="Bob", age=30)

Actually, bob knew about alice from 1980, but alice knew bob three years later ... If you add a node called, it will be as follows.

bob.relationships.create("Knows", alice, since=1980)
alice.relationships.create("Knows", bob, since=1983)

Let's display this. (Proceed on the assumption that python is running on iPython notebook)

Hit the query to display all nodes including node-to-node relationships. Also, setting data_contents = True is the key, and it will not work well without this. (I took a little time without knowing this ...)

gdb.query("MATCH (n)-[r]-(m) RETURN n, r, m", data_contents=True)

neo4j_001-compressor.png The graph is displayed!

To see this in your browser, go to http: // localhost: 7474 / browser /

MATCH (n)-[r]-(m) RETURN n, r, m

neo4j_002-compressor.png

And press Enter neo4j_003.png The graph is displayed: grinning:

2. Play with the data

Delete the data up to that point once.

# All Delete
gdb.query("MATCH (n) OPTIONAL MATCH (n)-[r]-() DELETE n,r", data_contents=True)

Then add 3 Nodes and Relationships.

#Add Person Node
alice = gdb.nodes.create(name="Alice", age=30)
bob   = gdb.nodes.create(name="Bob", age=30)
ken   = gdb.nodes.create(name="Ken", age=35)

alice.labels.add("Person")
bob.labels.add("Person")
ken.labels.add("Person")

#Relationship settings
bob.relationships.create("Knows", alice, since=1980)
alice.relationships.create("Knows", bob, since=1983)
alice.relationships.create("Knows", ken, since=2015)

Node refers to each person here, and Relationship refers to the relationship that Alice knows Bob.

neo4j_004-compressor-2.png

When you hit the display query on iPython,

gdb.query("MATCH (n)-[r]-(m) RETURN n, r, m", data_contents=True)

neo4j_005-compressor.png

A similar graph is displayed: D

Let's take a look at the graph by defining the person who writes next has a blog and the relationship with that blog as blog owner: "Owner" and subscriber: "Subscribe".

#Add Blog Node
cam_blog  = gdb.nodes.create(name="Camera Blog")
comp_blog = gdb.nodes.create(name="Computer Blog")
trav_blog = gdb.nodes.create(name="Travel Blog")
gour_blog = gdb.nodes.create(name="Gourmet Blog")

cam_blog.labels.add("Blog")
comp_blog.labels.add("Blog")
trav_blog.labels.add("Blog")
gour_blog.labels.add("Blog")

#Add Relation
alice.relationships.create("Own", cam_blog)
bob.relationships.create("Own", comp_blog)
ken.relationships.create("Own", trav_blog)

alice.relationships.create("Subscribe", trav_blog)
alice.relationships.create("Subscribe", gour_blog)
bob.relationships.create("Subscribe", cam_blog)
ken.relationships.create("Subscribe", comp_blog)

Also, if you access http: // localhost: 7474 / browser / with a browser and throw the following query,

MATCH (n)-[r]-(m) RETURN n, r, m

A graph like this is displayed. I think it's pretty easy to understand. That's why it is said that graph databases are easier to understand relationships than relational databases. neo4j_006.png

3. Cypher First Step

Let me give you some examples of the basic writing of Cypher.
The following Bob, Alice, Ken indicated by red circles are selected.

match (n:Person) RETURN n

neo4j_007-compressor.png (* The red frame is added for clarity and does not appear on the Neo4J browser screen.)

This time the Blog node is selected.

match (n:Blog) RETURN n

neo4j_008-compressor.png

This Cypher query extracts only Bob.

MATCH (b:Person {name:'Bob'}) RETURN b

neo4j_009-compressor.png

Select the relationship as well. In the following cases, only those related to Own are applicable.

MATCH (p:Person)-[r:Own]->(b:Blog) RETURN p,r,b;

neo4j_011-compressor.png

Finally, randomly generate 100 Person nodes and 100 Blog nodes, and generate and display a graph with 500 Subscribe relationships.

import numpy.random as rd

l = 100

person_list = []
blog_list = []
for i in range(l):
    
    p = gdb.nodes.create(name="person_%d"%i)
    p.labels.add("Person")
    
    b  = gdb.nodes.create(name="Blog_%d"%i)
    b.labels.add("Blog")
    
    person_list.append(p)
    blog_list.append(b)

r1 = range(len(person_list))
rd.shuffle(r1)

for i in range(len(blog_list)):
    blog_list[i].relationships.create("Own", person_list[r1[i]])


r2 = range(l) * 5
rd.shuffle(r2)
r3 = range(l) * 5
rd.shuffle(r3)
                
for i,j in zip(r2, r3):
    person_list[i].relationships.create("Subscribe", blog_list[j])

neo4j_012-compressor.png

It's chaos! : grin:

3. Draw with Networkx

Install iCypher.

pip install ipython-cypher

You can graph it with networkx with the code below.

neo4j-networkx.py


%load_ext cypher
%matplotlib inline
import networkx as nx
import matplotlib.pyplot as plt

result = %%cypher http://<User ID>:<Password>@localhost:7474/db/data/ MATCH (n)-[r]-(m) RETURN n,r,m;
node_map ={'Person':'#22FF99', 'Blog': '#6622FF' }
node_label={'Person':'Person', 'Blog': 'Blog' }

g = result.get_graph()
pos=nx.get_node_attributes(g,'pos')
plt.figure(figsize=(15,15))
nx.draw(g, node_color=[node_map[g.node[n]['labels'][0]] for n in g],node_size=80, width=0.5, edge_color='#999999')

neo4j_014-compressor.png

Recommended Posts

Introduction to Graph Database Neo4j in Python for Beginners (for Mac OS X)
[Python] Introduction to graph creation using coronavirus data [For beginners]
Introduction to Programming (Python) TA Tendency for beginners
[Introduction for beginners] Working with MySQL in Python
[For beginners] Introduction to vectorization in machine learning
How to Introduce IPython (Python2) to Mac OS X-Preparation for Introduction to Machine Learning Theory-
Building a Python environment for programming beginners (Mac OS)
Introduction to Python For, While
I tried to build an environment for machine learning with Python (Mac OS X)
[Explanation for beginners] Introduction to convolution processing (explained in TensorFlow)
[Explanation for beginners] Introduction to pooling processing (explained in TensorFlow)
An introduction to Python for non-engineers
Introduction to Python Django (2) Mac Edition
[Introduction to Python] How to use the in operator in a for statement?
[For beginners] How to register a library created in Python in PyPI
Steps to get Caffe into Mac OS X 10.10 in CPU Mode
[Introduction to Python] How to use class in Python?
How to erase Python 2.x on Mac.
Beginners read "Introduction to TensorFlow 2.0 for Experts"
An introduction to Python for machine learning
Introduction to Vectors: Linear Algebra in Python <1>
Introduction to Effectiveness Verification Chapter 1 in Python
An introduction to Python for C programmers
Create a Python development environment in 10 minutes (Mac OS X + Visual Studio Code)
Introduction to Deep Learning (1) --Chainer is explained in an easy-to-understand manner for beginners-
Memo # 4 for Python beginners to read "Detailed Python Grammar"
Data analysis in Python Summary of sources to look at first for beginners
The fastest way for beginners to master Python
An introduction to object-oriented programming for beginners by beginners
Python for super beginners Python for super beginners # Easy to get angry
Introduction to effectiveness verification Chapter 3 written in Python
tse --Introduction to Text Stream Editor in Python
From file to graph drawing in Python. Elementary elementary
I wrote "Introduction to Effect Verification" in Python
Memo # 1 for Python beginners to read "Detailed Python Grammar"
Run Zookeeper x python (kazoo) on Mac OS X
[Introduction to Udemy Python3 + Application] 43. for else statement
Python beginners tried it in 3 days from OS installation to running Twitter API
Memo # 7 for Python beginners to read "Detailed Python Grammar"
Memo # 6 for Python beginners to read "Detailed Python Grammar"
Shpinx (Python documentation builder) on Mac OS X
How to make Python faster for beginners [numpy]
Memo # 5 for Python beginners to read "Detailed Python Grammar"
Run Qiita API v2 Python wrapper in Python3 environment (Mac OS X 10.11 (El Capitan))
Basic story of inheritance in Python (for beginners)
Understand Python for Pepper development. -Introduction to Python Box-
[For beginners in competition professionals] I tried to solve 40 AOJ "ITP I" questions with python
[For beginners] Web scraping with Python "Access the URL in the page to get the contents"
Output the specified table of Oracle database in Python to Excel for each file
Rock-paper-scissors poi in Python for beginners (answers and explanations)
Tool to make mask image for ETC in Python
I tried to graph the packages installed in Python
How to convert Python # type for Python super beginners: str
Preparing to use aws cli on Mac OS X
[For beginners] How to study Python3 data analysis exam
How to run python in virtual space (for MacOS)
Building an environment for "Tello_Video" on Mac OS X
Very easy to install SciPy on Mac OS X
[Introduction to python] A high-speed introduction to Python for busy C ++ programmers
mac OS X 10.15.x pyenv Python If you can't install
How to exit when using Python in Terminal (Mac)