博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
neo4j
阅读量:6301 次
发布时间:2019-06-22

本文共 3842 字,大约阅读时间需要 12 分钟。

hot3.png

基本概念

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、

转载于:https://my.oschina.net/u/3267050/blog/1623639

你可能感兴趣的文章
Spring Boot2.0+中,自定义配置类扩展springMVC的功能
查看>>
MySQL(Navicat)运行.sql文件时报错[Err] 2006 - MySQL server has gone away 的解决方法
查看>>
参与博客编辑器改版,我的礼物 感谢51cto
查看>>
Fedora19和Windows7双系统修改默认引导
查看>>
JavaWeb笔记——JSTL标签
查看>>
Eclipse插件大全 挑选最牛的TOP30
查看>>
一些实用性的总结与纠正
查看>>
我的友情链接
查看>>
Kubernetes概念
查看>>
vCenter Server Web Client 乱码
查看>>
java-Spring jdbcTemplete datasource相关代码示例
查看>>
如何做一个合格的运维
查看>>
vim编辑器(上)
查看>>
Subversion 使用手册(开发人员)
查看>>
树链剖分 月下毛景树
查看>>
颓废了1年+,今天开始勤(tui)奋(fei)啦
查看>>
windows下批量修改文件(或文件夹)权限或所有者
查看>>
IP地址的分类
查看>>
我的友情链接
查看>>
关于测量数据用于团队改进的一些事儿
查看>>