基本概念
Nodes:graph data records 结点 用"()"
Relationships : connect nodes 关系 用"[]"
properties : named data values 属性 (node和relationships都有属性) 用"{}"
labels :表名(不需要提前创建)
Cypher Query Language
语法官网
创建
1、创建node
CREATE (TheMatrix:Movie {title:'The Matrix', released:1999, tagline:'Welcome to the Real World'})CREATE (Keanu:Person {name:'Keanu Reeves', born:1964})
2、创建relationship(可以多个relation一起创建,逗号隔开)
只创建relationship(node已经预先创建好了)
create (Anders:test {name:"Anders"})create (Ceasar:test {name:"Ceasar"})create (Bossman:test {name:"Bossman"})create (Emil:test {name:"Emil"})create (David:test {name:"David"})CREATE (Anders)-[:KNOWS]->(Bossman), (Anders)-[:BLOCKS]->(Ceasar), (Ceasar)-[:KNOWS]->(Emil), (Bossman)-[:KNOWS]->(Emil), (Bossman)-[:BLOCKS]->(David), (Anders)<-[:KNOWS]-(David)
创建节点+relationship
CREATE (Anders:test {name:"Anders"})-[:KNOWS]->(Bossman :test {name:"Bossman"}), (Anders:test {name:"Anders"})-[:BLOCKS]->(Ceasar :test {name:"Ceasar"}), (Ceasar:test {name:"Ceasar"})-[:KNOWS]->(Emil :test {name:"Emil"}), (Bossman:test {name:"Bossman"})-[:KNOWS]->(Emil :test {name:"Emil"}), (Bossman:test {name:"Bossman"})-[:BLOCKS]->(David :test {name:"David"}), (Anders:test {name:"Anders"})<-[:KNOWS]-(David :test {name:"David"})
删除
删除所有的节点,谨慎使用MATCH (n:test)DETACH DELETE n删除节点及关系MATCH (n)-[r]-()DELETE n,r单纯删除所有节点:match (n)delete n
查询
查询某个label下的node数
match (n:Greeting) return count(n)
查询孤立的点
match (a:job) WHERE NOT ((a)--()) RETURN a.job_id或 match (a:job) WHERE NOT ((a)-[]-()) RETURN a.job_idmatch (a:job) WHERE NOT ((a)<-[]-()) RETURN a.job_id
查询 之 starts with
MATCH (a:Person) where a.name starts with "T" return a.name ;
查询 之 关系查询
MATCH (a:Person)-[:ACTED_IN]->(m)<-[:DIRECTED]-(d) where a.name="Tom Hanks" RETURN a,m,d LIMIT 10;match (a:job)-[]->(b:job) where b.job_id="job_scheduling" RETURN a,b
查询直接指向job_scheduling 节点的节点个数
match p=(a:job)-[:denp]->(b:job) where b.job_id="job_scheduling" RETURN count(p)等价于match p=(a:job)-[:denp]->(b:job {job_id:"job_scheduling"}) RETURN a,b
查询出只和job_scheduling有关系的job_id
match p= (j1:job)-[:denp*2..]->(j2:job) with collect(distinct j1.job_id ) as lista match (j3:job) withcollect(distinct j3.job_id ) as listb ,listawith FILTER( n IN listb WHERE NOT n IN lista ) as listc unwind listc as cRETURN cmatch p= (j1:JOB0317)-[:denp*2..]->(j2:JOB0317) with collect(distinct j1.job_id ) as lista match (j3:JOB0317) withcollect(distinct j3.job_id ) as listb ,listawith FILTER( n IN listb WHERE NOT n IN lista ) as listc unwind filter(x in listc where x starts with "stg") as c return c
介绍:(1)with
(2)unwind:UNWIND会将大量的数据(高达10k或者50k条)[一个数据集合collect]分散成一行一行的。比如:
UNWIND[1,2,3] AS xRETURN xWITH [1,1,2,2] AS coll UNWIND coll AS xWITH DISTINCT xRETURN collect(x) AS SET
查询出从stg*的job_id到job_scheduling 的最大时长路径
match (j:job) where j.job_id=~"stg.*"with j.job_id as jidmatch p=(j1:job{job_id:jid})-[:denp*]->(j2:job {job_id:"job_scheduling"})unwind nodes(p) as ndswith sum(nds.duration) as sum , length(p) as len , nodes(p) as nsorder by sum desc return sum/3600 ,len ,extract(n IN ns | n.job_id ) as idslimit 10match (j:job) where j.job_id=~"stg.*"with j.job_id as jidmatch p=(j1:job{job_id:jid})-[:denp*]->(j2:job {job_id:"job_scheduling"})unwind nodes(p) as ndswith sum(nds.duration) as sum , length(p) as len , nodes(p) as nsorder by sum desc return sum/3600 ,len ,nslimit 10
查询路径最长的
match (j:job) where j.job_id=~"stg.*"with j.job_id as jidmatch p=(j1:job{job_id:jid})-[:denp*]->(j2:job)unwind nodes(p) as ndswith sum(nds.duration) as sum , length(p) as len , nodes(p) as nsorder by len desc return sum/3600 ,len ,extract(n IN ns | n.job_id ) as idslimit 10
java API
1、maven依赖
org.neo4j.driver neo4j-java-driver 1.4.4
2、