Neo4j簡單的樣例
阿新 • • 發佈:2017-08-13
ebp 農業銀行 ati nts -m sdn neo size admin
這裏數據的路徑是:/home/admin/db/neo4j
系統環境:
Ubuntu 04.10 x64
一:安裝
下載最新版:neo4j-community-2.2.3-unix.tar.gz 解壓
cd neo4j-community-2.2.3/bin
./neo4j start
啟動之後。默認的webport是:7474
三:JAVA操作neo4j
以下樣例演示了一個銀行層級關系的樣例
央行以下有四大行,四大行以下有各自的分行。各自的分行又有各自的支行
package com.lala.neo4j; import org.neo4j.graphdb.Label; public class BankLabel implements Label { private String name; public BankLabel(String name) { this.name = name; } public String name() { return name; } }
package com.lala.neo4j; import org.neo4j.graphdb.RelationshipType; public class BankRelationshipType implements RelationshipType { private String ship; public BankRelationshipType(String ship) { this.ship = ship; } public String name() { return ship; } }
package com.lala.neo4j; import org.neo4j.graphdb.GraphDatabaseService; import org.neo4j.graphdb.Node; import org.neo4j.graphdb.Transaction; import org.neo4j.graphdb.factory.GraphDatabaseFactory; public class Neo4j { /** * 標簽 */ static BankLabel top = new BankLabel("總行"); static BankLabel sdh = new BankLabel("四大銀行"); static BankLabel fh = new BankLabel("分行"); static BankLabel zh = new BankLabel("支行"); /** * 關系 */ static BankRelationshipType xj = new BankRelationshipType("XJ"); static BankRelationshipType sj = new BankRelationshipType("SJ"); /** * 初始化總行和分行的關系數據 */ static void init1(GraphDatabaseService db)throws Exception { Node n1 = db.createNode(top); n1.setProperty("name", "央行"); Node n2 = db.createNode(sdh); n2.setProperty("name", "農業銀行"); Node n3 = db.createNode(sdh); n3.setProperty("name", "工商銀行"); n1.createRelationshipTo(n2, sj); n2.createRelationshipTo(n1, xj); n1.createRelationshipTo(n3, sj); n3.createRelationshipTo(n1, xj); } /** * 初始化農業銀行分行數據 */ static void init2(GraphDatabaseService db)throws Exception { Node n1 = db.getNodeById(1); Node gd = db.createNode(fh); gd.setProperty("name", "農業銀行廣東分行"); n1.createRelationshipTo(gd, sj); gd.createRelationshipTo(n1, xj); Node hn = db.createNode(fh); hn.setProperty("name", "農業銀行湖南分行"); n1.createRelationshipTo(hn, sj); hn.createRelationshipTo(n1, xj); Node hb = db.createNode(fh); hb.setProperty("name", "農業銀行湖北分行"); n1.createRelationshipTo(hb, sj); hb.createRelationshipTo(n1, xj); Node zj = db.createNode(fh); zj.setProperty("name", "農業銀行浙江分行"); n1.createRelationshipTo(zj, sj); zj.createRelationshipTo(n1, xj); } /** * 初始化工商銀行分行數據 */ static void init3(GraphDatabaseService db)throws Exception { Node n1 = db.getNodeById(2); Node gd = db.createNode(fh); gd.setProperty("name", "工商銀行廣東分行"); n1.createRelationshipTo(gd, sj); gd.createRelationshipTo(n1, xj); Node hn = db.createNode(fh); hn.setProperty("name", "工商銀行湖南分行"); n1.createRelationshipTo(hn, sj); hn.createRelationshipTo(n1, xj); Node hb = db.createNode(fh); hb.setProperty("name", "工商銀行湖北分行"); n1.createRelationshipTo(hb, sj); hb.createRelationshipTo(n1, xj); } /** * 初始化農業銀行廣東分行以下的支行數據 */ static void init4(GraphDatabaseService db)throws Exception { Node n1 = db.getNodeById(3); Node th = db.createNode(zh); th.setProperty("name", "農業銀行天河支行營業部"); n1.createRelationshipTo(th, sj); th.createRelationshipTo(n1, xj); Node yt = db.createNode(zh); yt.setProperty("name", "農業銀行燕塘支行"); n1.createRelationshipTo(yt, sj); yt.createRelationshipTo(n1, xj); Node thb = db.createNode(zh); thb.setProperty("name", "農業銀行天河北路支行"); n1.createRelationshipTo(thb, sj); thb.createRelationshipTo(n1, xj); Node sp = db.createNode(zh); sp.setProperty("name", "農業銀行石牌東路支行"); n1.createRelationshipTo(sp, sj); sp.createRelationshipTo(n1, xj); Node hy = db.createNode(zh); hy.setProperty("name", "農業銀行華苑支行"); n1.createRelationshipTo(hy, sj); hy.createRelationshipTo(n1, xj); } /** * start a=node(13) MATCH (a)<-[:SJ*1..3]-(b) return a,b (往上找三級,一共四級) * start a=node(1) MATCH (a)-[:SJ*1..3]->(b) return a,b (往下找三級,一共四級) */ public static void main(String[] args) throws Exception { GraphDatabaseService db = new GraphDatabaseFactory().newEmbeddedDatabase("/home/admin/db/neo4j"); Transaction tx = db.beginTx(); init1(db); init2(db); init3(db); init4(db); tx.success(); tx.close(); } }
這裏數據的路徑是:/home/admin/db/neo4j
cd neo4j-community-2.2.3/conf
vim neo4j-server.properties 文件。把數據庫文件夾改為上面的路徑,然後,重新啟動neo4j
訪問
http://127.0.0.1:7474
輸入默認的username/password neo4j/neo4j
然後。改動默認password
就可以查詢
以下給幾個查詢語句
查詢全部:match a return a
查詢全部標簽為‘分行‘的數據 match (a:分行) return a
start a=node(1) MATCH (a)-[:SJ*1..3]->(b) return a,b (從node id=1開始,往下找三級,一共四級)
start a=node(13) MATCH (a)<-[:SJ*1..3]-(b) return a,b (從node id=13開始。往上找三級。一共四級)
總結:neo4j非常適合保存有層級關系的數據,比方說:無限極分類
Neo4j簡單的樣例