python如何收集数据库_如何从Python获取Neo4j图形数据库的节点数?

I'm trying to get the number of nodes of a Neo4j graph database using Python, but I don't find any method or property to do that.

Does anybody how can I get this information?

Other Python packages like NetworkX has a method to get this information.

>>> G = nx.Graph() # or DiGraph, MultiGraph, MultiDiGraph, etc

>>> G.add_path([0,1,2])

>>> len(G)

3

解决方案

Update:

Since I first wrote this, the answer has changed. The database now keeps exact counts of total nodes, as well as counts by label. Unlike most databases, this is not a heuristic, these counters are transactionally kept in sync with the rest of the datastore.

This means you can get exact node counts in O(1) time from Neo4j. You get access to them by asking Cypher:

MATCH (n) RETURN count(*)

Original reply:

There are two ways to get the number of nodes in a neo4j database. The first one is to actually iterate through all the nodes, and counting them.

Alternative two is to use the "number of node ids in use" statistic provided by the db kernel, which does not guarantee to be exact, but will be at least the number of nodes in use. In a high-load db it will be higher, since it also contains ids of deleted nodes that have not been reclaimed yet.

Alt one is reasonably exact (depending on how many are created/deleted while you iterate), but can be super slow. Alt two is potentially way off, but is a O(1) operation.

You currently don't have much choice, because alt one is the only one that works. It isn't officially supported, so doing it today looks a bit dirty:

from neo4j import GraphDatabase

db = GraphDatabase('..')

node_count = sum(1 for _ in db.getAllNodes().iterator())

I've added two issues for this, one to add support for accessing management info (eg. support the alt two method), and one to add support for these use cases:

node_count = sum(1 for _ in db.nodes)

node_count = len(db.nodes)

Follow these issues here:

Please let us now if you run into any other trouble with neo4j-embedded, add a ticket to the github issues if you discover any bugs or think of any other enhancements!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值