前言java
「圖」一直是計算機算法研究的重要領域,但因爲圖的複雜性缺乏成熟的產品。直到社交網絡的普及,社會化關係網和複雜網絡的更入研究,你們對於「圖數據庫」有了更強烈的需求。圖數據庫產品也趨向成熟,慢慢地走進了你們的視野。node
Neo4j就是圖數據庫的表明之做。程序員
關於做者:web
張丹(Conan), 程序員Java,R,PHP,Javascript算法
weibo:@Conan_Zsql
blog: http://blog.fens.meshell
email: bsspirit@gmail.com數據庫
轉載請註明出處:
http://blog.fens.me/nosql-neo4j-intro/json
目錄瀏覽器
Neo4j簡介
Neo4j單機安裝
建立一個簡單的社交關係圖
Neo4j集羣安裝HA
Neo4j是一個用Java實現的、高性能的、NoSQL圖形數據庫。Neo4j 使用圖(graph)相關的概念來描述數據模型,經過圖中的節點和節點的關係來建模。Neo4j徹底兼容ACID的事務性。Neo4j以「節點空間」來表達領域數據,相對於傳統的關係型數據庫的表、行和列來講,節點空間能夠更好地存儲由節點關係和屬性構成的網絡,如社交網絡,朋友圈等。
由Neo4j構建「圖」模型,也能夠準確表達 數據庫模型,key-value模型,文檔模型的數據關係。
系統環境
Linux: Ubuntu 12.04.2 LTS 64bit Server
SUN Java: 1.6.0_29 64-Bit
下載neo4j-enterprise,企業版
~ wget http://dist.neo4j.org/neo4j-enterprise-1.9.4-unix.tar.gz ~ tar xvf neo4j-enterprise-1.9.4-unix.tar.gz ~ mv neo4j-enterprise-1.9.4 neo4j194 ~ mv neo4j194/ /home/conan/toolkit/ ~ cd /home/conan/toolkit/neo4j194
配置Neo4j服務器容許遠程訪問,修改neo4j-server.properties
~ vi conf/neo4j-server.properties #取消註釋 org.neo4j.server.webserver.address=0.0.0.0
啓動Neo4j
~ bin/neo4j start WARNING: Max 1024 open files allowed, minimum of 40 000 recommended. See the Neo4j manual. WARNING! You are using an unsupported version of the Java runtime. Please use Oracle(R) Java(TM) Runtime Environment 7. Using additional JVM arguments: -server -XX:+DisableExplicitGC -Dorg.neo4j.server.properties=conf/neo4j-server.properties -Djava.util.logging.config.file=conf/logging.properties -Dlog4j.configuration=file:conf/log4j.properties -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled Starting Neo4j Server...WARNING: not changing user process [2408]... waiting for server to be ready....... OK. Go to http://localhost:7474/webadmin/ for administration interface.
經過瀏覽器打開Neo4j的web控制檯, http://192.168.1.201:7474/webadmin/
很是簡單地,完成了單機的Neo4j的安裝。
~ bin/neo4j-shell neo4j-sh (?)$ CREATE (A {id:1,name:'A'}), (B {id:2,name:'B'}), (C {id:3,name:'C'}), (D {id:4,name:'D'}),(A)-[:knows]->(B),(A)-[:knows]->(C),(B)-[:knows]->(D),(D)-[:knows]->(A); +-------------------+ | No data returned. | +-------------------+ Nodes created: 4 Relationships created: 4 Properties set: 8 neo4j-sh (?)$ START n=node(*) RETURN n; +-------------------------+ | n | +-------------------------+ | Node[49]{name:"A",id:1} | | Node[50]{name:"B",id:2} | | Node[51]{name:"C",id:3} | | Node[52]{name:"D",id:4} | +-------------------------+ 4 rows
經過控制檯以圖形展現
刪除數據節點和關係
neo4j-sh (?)$ START n=node(*) MATCH n-[r]-() DELETE n, r; +-------------------+ | No data returned. | +-------------------+ Nodes deleted: 4 Relationships deleted: 4 neo4j-sh (?)$ START n=node(*) RETURN n; +---+ | n | +---+ +---+ 0 row
模擬Neo4j的高可用:Neo4j企業版本才提供高可用性功能。
Neo4j HA主要提供如下兩個功能:
容錯數據庫架構 保存多個數據副本,即便硬件故障,也能保證可讀寫。
水平方向擴展以讀爲主架構 讀操做負載均衡。
Neo4j HA模式總有單個master,零個或多個slave。與其餘ms複製架構,Neo4j HA的slave能夠處理寫操做,而無需重定向寫入到master。
Neo4j的集羣須要複製多份Neo4j的環境,咱們這裏準備構建3個節點
~ mkdir /home/conan/neo4j ~ cd /home/conan/neo4j ~ cp -R /home/conan/toolkit/neo4j194 /home/conan/neo4j ~ mv neo4j194/ n1 ~ cp -R n1 n2 ~ cp -R n1 n3 ~ ls n1 n2 n3
分別修改各節點的配置文件
neo4j.properties
neo4j-server.properties
neo4j-server.properties
n1節點
~ vi n1/conf/neo4j.properties ha.server_id=1 ha.server=127.0.0.1:6361 online_backup_server=127.0.0.1:6362 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 ~ vi n1/conf/neo4j-server.properties org.neo4j.server.webserver.port=7474 org.neo4j.server.webserver.https.port=7473 org.neo4j.server.database.mode=HA ~ vi n1/conf/neo4j-wrapper.conf wrapper.java.additional.4=-Dcom.sun.management.jmxremote.port=3637 wrapper.java.additional.5=-Dcom.sun.management.jmxremote.password.file=conf/jmx.password wrapper.java.additional.6=-Dcom.sun.management.jmxremote.access.file=conf/jmx.access
n2節點
~ vi n2/conf/neo4j.properties ha.server_id=2 ha.server=127.0.0.1:6363 online_backup_server=127.0.0.1:6364 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 ~ vi n2/conf/neo4j-server.properties org.neo4j.server.webserver.port=7476 org.neo4j.server.webserver.https.port=7475 org.neo4j.server.database.mode=HA ~ vi n1/conf/neo4j-wrapper.conf wrapper.java.additional.4=-Dcom.sun.management.jmxremote.port=3638 wrapper.java.additional.5=-Dcom.sun.management.jmxremote.password.file=conf/jmx.password wrapper.java.additional.6=-Dcom.sun.management.jmxremote.access.file=conf/jmx.access
n3節點
~ vi n3/conf/neo4j.properties ha.server_id=3 ha.server=127.0.0.1:6365 online_backup_server=127.0.0.1:6366 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 ~ vi n3/conf/neo4j-server.properties org.neo4j.server.webserver.port=7478 org.neo4j.server.webserver.https.port=7478 org.neo4j.server.database.mode=HA ~ vi n1/conf/neo4j-wrapper.conf wrapper.java.additional.4=-Dcom.sun.management.jmxremote.port=3639 wrapper.java.additional.5=-Dcom.sun.management.jmxremote.password.file=conf/jmx.password wrapper.java.additional.6=-Dcom.sun.management.jmxremote.access.file=conf/jmx.access
分別啓動3個節點:
~ n1/bin/neo4j start ~ n2/bin/neo4j start ~ n3/bin/neo4j start ~ jps 5033 Bootstrapper 4073 StartClient 5546 Jps 5393 Bootstrapper 5219 Bootstrapper
命令行curl訪問測試:
curl -H "Content-Type:application/json" -d '["org.neo4j:*"]' http://localhost:7474/db/manage/server/jmx/query
打開瀏覽器控制檯webadmin
http://192.168.1.201:7474/webadmin/#/info/org.neo4j/High%20Availability/
這樣就完成了 Neo4j集羣對於高可用安裝實踐!