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/