Neo4j

查询语句

1、度>1的结点数

match (n:`事件`)-[]-() 
with n,count(*) as nums 
where nums>1 
return count(n)

2、入度>=1的结点数

match (n:`事件`)-[r:`导致`]->(m:`事件`) 
with count(n) as Degreein,m
where Degreein>=1 
return count(m)

3、出度>=1的结点数

match (m)-[r:`导致`]->(p:`事件`) 
with count(p) as OutDegree,m 
where OutDegree>=1 
return count(m)

4、出度>=1,入度>=1的结点数

match (n:`事件`)-[r:`导致`]->(m:`事件`) 
with count(n) as InDegree,m 
match (m)-[r:`导致`]->(p:`事件`) 
with InDegree,count(p) as OutDegree,m 
where InDegree >= 1 and OutDegree >= 1 
return count(m)

5、结点的最大度

match (n)-[r]->(m) 
with count(n) as InDegree,m 
match (m)-[r]->(p) 
with InDegree,count(p) as OutDegree,m
return max(InDegree + OutDegree)

6、度最大的结点

match (n)-[r]->(m) 
with count(n) as InDegree,m 
match (m)-[r]->(p) 
with InDegree,count(p) as OutDegree,m
where InDegree+OutDegree =  497
return m

7、降序输出度top8

match (n:`事件`)-[r:`导致`]->(m:`事件`) 
with count(n) as InDegree,m 
match (m)-[r:`导致`]->(p:`事件`) 
with InDegree,count(p) as OutDegree,m 
order by InDegree + OutDegree DESC
return m,InDegree+OutDegree
limit 8

8、查询入度大于0,出度为1的结点

match (n)-[]->(m) 
with count(n) as m_InDegree,m
match (m)-[]->(p)
with count(p) as m_OutDegree,m_InDegree,m,p
where m_InDegree > 0 and m_OutDegree =1
return m.name, p.name limit 10

9、删除自循环的点

MATCH p = (n)-[r]-(n) DETACH DELETE r;

10、删除没有节点的标签

match (n:prod) delete n
drop index on :prod(entity_id)

11、结点名称长度为1的点

match (n) where length(n.name) = 1 return distinct n.name,count(n)

12、将一个节点上的所有关系移到另一个节点上

MATCH p = (n:corp)-[r:`子公司`]->(m:corp)
where n.name = "金花投资)"
with m as subcompanys
match (q:corp)
where q.name ="金花投资"
with subcompanys,q
merge (q)-[r:`子公司`]->(subcompanys)

13、清库

match (n) detach delete n

14、使用csv文件导入节点

LOAD CSV WITH HEADERS FROM "file:///tech.csv" AS line
create(n:tech{entity_id:line.entity_id, name:line.name});

15、使用csv文件导入关系

LOAD CSV WITH HEADERS FROM "file:///参股.csv" AS line 
match (from{entity_id:line.head_entity_id}),(to{entity_id:line.tail_entity_id}) 
merge (from)-[r:参股{originalSentenceOfFileName:line.pro}]->(to);

16、百万级数据秒级导入

neo4j-admin import --nodes=import\node_header.csv,import\corp.csv,import\prod.csv --relationships=import\relation_header.csv,import\参股.csv,import\供应商.csv,import\客户.csv,import\业务.csv,import\子公司.csv

 17、使用apoc导出库

    将apoc-3.5.0.4-all.jar放入database中plugins中,在conf中设置apoc.export.file.enabled=true

CALL apoc.export.json.all("/exported.json",{useTypes:true})
CALL apoc.export.csv.all('/exported.db.csv',{stream:true,batchSize:2})

18、使用dump导出库

   先停库,然后在tetminal中cd bin,

neo4j-admin  dump --database=graph.db --to=C:\Users\Desktop\Graph.db.dump

19、记录一个困扰我多日的Bug

        当使用dump导库时,报错如下:

To perform recovery please start database in single mode and perform clean shutdown.

       经过翻墙查到,   

step1:shutdown database;
step2:open terminal;
step3:run bin/neo4j console;
step4:after startup,hit Ctrl+C to shutdown database;
step5:then,run neo4j-admin dump respectivery.

20、筛选出边上的属性中不包含某个字符串的关系

MATCH p = ()-[r:参股]->()
WHERE r.originalSentence =~ '.*参股.*'
RETURN n

21、删除孤立节点

match (n) where not(n)-[]-() delete n

22、两个节点互为参股关系时,删除来源句子较少的边

match p1 = (n:corp)-[r1:`参股`]->(m:corp)  
with size(split(r1.originalSentence,'@@')) as r1_sens, n,m, r1
match p2 = (m)-[r2:`参股`]->(n)
with size(split(r2.originalSentence,'@@')) as r2_sens,n,m,r1,r2
where r1_sens < r2_sens
delete r1

23、当两个节点之间同时存在参股关系和子公司关系时(A-参股->B,B-子公司->A)时,删除参股关系

match p1 = (n:corp)-[r1:`参股`]->(m:corp)  
with n,m,r1
match p2 = (m)-[r2:`子公司`]->(n)
with n,m,r1,r2
delete r1

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值