我是neo4j和cypher的新手,有大约一周的经验......我正在开发一个小项目来操作数以万计的TWS批处理作业的图表在我公司的主机上运行。一个关键的任务是找出我们所谓的午夜最后一批批处理作业的关键路径,这实际上是neo4j中的加权最短路径。我已经使用下面的密码实现了这个目标。
MATCH (a:Job {Jobname:...}),(b:Job {Jobname:...})
call apoc.algo.dijkstra(a,b,'runafter>','Duration') YIELD path, weight
RETURN path,weight`
我创建了一个带有neo4j驱动程序的python,它每天自动运行,从rbdms中提取批处理作业数据,并在neo4j中每天创建一个新图形并运行cyphers并格式化结果key-path以适应我的MySQL所以我可以比较每个不同日子的关键路径
但是我想到了一个新的想法,如果我可以增强这个密码,以便返回路径上的节点将被设置为标签/或属性?以便我以后可以轻松实现再次参考密钥路径而不是每次都调用Dijkstra。我知道我可以使用我的python程序来做到这一点,就在关键路径返回并生成一系列密码来完成这项工作之后,但我认为应该有一个单独的密码解决方案。非常感谢提前!
答案 0 :(得分:2)
MATCH (a:Job {Jobname:...}),(b:Job {Jobname:...}) WITH a, b,
a.Jobname + '-' + b.Jobname AS pathID
CALL apoc.algo.dijkstra(a,b,'runafter>','Duration') YIELD path, weight
FOREACH (n IN NODES(path)|
SET n.pathID = pathID,
n.pathWeight = weight
)
RETURN path,weight
由于你使用了apoc,你可以set labels:
MATCH (a:Job {Jobname:...}),(b:Job {Jobname:...}) WITH a, b,
'inCalculatedPath' + '-' + a.Jobname + '-' + b.Jobname AS pathID
CALL apoc.algo.dijkstra(a,b,'runafter>','Duration') YIELD path, weight
CALL apoc.create.addLabels( NODES(path), ['inCalculatedPath', pathID])
RETURN path,weight
另一种方法是添加类似Calculated path
node:
MATCH (a:Job {Jobname:...}),(b:Job {Jobname:...}) WITH a, b
CALL apoc.algo.dijkstra(a,b,'runafter>','Duration') YIELD path, weight
CREATE (P:CalculatedPath)
SET P.weight = weight,
P.start = ID(a),
P.end = ID(b),
P.pathNodes = REDUCE(ids=[], n IN NODES(path)| ids + ID(n)),
P.pathRels = REDUCE(ids=[], r IN RELS(path) | ids + ID(r))
FOREACH (n IN NODES(path)|
MERGE (n)-[:inPath]->(P)
)
RETURN path, weight
让路径回来:
MATCH (a:Job {Jobname:...}),(b:Job {Jobname:...}) WITH a, b
MATCH (path:CalculatedPath {start: ID(A), end: ID(b)})
RETURN path, path.weight AS weight