neo4j是java编写的图(或叫网络)NoSQL。 这里的图的意思是,通过结点和关系,描绘一张数据图形;有些人可能想当然的理解为以图形结构存储数据。
neo4j有一套完整的解决方案,支持备份/恢复、高级监控、ha等(企业版,收费)
可做为(java)嵌入式开发,支持多种语言。
neo4j的查询语言是cypher--一种类人类语言,虽然neo4j也(有限的)支持sql,但sql在路径搜索上不如cypher直观。
参考资料:
cypher参考: http://docs.neo4j.org/refcard/2.1.1/
neo4j提供一个web管理(需要html5支持),可在web控制台执行cypher语句和监控db状态。
大部分图nosql遵守(注意:非强制)的三个特性:node(结点)、relationship(关系)、property(属性)
以下是一些演示。
创建node
CREATE (u:USER) SET u.name='张良',u.sex='M',u.userid='u1001';CREATE (u:USER) SET u.name='钱倩',u.sex='F',u.userid='u1002';CREATE (u:USER) SET u.name='王星宇',u.sex='M',u.userid='u1003';CREATE (u:USER) SET u.name='张闻',u.sex='M',u.userid='u1004';CREATE (u:USER) SET u.name='张一飞',u.sex='M',u.userid='u1005';CREATE (u:USER) SET u.name='周远',u.sex='M',u.userid='u1006';CREATE (u:USER) SET u.name='谢晁',u.sex='M',u.userid='u1007';CREATE (u:USER) SET u.name='翁莉',u.sex='F',u.userid='u1008';CREATE (u:USER) SET u.name='张茜静',u.sex='F',u.userid='u1009';CREATE (u:USER) SET u.name='孙雯君',u.sex='F',u.userid='u1010';CREATE (u:USER) SET u.name='隋燕',u.sex='F',u.userid='u1011';CREATE (g:GROUP) SET g.name='s4a',g.groupid='g101';CREATE (g:GROUP) SET g.name='3G',g.groupid='g102';CREATE (g:GROUP) SET g.name='TJ加油卡',g.groupid='g103';CREATE (d:DEPT) SET d.name='开发部',d.deptid='d101';CREATE (d:DEPT) SET d.name='测试部',d.deptid='d102';CREATE (d:DEPT) SET d.name='人事部',d.deptid='d103';
建立关系
MATCH (g {groupid:'g101'}), (d {deptid:'d101'}) MERGE (g)-[:BELONG]->(d );MATCH (g {groupid:'g102'}), (d {deptid:'d101'}) MERGE (g)-[:BELONG]->(d );MATCH (g {groupid:'g103'}), (d {deptid:'d101'}) MERGE (g)-[:BELONG]->(d );MATCH (u {userid:'u1001'}), (g {groupid:'g101'}) MERGE (u)-[:BELONG {role:'member'}]->(g);MATCH (u {userid:'u1002'}), (g {groupid:'g101'}) MERGE (u)-[:BELONG {role:'member'}]->(g);MATCH (u {userid:'u1003'}), (g {groupid:'g101'}) MERGE (u)-[:BELONG {role:'member'}]->(g);MATCH (u {userid:'u1004'}), (g {groupid:'g101'}) MERGE (u)-[:BELONG {role:'member'}]->(g);MATCH (u {userid:'u1005'}), (g {groupid:'g102'}) MERGE (u)-[:BELONG {role:'member'}]->(g);MATCH (u {userid:'u1006'}), (g {groupid:'g103'}) MERGE (u)-[:BELONG {role:'member'}]->(g);MATCH (u {userid:'u1007'}), (d {deptid:'d101'}) MERGE (u)-[:BELONG {role:'admin'}]->(d);MATCH (u {userid:'u1008'}), (d {deptid:'d101'}) MERGE (u)-[:BELONG {role:'member'}]->(d);MATCH (u {userid:'u1009'}), (d {deptid:'d102'}) MERGE (u)-[:BELONG {role:'member'}]->(d);MATCH (u {userid:'u1010'}), (d {deptid:'d102'}) MERGE (u)-[:BELONG {role:'member'}]->(d);MATCH (u {userid:'u1011'}), (d {deptid:'d103'}) MERGE (u)-[:BELONG {role:'member'}]->(d);
也可以通过这种方式建立结果和关系。
START会创建结点和关系;而MERGE是在已存在结点间建立关系。
START (u:USER)-[r:BELONG]->(g:GROUP) SET u.userid='u1001',r.role='member',g.name='TJ加油卡',g.groupid='g103';
一些查询操作
//查看所有用户MATCH (u:USER) RETURN u;//查询组与部门的对应关系MATCH (g:GROUP)-->(d:DEPT) return g.name as 组,d.name as 部门;//查询所有部门成员MATCH (u:USER)-[*]->(d:DEPT ) return u,d;//查询s4a组所有成员MATCH (u:USER)-[r:BELONG]->(g {groupid:'g101'}) return u,r,g;//查询开发部所有成员MATCH (u:USER)-[*]->(d:DEPT {deptid:'d101'}) return u,d;//查询开发部主管MATCH (u:USER)-[* {role:'admin'}]->(d:DEPT {deptid:'d101'}) return u,d;//增加周远为TJ加油卡的组长MATCH (u {userid:'u1006'}), (g {groupid:'g103'}) MERGE (u)-[:BELONG {role:'admin'}]->(g);//增加周远为TJ加油卡的成员MATCH (u {userid:'u1006'}), (g {groupid:'g103'}) MERGE (u)-[:BELONG {role:'member'}]->(g);//移除周远在TJ加油卡的角色MATCH (u {userid:'u1006'})-[r]->(g {groupid:'g103'}) REMOVE r.role;//移除周远在TJ加油卡的关系MATCH (u {userid:'u1006'})-[r]->(g {groupid:'g103'}) DELETE r;//设置周渊为TJ加油卡的组长MATCH (u {userid:'u1006'})-[r]->(g {groupid:'g103'}) SET r.role='admin';//查看上面操作结果MATCH (u {userid:'u1006'})-[r]->(g {groupid:'g103'}) return u,r,g;
在java下应用neo4j(嵌入式)
package org.sl.neo4j;import org.neo4j.graphdb.GraphDatabaseService;import org.neo4j.graphdb.Node;import org.neo4j.graphdb.Relationship;import org.neo4j.graphdb.factory.GraphDatabaseFactory;public class Neo4jPool { public static String DB_PATH = "D:/jars/neo4j-community-2.1.1/neo4jdb/neo4jtest1";// public static String DB_PATH = "http://127.0.0.1:7474"; private static ThreadLocaltl = new ThreadLocal (); public static GraphDatabaseService getConnect(){ GraphDatabaseService graphDb; synchronized(tl){ if(tl.get() == null){ graphDb = new GraphDatabaseFactory().newEmbeddedDatabase( DB_PATH ); registerShutdownHook(graphDb); tl.set(graphDb); }else{ graphDb = tl.get(); } } return graphDb; } static void registerShutdownHook(final GraphDatabaseService graphDb){ Runtime.getRuntime().addShutdownHook(new Thread(){ public void run(){ graphDb.shutdown(); } }); }}
static void t1(){ GraphDatabaseService db = Neo4jPool.getConnect(); Transaction tx = db.beginTx(); try{ Node n1 = db.createNode(); n1.setProperty("name", "n1"); n1.setProperty("value", "v1"); Node n2 = db.createNode(); n2.setProperty("name", "n2"); n2.setProperty("value", "v2"); n2.setProperty("desc", "desc2"); RelationshipType rt = DynamicRelationshipType.withName("relat1"); Relationship rela = n1.createRelationshipTo(n2, rt); //RelTypes.KNOWS rela.setProperty("name", "rela1"); tx.success(); }catch(Exception ex){ ex.printStackTrace(); tx.failure(); }finally{ tx.close(); } } static void t2(){ GraphDatabaseService db = Neo4jPool.getConnect(); Transaction tx = db.beginTx(); try{// NodesFunction nn = new NodesFunction(nn); Iterablenodes = db.getAllNodes(); for(Node n: nodes){ System.out.println(n.getProperty("name")); } tx.success(); }catch(Exception ex){ ex.printStackTrace(); tx.failure(); }finally{ tx.close(); } } static void t5(){ GraphDatabaseService db = Neo4jPool.getConnect(); Transaction tx = db.beginTx(); try{ ExecutionEngine engine = new ExecutionEngine( db ); String query = "START n=node(*) RETURN n,n.name"; ExecutionResult rs = engine.execute(query); ResourceIterator riter = rs.columnAs("n"); while(riter.hasNext()){ Node n = riter.next(); // n.setProperty("desc", "desc222222"); // Iterable ps = n.getPropertyKeys(); Iterator piter = n.getPropertyKeys().iterator(); while(piter.hasNext()){ String key = piter.next(); System.out.println(key+":"+n.getProperty(key)); }// n.setProperty("desc", "desc21"); } tx.success(); }catch(Exception ex){ ex.printStackTrace(); tx.failure(); } tx.close(); db.shutdown(); } static void t6(){ GraphDatabaseService db = Neo4jPool.getConnect(); Transaction tx = db.beginTx(); try{ String sql = "START n=node(1) SET n.desc='desc0000' "; ExecutionEngine engine = new ExecutionEngine( db ); engine.execute(sql); tx.success(); }catch(Exception ex){ ex.printStackTrace(); tx.failure(); } }