neo4j

基本概念

Nodes:graph data records 结点 用"()"

Relationships : connect nodes 关系 用"[]"

properties : named data values 属性 (node和relationships都有属性) 用"{}"

labels :表名(不需要提前创建)

输入图片说明

Cypher Query Language

语法官网 https://neo4j.com/docs/developer-manual/current/cypher/syntax/parameters/

创建

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_id

match (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) 
with
collect(distinct j3.job_id ) as listb   ,lista

with FILTER( n IN listb  WHERE NOT n IN lista )  as listc
  unwind listc as c
RETURN  c


match p= (j1:JOB0317)-[:denp*2..]->(j2:JOB0317)   
with 
collect(distinct j1.job_id ) as lista 

match (j3:JOB0317) 
with
collect(distinct j3.job_id ) as listb   ,lista

with 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 x
RETURN x


WITH [1,1,2,2] AS coll 
UNWIND coll AS x
WITH DISTINCT x
RETURN collect(x) AS SET
查询出从stg*的job_id到job_scheduling 的最大时长路径
match (j:job) where j.job_id=~"stg.*"
with j.job_id as jid
match p=(j1:job{job_id:jid})-[:denp*]->(j2:job {job_id:"job_scheduling"})
unwind  nodes(p) as nds
with sum(nds.duration) as sum , length(p) as len , nodes(p) as ns
order by sum desc 
return sum/3600 ,len ,extract(n IN ns | n.job_id ) as ids
limit 10


match (j:job) where j.job_id=~"stg.*"
with j.job_id as jid
match p=(j1:job{job_id:jid})-[:denp*]->(j2:job {job_id:"job_scheduling"})
unwind  nodes(p) as nds
with sum(nds.duration) as sum , length(p) as len , nodes(p) as ns
order by sum desc 
return sum/3600 ,len ,ns
limit 10
查询路径最长的
match (j:job) where j.job_id=~"stg.*"
with j.job_id as jid
match p=(j1:job{job_id:jid})-[:denp*]->(j2:job)
unwind  nodes(p) as nds
with sum(nds.duration) as sum , length(p) as len , nodes(p) as ns
order by len  desc 
return sum/3600 ,len ,extract(n IN ns | n.job_id ) as ids
limit 10

java API

1、maven依赖

   <dependency>

        <groupId>org.neo4j.driver</groupId>

        <artifactId>neo4j-java-driver</artifactId>

        <version>1.4.4</version>

        <!--<scope>provided</scope>-->

    </dependency>

2、

猜你喜欢

转载自my.oschina.net/u/3267050/blog/1623639