[Neo4j系列二十一]统计节点、关系、属性数量的常用Cypher(干货)

neo4j 1年前 ⋅ 1109 阅读

 这可能是全网唯一且首次总结Cypher统计节点、关系、属性数量的文章,干货在手,点击在看。


      在使用Neo4j时,是不是经常被Leader问我们图库里多少节点多少关系了?然后你是怎么解决呢?马上登录页面控制台看看数字吗?这样的事咱们作为程序员干一次两次可以,每次都这么干就要想点偷懒的方法了。提供一个页面实时统计数量指标或者每次更新完或每周定时通过webhook推送这些消息到钉钉群就解决了,废话不多说看具体统计语句。

图片

  • 统计所有节点总数
match (n) return count(n)
  • 统计所有关系总数
match ()-[r]->() return count(r)
  • 统计所有节点属性总数
match (n) with size(keys((properties(n)))) as l return sum(l)
  • 统计所有关系属性总数
match ()-[r]->() with size(keys(properties(r))) as l return sum(l)
  • 统计节点类型即labels数量
CALL db.labels() YIELD label return size(collect(label))
match (n) with distinct labels(n) as l return reduce(s=0, x in collect(size(l))|s + x)
match (n) with distinct labels(n) as l unwind l as ls return size(collect(ls))
  • 统计关系类型即type数量
match ()-[r]->() with collect(distinct type(r)) as l return size(l)
  • 列出节点类型即labels
CALL db.labels() YIELD label return collect(label)
match (n) with distinct labels(n) as l unwind l as ls return collect(ls)
  • 列出关系类型即type
match ()-[r]->() with collect(distinct type(r)) as l return l
  • 统计节点每种类型即每种labels的数量
match (n) with distinct labels(n) as l, n unwind l as label return label, count(n)
match (n) with distinct labels(n) as l unwind l as label match(m) where label in labels(m) return label, count(m)
  • 统计关系每种类型即每种type的数量
match ()-[r]->() return distinct type(r), count(*)
以上完全是在工程实践中总结出来的干货,程序可以帮我们实现很多想法,多动手,勤尝试,必定收获满满!



原创不易,求转发,求点“在看”,喜欢请关注,在这里有最真实的分享!

图片

 Python与知识图谱


建了一个知识图谱的交流群,欢迎技术相关的朋友入群交流!

可以加我个人微信入群

(微信号:yangzhi1278085188,暗号:neo4j)


更多内容请访问:IT源点

相关文章推荐

全部评论: 0

    我有话说: