其他分享
首页 > 其他分享> > neo4j本人常用CQL语句

neo4j本人常用CQL语句

作者:互联网

neo4j常用CQL语句:

 


查看某节点相关的所有关系

MATCH (n:BussinessObj {name:"冻结" } ) -[r]->(z) RETURN n,z
MATCH (b:BussinessObj)-[r]-() RETURN b,r


删除节点,以及与之相关的所有关系
MATCH (b:BussinessObj)-[r]-() DELETE b,r

 

一些不存在关系的节点没有删除掉还得再用语句:
MATCH (b:BussinessObj) DELETE b

 

删除某类节点的某个关系
假设要删除BussinessObj类节点的某个关系,但是你忘记具体的关系名是什么了,所以应该查看BussinessObj类存在哪些关系,关系名是什么。具体语句:MATCH (n:BussinessObj)-[r]-() RETURN r,type(r)

 

然后再指定要删除的具体的关系名,假如我要删除BussinessObj类的companyTagIs关系。语句:MATCH (n:BussinessObj)-[r:companyTagIs]-() DELETE r 具有删除某一个关系。注意,如果DELETE时将n也放在其后,会出问题,因为n即BussinessObj还存在其他关系,在你还没有删除掉BussinessObj的所有关系之前,是不能正确删除节点的。

 

 

删除节点及关系
MATCH (n)-[r]-()
DELETE n,r
上面的方式会将node和relations都删除,但这种删除方式仍会有一些信息未清空(如Property keys),彻底删除数据库使用如下方式:

systemctl stop neo4j

rm -rf /var/lib/neo4j/data/databases/graph.db


systemctl start neo4j


————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
导入节点:
1. 先将BussinessObj.csv放入 /var/lib/neo4j/import目录,若有中文先转成UTF-8格式name coreFlag description
如果是liunx系统必须加入file:/作为前缀,windows系统加入LOAD CSV FROM "file://c:/location/local/my.csv"

LOAD CSV WITH HEADERS FROM "file:/BussinessObj.csv" AS line
MERGE (b:BussinessObj{name:line.name,coreFlag:line.coreFlag,description:line.description})


LOAD CSV WITH HEADERS FROM "file:/BussinessObj1_2.csv" AS line
MERGE (b:BussinessObj{name:line.name,coreFlag:line.coreFlag,description:line.description})


————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

 

 

 

 

创建对象节点:
{
"name": "冻结11",
"description": "经济业务处理前进行账户余额预先冻结1269",
"coreFlag": "Y858"
}

创建同类型的节点间关系

 

==============================================================
下一类型关系 nextNode

==============================================================
match (n1:BussinessObj),(n2:BussinessObj) where n1.name ='订单支付' and n2.name='清分'
return n1,n2 ;

match (n1:BussinessObj),(n2:BussinessObj) where n1.name ='订单支付' and n2.name='清分'
create (n1)-[t:nextNode{name:'下一节点'}]->(n2)
return n1,n2,t;

match (n1:BussinessObj),(n2:BussinessObj) where n1.name ='清分' and n2.name='日结算'
create (n1)-[t:nextNode{name:'下一节点'}]->(n2)
return n1,n2,t;

 

match (n1:BussinessObj),(n2:BussinessObj) where n1.name ='清分' and n2.name='账务'
create (n1)-[t:nextNode{name:'下一节点'}]->(n2)
return n1,n2,t;


match (n1:BussinessObj),(n2:BussinessObj) where n1.name ='账务' and n2.name='会计'
create (n1)-[t:nextNode{name:'下一节点'}]->(n2)
return n1,n2,t;

 

match (n1:BussinessObj),(n2:BussinessObj) where n1.name ='日结算' and n2.name='账务'
create (n1)-[t:nextNode{name:'下一节点'}]->(n2)
return n1,n2,t;

 


match (n1:BussinessObj),(n2:BussinessObj) where n1.name ='订单取消' and n2.name='清分'
create (n1)-[t:nextNode{name:'下一节点'}]->(n2)
return n1,n2,t;

 


供应商罚款资金流
match (n1:BussinessObj),(n2:BussinessObj) where n1.name ='供应商罚款' and n2.name='冻结'
MERGE (n1)-[t:nextNode{name:'下一节点'}]->(n2)
return n1,n2,t

match (n1:BussinessObj),(n2:BussinessObj) where n1.name ='供应商罚款' and n2.name='保证金'
MERGE (n1)-[t:nextNode{name:'下一节点'}]->(n2)
return n1,n2,t

match (n1:BussinessObj),(n2:BussinessObj) where n1.name ='冻结' and n2.name='转账'
MERGE (n1)-[t:nextNode{name:'下一节点'}]->(n2)
return n1,n2,t

match (n1:BussinessObj),(n2:BussinessObj) where n1.name ='冻结' and n2.name='保证金'
MERGE (n1)-[t:nextNode{name:'下一节点'}]->(n2)
return n1,n2,t

match (n1:BussinessObj),(n2:BussinessObj) where n1.name ='转账' and n2.name='解冻'
MERGE (n1)-[t:nextNode{name:'下一节点'}]->(n2)
return n1,n2,t

match (n1:BussinessObj),(n2:BussinessObj) where n1.name ='转账' and n2.name='账务'
MERGE (n1)-[t:nextNode{name:'下一节点'}]->(n2)
return n1,n2,t

match (n1:BussinessObj),(n2:BussinessObj) where n1.name in ['转账'] and n2.name='保证金'
create (n1)-[t:nextNode{name:'所属类型'}]->(n2)
return n1,n2,t

 

 


平台提现资金流
match (n1:BussinessObj),(n2:BussinessObj) where n1.name ='平台提现' and n2.name='冻结'
MERGE (n1)-[t:nextNode{name:'下一节点'}]->(n2)
return n1,n2,t

 

 

供应商提现资金流
match (n1:BussinessObj),(n2:BussinessObj) where n1.name ='供应商提现' and n2.name='冻结'
MERGE (n1)-[t:nextNode{name:'下一节点'}]->(n2)
return n1,n2,t

match (n1:BussinessObj),(n2:BussinessObj) where n1.name ='供应商提现' and n2.name='保证金'
MERGE (n1)-[t:nextNode{name:'下一节点'}]->(n2)
return n1,n2,t


售后资金流
match (n1:BussinessObj),(n2:BussinessObj) where n1.name ='售后' and n2.name='冻结'
MERGE (n1)-[t:nextNode{name:'下一节点'}]->(n2)
return n1,n2,t

match (n1:BussinessObj),(n2:BussinessObj) where n1.name ='售后' and n2.name='保证金'
MERGE (n1)-[t:nextNode{name:'下一节点'}]->(n2)
return n1,n2,t

 

 


==============================================================
从属类型关系 BelongToRelation

==============================================================
match (n1:BussinessObj),(n2:BussinessObj) where n1.name in ['账户','账户余额','账务流水'] and n2.name='账务'
create (n1)-[t:BelongToRelation{name:'所属类型'}]->(n2)
return n1,n2,t

match (n1:BussinessObj),(n2:BussinessObj) where n1.name in ['账户余额'] and n2.name='账户'
create (n1)-[t:BelongToRelation{name:'所属类型'}]->(n2)
return n1,n2,t

match (n1:BussinessObj),(n2:BussinessObj) where n1.name in ['保证金'] and n2.name='账户'
create (n1)-[t:BelongToRelation{name:'所属类型'}]->(n2)
return n1,n2,t

 

 

match (n1:BussinessObj),(n2:BussinessObj) where n1.name in ['冻结','解冻'] and n2.name='账户余额'
create (n1)-[t:BelongToRelation{name:'所属类型'}]->(n2)
return n1,n2,t

match (n1:BussinessObj),(n2:BussinessObj) where n1.name in ['划付','渠道代付'] and n2.name='出金'
create (n1)-[t:BelongToRelation{name:'所属类型'}]->(n2)
return n1,n2,t

 

match (n1:BussinessObj),(n2:BussinessObj) where n1.name in ['会计账户','会计科目','会计分录','记账场景'] and n2.name='会计'
MERGE (n1)-[t:BelongToRelation{name:'所属类型'}]->(n2)
return n1,n2,t

 


match (n1:BussinessObj),(n2:BussinessObj) where n1.name='冻结' and n2.name='冻结类'
create (n1)-[t:BelongToRelation{name:'所属类型'}]->(n2)
return n1,n2,t

 

match (n1:BussinessObj),(n2:BussinessObj) where n1.name in ['冻结7','冻结100'] and n2.name='冻结类'
return n1,n2

match (n1:BussinessObj),(n2:BussinessObj) where n1.name in ['冻结7','冻结100'] and n2.name='冻结类'
create (n1)-[t:BelongToRelation{name:'所属类型'}]->(n2)
return n1,n2,t

 

Cypher
:help 帮助页面
:schema 查看数据库结构
:schema ls -l :Person

:server change-password // 修改密码
CALL dbms.changePassword("newpassword") // (旧版本)修改密码

:server connect 连接

:play sysinfo 查看系统信息

// List node labels 查询所有的label
CALL db.labels()

// List relationship types 查询所有的type
CALL db.relationshipTypes()

// What is related, and how 查询数据里的节点和关系 类似于 SQL的desc
CALL db.schema()

// List functions
CALL dbms.functions()

// List procedures
CALL dbms.procedures()

CALL dbms.listQueries() ;

CALL dbms.killQuery(queryId);

// 查询一共有多少节点
// Count all nodes
match (n) RETURN count(n)

// 查询一共有多少关系 // 不带方向的话结果是2倍
// Count all relationships
match ()-->() RETURN count(*);
match ()-[r]->() return count(r);

 

// 查询某节点一共有多少关系 // 不带方向的话结果是2倍
match (n:XXX {name:'账务流水'})-[r]->() return count(r);


//同一标签下的两个节点建立关系
match (a:Person), (b:Person) where a.name = 'zhangsan' and b.name = 'lisi'
merge (a)-[r:RELTYPE]->(b) return r

// 模糊匹配
match (n:Person) where n.name =~ '张.*' return n

// 包含
match (n:Person) where n.name contains '张' return n;

// 去重
match (n:Person) with n.name as name return distinct name;

// Count all nodes 查询一共有多少节点
match (n) RETURN count(n)

// Count all relationships 查询一共有多少关系
match ()-->() RETURN count(*);


// 查询一共有多少种节点
call db.labels();
match (n) return distinct label(n);


// 查询一共有多少关系
call db.relationshipTypes()

// 查询数据库里的所有属性
match (n) unwind keys(n) as allkeys return distinct allkeys;
// 查询关系最多的节点
// 实际使用时,最好对n加个范围,要不然 全图扫描
// 使用with 和 别名,能减少一次count(*)的查询
match (n:BussinessObj)--() with n.name as name, count(*) as count return name, count order by count desc limit 1;
match (n:BussinessObj)-[r]-() with n.name as name, count(r) as count return name, count order by count desc limit 1;

// 查询孤立节点
match (n) where not (n)--() return n;
// 查询孤立节点ID
match (n) where not (n)--() return id(n);


查看某节点相关的所有关系
MATCH (n:BussinessObj ) -[r]->(z) where n.name="冻结" RETURN n,z
MATCH (n:BussinessObj {name:"冻结" } ) -[r]->(z) RETURN n,z
MATCH (b:BussinessObj)-[r]-() RETURN b,r


MATCH (n:BussinessObj {name:"冻结" } ) -[r]->(z) RETURN n,z

深度查询
1:使用with关键字
查询三层级关系节点如下:with可以将前面查询结果作为后面查询条件

MATCH (n1:BussinessObj ) -[r1]->(n2:BussinessObj) where n1.name="冻结"
WITH n1,r1,n2
MATCH (n2:BussinessObj )-[r2]->(n3:BussinessObj)
return n1,r1,n2,r2,n3

2:直接拼接关系节点查询
match (n1:BussinessObj{name:'冻结'})-[r1]->(n2:BussinessObj)-[r2]->(n3:BussinessObj)
return n1,r1,n2,r2,n3

3.为了方便,可以将查询结果赋给变量,然后返回
match data=(n1:BussinessObj{name:'冻结'})-[r1]->(n2:BussinessObj)-[r2]->(n3:BussinessObj) return data

 

4.使用深度运算符
当实现多深度关系节点查询时,显然使用以上方式比较繁琐。

可变数量的关系->节点可以使用-[:TYPE*minHops..maxHops]->。

查询:

如果在1到3的关系中存在路径,将返回开始点和结束点。

match data=(n1:BussinessObj{name:'冻结'})-[*1..3]->(n2:BussinessObj) return data

返回节点信息+关系信息
match (n1:BussinessObj{name:'清分'})-[r*1..10]->(n2:BussinessObj) return n1 ,r , n2

仅返回节点信息

match (n1:BussinessObj{name:'清分'})-[*1..10]->(n2:BussinessObj) return n1 , n2

仅返回关系
match (n1:BussinessObj{name:'清分'})-[r*1..10]->(n2:BussinessObj) return r
match (n1:BussinessObj)-[r*1..10]->(n2:BussinessObj) return r

 

标签:语句,return,name,CQL,match,n1,neo4j,n2,BussinessObj
来源: https://www.cnblogs.com/sea520/p/11945387.html