Neo4j简单的例子

时间:2023-01-10 18:04:10

系统环境:

Ubuntu 04.10 x64


一:安装

下载最新版:neo4j-community-2.2.3-unix.tar.gz  解压 

cd neo4j-community-2.2.3/bin

./neo4j start

启动之后,默认的web端口是: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

输入默认的用户名/密码 neo4j/neo4j

然后,修改默认密码


即可查询


下面给几个查询语句


查询所有:match a return a


查询所有标签为'分行'的数据 match (a:分行) return a


start a=node(1) MATCH (a)-[:SJ*1..3]->(b) return a,b (从node id=1开始,往下找三级,一共四级)

Neo4j简单的例子


start a=node(13) MATCH (a)<-[:SJ*1..3]-(b) return a,b (从node id=13开始,往上找三级,一共四级)

Neo4j简单的例子


总结:neo4j很适合保存有层级关系的数据,比如说:无限极分类