2

我正在运行一个Python (3.8)使用pipneo4j 4.0.0与社区版neo4j 4.1.1服务器交互的脚本。

MERGE如果节点和关系不存在,我正在运行许多用于更新或创建的查询。到目前为止,这运行良好,因为数据库正在按预期获取数据。

但是,从我的脚本方面,我想知道在每个查询中创建了多少个节点和边。问题是,在Cypher我从脚本发送到数据库的这些查询中,我不止一次调用MERGE并且还使用APOC过程(尽管这些APOC只是用于更新标签,它们不会创建实体)。

下面是一个查询示例:

comment_field_names: List[str] = list(threads[0].keys())
cypher_single_properties: List[str] = []
for c in comment_field_names:
    cypher_single_properties.append("trd.{0} = {1}.{0}".format(c, "trd_var"))
cypher_property_string: str = ", ".join(cypher_single_properties)


with driver.session() as session:
    crt_stmt = ("UNWIND $threads AS trd_var "

                "MERGE (trd:Thread {thread_id:trd_var.thread_id}) "
                "ON CREATE SET PY_REPLACE "
                "ON MATCH SET PY_REPLACE "

                "WITH trd AS trd "
                "CALL apoc.create.addLabels(trd, [\"Comment\"]) YIELD node "

                "WITH trd as trd "
                "MERGE (trd)-[r:THREAD_IN]->(Domain {domain_id:trd.domain_id}) "
                "ON CREATE SET r.created_utc = trd.created_utc "
                "ON MATCH SET r.created_utc = trd.created_utc "

                "RETURN distinct 'done' ")
    crt_params = {"threads": threads}

    # Insert the individual properties we recorded earlier.
    crt_stmt = crt_stmt.replace("PY_REPLACE", cypher_property_string)
    
    run_res = session.run(crt_stmt, crt_params)

这可以正常工作,并且节点是使用从 传递的属性创建的,该属性通过threads Dict变量传递crt_paramsUNWIND

但是,其中的Result实例内部run_res没有任何实例供我访问已创建节点和关系的统计信息。我怀疑这是因为:ResultSummarySummaryCounters

"RETURN distinct 'done' "

但是,我不确定这是否是原因。

希望有人可以帮助我设置我的查询,以便无论MERGE我执行多少操作,我都可以获得发送的整个查询的统计信息crt_stmt

非常感谢。

4

1 回答 1

2

使用较早的 neo4j 版本时,您可以编写n = result.summary().counters.nodes_created,但从 4.0 开始,该summary()方法不存在。

现在我从https://neo4j.com/docs/api/python-driver/current/break_changes.html发现Result.summary()已经被替换为Result.consume(),这种行为是消耗缓冲区中所有剩余的记录并返回ResultSummary.

您可以通过以下方式获取所有计数器counters = run_res.consume().counters

于 2020-07-26T14:59:21.850 回答