Neo4j數據庫簡介
Neo4j 是一個NoSQL的圖形數據庫(Graph Database)。Neo4j使用圖(graph)相關的概念來描述數據模型,把數據保存為圖中的節點以及節點之間的關系。很多應用中數據之間的關系,可以很直接地使用圖中節點和關系的概念來建模。對於這樣的應用,使用 Neo4j 來存儲數據會非常的自然,要優於使用關系數據庫。官方文檔對Neo4j的介紹如下所示:
Neo4j is the world’s leading graph database. It is built from the ground up to be a graph database,
meaning that its architecture is designed for optimizing fast management, storage, and traversal of
nodes and relationships. Therefore, relationships are said to be first class citizens in Neo4j. An
operation known in the relational database world as a join exhibits performance which degrades
exponentially with the number of relationships. The corresponding action in Neo4j is performed as
navigation from one node to another; an operation whose performance is linear.
Neo4j分社區版(Community Edition)和企業版(Enterprise Edition),社區版本為免費版本。目前的最新版本為Neo4j 3.1。官方網址為https://neo4j.com/ Neo4j的相關文檔URL地址如下
https://neo4j.com/docs/developer-manual/3.1/
https://neo4j.com/docs/operations-manual/3.1/
https://neo4j.com/docs/developer-manual/3.1/cypher/
System requirements
Neo4j的安裝對硬件、系統等有一些要求,例如CPU、Memory、Disk、Filesystem、Software等
CPU
關於CPU,最低要求是Itel Core i3 推薦使用Intel Core i7 或 IBM POWER 8
Memory
關於Memory,最低要求是2GB, 推薦是16-32GB 或更多。 越大的內存允許處理越大的圖,但必須正確配置,以免破垃圾收集壞性操作。
Disk
除了Disk的容量,Disk的性能對Neoj4的影響非常大,Neo4j的工作負載傾向於隨機讀。選擇平均尋址時間低的存儲,例如SSD
最低要求 10GB SATA
推薦配置 SSD w/SATA
FileSystem
最低要求 ext4(或類似的)
推薦配置 ext4、ZFS
Software
Java
必須安裝OpenJDK 8 或 Oracle Java 8
Operation Sytem
Linux(Ubuntu, Debian) ,但是我的測試環境為Red Hat Enterprise Linux Server release 6.6,暫時還沒有發現什么問題。
Architectures
X86 或 OpenPower(POWER8)
Neo4j安裝步驟:
下面簡單介紹Linux下Neo4j 3.1的安裝步驟,安裝測試環境為Red Hat Enterprise Linux Server release 6.6, Debian下安裝具體參考文檔,這里不做介紹。
1:下載Neo4j 3.1的安裝包,去官方網址注冊下載即可。選擇特定的版本。下載地址 https://neo4j.com/download/
2:檢查上面System Requirement,安裝JDK 8
[root@gettestlnx03 ~]# rpm -qa | grep gcj
libgcj-devel-4.4.7-11.el6.x86_64
libgcj-4.4.7-11.el6.x86_64
java-1.5.0-gcj-1.5.0.0-29.1.el6.x86_64
[root@gettestlnx03 ~]# rpm -ivh /tmp/jdk-8u121-linux-x64.rpm
Preparing... ########################################### [100%]
1:jdk1.8.0_121 ########################################### [100%]
Unpacking JAR files...
tools.jar...
plugin.jar...
javaws.jar...
deploy.jar...
rt.jar...
jsse.jar...
charsets.jar...
localedata.jar...
[root@gettestlnx03 ~]# java -version
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
[root@gettestlnx03 ~]#
編輯/etc/profile文件,添加下面配置信息,然后執行source /etc/profile命令使之生效。
export JAVA_HOME=/usr/java/jdk1.8.0_121/
export CLASSPATH=/usr/java/jdk1.8.0_121/lib
export PATH=$AVA_HOME/bin:$PATH
3:創建一個用戶neo4j,用來管理Neo4j 。當然這不是必須的。也可以是其它賬號或root賬號。
[root@gettestlnx03 ~]# /usr/sbin/groupadd nosql
[root@gettestlnx03 ~]# /usr/sbin/useradd -G nosql neo4j
4:然后切換到neo4j賬戶下,解壓neo4j-community-3.1.1-unix.tar.gz包
[root@gettestlnx03 ~]# su - neo4j
[neo4j@gettestlnx03 ~]$ tar -zxvf /tmp/neo4j-community-3.1.1-unix.tar.gz -C /home/neo4j/
配置.bash_profile文件,增加下面一行,並使之生效。
[root@gettestlnx03 neo4j]# su - neo4j
[neo4j@gettestlnx03 ~]$ vi .bash_profile
export PATH=/home/neo4j/neo4j-community-3.1.1/bin:$PATH #新增一行
[neo4j@gettestlnx03 ~]$ source .bash_profile
另外,修改配置文件/etc/security/limits.conf ,增加下面配置。
neo4j soft nofile 40000
neo4j hard nofile 40000
The usual default of 1024 is often not enough. This is especially true when many indexes are used or a
server installation sees too many connections. Network sockets count against the limit as well. Users
are therefore encouraged to increase the limit to a healthy value of 40 000 or more, depending on
usage patterns. It is possible to set the limit with the ulimit command, but only for the root user, and
it only affects the current session. To set the value system wide, follow the instructions for your
platform.
編輯/etc/pam.d/su 在配置文件里面增加選項。
session optional pam_xauth.so
Neo4j服務關閉啟動
Neo4j服務的啟動、關閉如下所示
[neo4j@gettestlnx03 ~]$ neo4j start
Starting Neo4j.
Started neo4j (pid 2605). By default, it is available at http://localhost:7474/
There may be a short delay until the server is ready.
See /home/neo4j/neo4j-community-3.1.1/logs/neo4j.log for current status.
[neo4j@gettestlnx03 ~]$ neo4j status
Neo4j is running at pid 2605
[neo4j@gettestlnx03 ~]$ neo4j stop
Stopping Neo4j.. stopped
[neo4j@gettestlnx03 ~]$
如果不設置Neo4j的配置文件,只能從本機訪問Neo4J的web控制台(http://localhost:7474/) Neo4j的控制文件一般位於<neo4jhome>/conf/neo4j.conf,例如我測試環境為/home/neo4j/neo4j-community-3.1.1/conf
Neo4j的配置文件將單獨再做詳細介紹。如果我們需要遠程訪問Neo4j的Web控制台,我們需要修改neo4j.conf配置文件的參數,如下截圖所示:
當然,還需要配置iptables,開放7474端口號,否則你還是無法遠程訪問Web控制台。
-A INPUT -p tcp -m state --state NEW -m tcp --dport 7474 -j ACCEPT
然后你就可以從遠程http://xxx.xxx.xxx.xxx:7474 訪問Neo4j的Web控制台了, 第一次登陸會提示你輸入初始化密碼並提醒你修改密碼。默認的密碼為neo4j. 如果允許遠程訪問,切記修改初始化密碼。安全非常重要。
簡單的測試案例
我們先創建一個簡單的父子關系圖。用來簡單演示一下圖形數據庫的使用。如下所示。
[neo4j@gettestlnx03 bin]$ ./cypher-shell
username: neo4j
password: **********
Connected to Neo4j 3.1.1 at bolt://localhost:7687 as user neo4j.
Type :help for a list of available commands or :exit to exit the shell.
Note that Cypher queries must end with a semicolon.
neo4j> CREATE (A {id:1,name:'張飛'}), (B {id:2,name:'張小二'}), (C {id:3,name:'張三'}), (D {id:4,name:'張凱'}),(E {id:5,name:'張雨'}),(A)-[:Son]->(B),(A)-[:Son]->(C),(B)-[:Son]->(D),(C)-[:Son]->(E);
Added 5 nodes, Created 4 relationships, Set 10 properties
neo4j> START n=node(*) RETURN n;
n
({name: "張飛", id: 1})
({name: "張小二", id: 2})
({name: "張三", id: 3})
({name: "張凱", id: 4})
({name: "張雨", id: 5})
neo4j>

如果你在Web控制台執行命令,就能得到如下一個圖形關系。如下所示。
參考資料:
https://neo4j.com/docs/developer-manual/3.1/