Neo4j做爲一個圖數據庫,其名字的由來其實與其在底層的存儲方式有關,Neo4j底層會以圖的方式把用戶定義的節點以及關係存儲起來,經過這種方式,但是高效的實現從某個節點開始,經過節點與節點間關係,找出兩個節點間的聯繫。
目前Neo4j常見於如下使用場景:
社交網絡
基於圖的搜索
推薦引擎
企業基礎設施及網絡架構
如:天眼查網站各企業之間關聯關係圖等。
本文重點介紹HA環境的部署,在一臺服務器上模擬三個節點。實際生產環境可分開在不一樣的服務器部署,配置文件按照不一樣的IP及端口作相應的修改便可。php
[root@appcan-t-app-7 ~]# more /etc/profile export JAVA_HOME=/usr/local/jdk1.8.0_60 export JRE_HOME=${JAVA_HOME}/jre export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib export PATH=${JAVA_HOME}/bin:$PATH [root@appcan-t-app-7 ~]# java -version java version "1.8.0_60" Java(TM) SE Runtime Environment (build 1.8.0_60-b27) Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)
wget https://neo4j.com/artifact.php?name=neo4j-community-3.4.9-unix.tar.gz (當前最新版本,可去網站選擇適用版本)
tar xf neo4j-community-3.4.9-unix.tar.gz -C /usr/local/ cd /usr/local/neo4j-community-3.4.9-unix/ ./bin/neo4j-installer install systemctl status neo4j-service
經過修改conf/neo4j.properties和conf/neo4j-server.properties文件完成HA配置。html
(1)conf/neo4j.properties: ## ha.server_id = 1 ha.server = 127.0.0.1:6363 online_backup_server = 127.0.0.1:6366 ha.cluster_server = 127.0.0.1:5001 ha.initial_hosts = 127.0.0.1:5001,127.0.0.1:5002,127.0.0.1:5003 ***************************************************************** ***************************************************************** (2)conf/neo4j-server.properties ## org.neo4j.server.database.location=data/graph.db org.neo4j.server.webserver.port=7474 org.neo4j.server.webserver.address=0.0.0.0 org.neo4j.server.webserver.https.port=7484 org.neo4j.server.database.mode=HA
(1)conf/neo4j.properties: ## ha.server_id = 2 ha.server = 127.0.0.1:6364 online_backup_server = 127.0.0.1:6367 ha.cluster_server = 127.0.0.1:5002 ha.initial_hosts = 127.0.0.1:5001,127.0.0.1:5002,127.0.0.1:5003 ***************************************************************** ***************************************************************** (2)conf/neo4j-server.properties ## org.neo4j.server.database.location=data/graph.db org.neo4j.server.webserver.port=7475 org.neo4j.server.webserver.address=0.0.0.0 org.neo4j.server.webserver.https.port=7485 org.neo4j.server.database.mode=HA
(1)conf/neo4j.properties: ## ha.server_id = 3 ha.server = 127.0.0.1:6365 online_backup_server = 127.0.0.1:6368 ha.cluster_server = 127.0.0.1:5003 ha.initial_hosts = 127.0.0.1:5001,127.0.0.1:5002,127.0.0.1:5003 ***************************************************************** ***************************************************************** (2)conf/neo4j-server.properties ## org.neo4j.server.database.location=data/graph.db org.neo4j.server.webserver.port=7476 org.neo4j.server.webserver.address=0.0.0.0 org.neo4j.server.webserver.https.port=7486 org.neo4j.server.database.mode=HA
依次啓動:
./bin/neo4j start
檢測:
#jps
17131 CommunityBootstrapper
17324 CommunityBootstrapper
16959 CommunityBootstrapperjava
WEB界面展現:
web
插入一個Person類別的節點,且這個節點有一個屬性name,屬性值爲Andres數據庫
CREATE (n:Person { name : 'Andres'});
插入一條a到b的有向邊,且邊的類別爲Follow服務器
MATCH (a:Person),(b:Person) WHERE a.name = 'Node A' AND b.name = 'Node B' CREATE (a)-[r:Follow]->(b);
更新一個Person類別的節點,設置新的name。網絡
MATCH (n:Person { name: 'Andres' }) SET n.name = 'Taylor';
Neo4j中若是一個節點有邊相連,是不能單單刪除這個節點的。架構
MATCH (n:Person { name:'Andres' }) DETACH DELETE n;
MATCH (a:Person)-[r:Follow]->(b:Person) WHERE a.name = 'Andres' AND b.name = 'Taylor' DELETE r;
最短路徑:app
MATCH (ms:Person { name:'Andres' }),(cs:Person { name:'Taylor' }), p = shortestPath((ms)-[r:Follow]-(cs)) RETURN p;
查詢兩個節點之間的關係:ide
MATCH (a:Person { name:'Andres' })-[r]->(b:Person { name:'Taylor' }) RETURN type(r);
查詢一個節點的全部Follower:
MATCH (:Person { name:'Taylor' })-[r:Follow]->(Person) RETURN Person.name;
建立惟一性約束,同時具備索引效果,相似關係型數據庫中的主鍵。注意,這隻能在數據庫中還未插入數據時設置。
CREATE CONSTRAINT ON (a:Person) ASSERT a.name IS UNIQUE;
建立索引:
CREATE INDEX ON :Person(name);
查看全部的節點數和邊數:
MATCH (n) RETURN count(n); MATCH ()-->() RETURN count(*);
一、http://www.javashuo.com/article/p-yovduyve-gn.html
二、http://www.cnblogs.com/rubinorth/p/5853204.html
三、https://neo4j.com/docs/2.0.2/ha-setup-tutorial.html