Skip to content
Ronak Nathani edited this page Aug 30, 2016 · 1 revision

Native graph databases provide a flexible and efficient way to store and query complex graph relations. The tutorial below was executed on an Ubuntu Server 14.04 LTS.

Table of Contents

  1. Requirements

  2. Installation

  3. Web interface

  4. Cpyher

4.1. Load data

4.2. Example query

### Requirements
  • Java8
### Installation

SSH into the node

localhost$ ssh -i ~/.ssh/personal-aws.pem ubuntu@namenode-public-dns
Install Java
ubuntu@<public-ip>:~$ sudo apt-get update
ubuntu@<public-ip>:~$ sudo add-apt-repository ppa:webupd8team/java
ubuntu@<public-ip>:~$ sudo apt-get update && sudo apt-get install oracle-java7-installer
ubuntu@<public-ip>:~$ java -version 
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)
Install Neo4j 3.0 Community Edition
ubuntu@<public-ip>:~$ wget -O - http://debian.neo4j.org/neotechnology.gpg.key >> key.pgp
ubuntu@<public-ip>:~$ sudo apt-key add key.pgp
ubuntu@<public-ip>:~$ echo 'deb http://debian.neo4j.org/repo stable/' | sudo tee -a /etc/apt/sources.list.d/neo4j.list > /dev/null
ubuntu@<public-ip>:~$ sudo apt-get update && sudo apt-get install neo4j
Modify some settings

Edit /etc/security/limits.conf and add these two lines:

root   soft    nofile  40000
root   hard    nofile  40000
neo4j   soft    nofile  40000
neo4j   hard    nofile  40000

Edit /etc/pam.d/su and uncomment the following line:

session    required   pam_limits.so

Edit /etc/neo4j/neo4j.conf and uncomment the following line:

dbms.connector.http.address=0.0.0.0:7474

Restart the machine:

ubuntu@<public-ip>:/$ sudo reboot

SSH back and restart Neo4j:

ubuntu@<public-ip>:/$ sudo /usr/bin/neo4j restart
### Web interface

Open <public-ip>:7474/browser/ in a borwser.

If you connect to Neo4j for the first time, you’re asked to change the password for the default user account “neo4j/neo4j”.

You can type commands into the command line on the top.

### Cpyher

Neo4j's query language allows us to describe and query complex graph relations.

#### Load data from a CSV file

Create the file friendships.csv and add the following lines to it:

1,2
1,3
1,4
2,3
5,6
6,7
7,5

Create the import folder and copy this csv file there.

sudo mkdir /usr/share/neo4j/import

1. Via web interface

For up to medium sized files we can use the web interface to import the graph. Copy the following code to Neo4j's command line:

  LOAD CSV FROM 'file:///friendships.csv' AS line
  MERGE (n:person {user_id : line[0]})
  WITH line, n
  MERGE (m:person {user_id : line[1]})
  WITH m,n
  MERGE (n)-[:FRIEND]-(m);
  

Click on the Database icon on the left and then on the Node labels to visualize the graph.

2. Via command line

#### Example query

As an example let's calculate the shortes path distance between 2 users.

Type the following to Neo4j's command line:

MATCH (u1:person { user_id: "1"}),(u2: person { user_id: "4" }),
p = shortestPath((u1)-[*..150]-(u2)) RETURN length(p) as length