Neo4J Cypher - 使用触发器记录csv文件中已删除的节点和关系

时间:2018-01-27 11:51:31

标签: neo4j cypher neo4j-apoc

我试图在Neo4J上使用触发器在CSV中记录节点或关系被删除时的详细信息。在尝试使用不同的触发查询后,我发现尝试将已删除关系的详细信息直接写入csv并不起作用,但创建一个虚拟节点来记录关系详细信息,然后将v节点写入csv。我使用的查询是:

call apoc.trigger.add("deletingRels","UNWIND {deletedRelationships} as del WITH del call apoc.create.vNode(['Record'], {startUUID:startNode(del).uuid, endUUID:endNode(del).uuid, relType:type(del)}) YIELD node as r WITH r, collect(r) as rs,'D:/export/reldeltest.cql' as filename call apoc.when(size(rs) > 0,'call apoc.export.csv.data(rs,[],filename,{quotes:false,useTypes:false}) YIELD rows RETURN rows','',{rs:rs,filename:filename}) YIELD value RETURN value",{phase:"before"})

运行查询:

match (t:Test {name:"test 12"})-[r]->(s:Test {name:"test 7"}) delete r return t,s

删除关系r并满意地产生以下csv:

"_id","_labels","startUUID","relType","endUUID","_start","_end","_type"
"-4",":Record","f8ccb190-fd43-11e7-866b-40b034e34b1f","testR","ad45c410-fd24-11e7-90a4-40b034e34b1f",,,

删除节点的类似触发器是:

call apoc.trigger.add("deletingNodes", "UNWIND {deletedNodes} as del WITH del call apoc.create.vNode(['Record'], {uuid:del.uuid, nodeLabels:labels(del)}) YIELD node as r WITH r, collect(r) as rs,'D:/export/nodedeltest.cql' as filename call apoc.when(size(rs) > 0,'call apoc.export.csv.data(rs,[],filename,{quotes:false,useTypes:false}) YIELD rows RETURN rows','',{rs:rs,filename:filename}) YIELD value RETURN value", {phase:"before"})

运行查询以删除节点会在Neo4J浏览器中导致以下错误消息:

Neo.ClientError.Transaction.TransactionHookFailed

并启动控制台中的错误日志:

WARN  Error executing trigger deletingNodes in phase before Node with id 1364 has been dele
ted in this transaction
org.neo4j.graphdb.QueryExecutionException: Node with id 1364 has been deleted in this transaction
        at org.neo4j.kernel.impl.query.QueryExecutionKernelException.asUserException(QueryExecutionKernelException.java:
35).

运行查询以匹配节点,表明它仍然存在。

我在Windows上运行了一个压缩版本的Neo4J 3.3.1,并在控制台中安装了apoc 3.3.1插件。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

我认为我们应该可以简化记录更新/删除数据的整个过程。

话虽如此,我认为你可以访问已删除的节点的属性del.uuid,这些属性不再可用/可访问,我想你可以访问{{1 }和id(del) b / c它们位于节点记录中。

对于这种关系,您也只能访问labels(del),但不能访问任何属性。

已删除的属性应在单独的(较早的)removed-properties-trigger中提供。