在不复制现有关系的情况下更新 neo4j 关系属性
Updating neo4j relationship properties without duplicating the existing relationship
我有以下内容来首先创建节点之间的关系(在上一步中已经创建的节点)
MATCH (a:node), (b:node) WHERE a.name = 'sw1' AND b.name = 'sw2'
merge (a)-[c:connect {packets_transmitted:0,packets_recieved:0}]->(b)
我需要更新属性而不复制已经存在的关系。现在每当我 运行 密码时,节点之间的关系 connect 就会重复。我该怎么办?
这是我用来更新关系属性的内容:
MATCH (a:node),(b:node) where a.name='sw1' and b.name ='sw2'
merge (a)-[c:connect]->(b)
set c.packets_transmitted = 250,c.packets_recieved = 300
更新:
这是我在 python 中编写的完整代码(我将其修改为尽可能清晰,但仍然存在重复关系)
注意:我直接在neo4j中执行,代码运行正常
cqlCreate_1=f"merge(n:node{{name:'openflow:2'}})"
with graphDB_Driver.session() as graphDB_Session:
graphDB_Session.run(cqlCreate_1)
cqlCreate_3=f"merge(m:node{{name:'2e:38:64:0e:e8:1f'}})"
with graphDB_Driver.session() as graphDB_Session:
graphDB_Session.run(cqlCreate_3)
cqlCreate_2=f"MATCH (a:node),(b:node) WHERE a.name ='openflow:2'\
and b.name='2e:38:64:0e:e8:1f'\
merge (a)-[c:connect{{p:0}}]-(b)"
with graphDB_Driver.session() as graphDB_Session:
graphDB_Session.run(cqlCreate_2)
cqlUpdate=f"MATCH (a:node)-[c:connect]-(b:node) where a.name='openflow:2'\
and b.name='2e:38:64:0e:e8:1f'\
set c.p=200"
with graphDB_Driver.session() as graphDB_Session:
graphDB_Session.run(cqlUpdate)
如果你想在节点之间找到一个 :connect 关系(你不关心匹配的属性是什么)然后更新属性,那么只 MERGE 关系,不要包含属性图案。然后使用 SET 将属性设置为新值:
MATCH (a:node), (b:node)
WHERE a.name = 'sw1' AND b.name = 'sw2'
MERGE (a)-[r:connect]-(b)
SET r.packets_transmitted = 0, r.packets_recieved = 0
如果只应在创建时设置属性,则改为使用 ON CREATE SET。
编辑:我删除了 MERGE 模式中的方向,这将允许它合并到节点之间现有的 :connect 关系,无论方向如何。
答案:
创建关系时不需要建立关系属性,因为每次都执行整个代码,这会导致关系重复。下面是更正后的代码:
cqlCreate_1=f"merge(n:node{{name:'openflow:2'}})"
with graphDB_Driver.session() as graphDB_Session:
graphDB_Session.run(cqlCreate_1)
cqlCreate_3=f"merge(m:node{{name:'2e:38:64:0e:e8:1f'}})"
with graphDB_Driver.session() as graphDB_Session:
graphDB_Session.run(cqlCreate_3)
cqlCreate_2=f"MATCH (a:node),(b:node) WHERE a.name ='openflow:2'\
and b.name='2e:38:64:0e:e8:1f'\
merge **(a)-[c:connect]-(b)**"
with graphDB_Driver.session() as graphDB_Session:
graphDB_Session.run(cqlCreate_2)
cqlUpdate=f"MATCH (a:node)-[c:connect]-(b:node) where a.name='openflow:2'\
and b.name='2e:38:64:0e:e8:1f'\
set c.p=200"
with graphDB_Driver.session() as graphDB_Session:
graphDB_Session.run(cqlUpdate)
我有以下内容来首先创建节点之间的关系(在上一步中已经创建的节点)
MATCH (a:node), (b:node) WHERE a.name = 'sw1' AND b.name = 'sw2'
merge (a)-[c:connect {packets_transmitted:0,packets_recieved:0}]->(b)
我需要更新属性而不复制已经存在的关系。现在每当我 运行 密码时,节点之间的关系 connect 就会重复。我该怎么办?
这是我用来更新关系属性的内容:
MATCH (a:node),(b:node) where a.name='sw1' and b.name ='sw2'
merge (a)-[c:connect]->(b)
set c.packets_transmitted = 250,c.packets_recieved = 300
更新: 这是我在 python 中编写的完整代码(我将其修改为尽可能清晰,但仍然存在重复关系)
注意:我直接在neo4j中执行,代码运行正常
cqlCreate_1=f"merge(n:node{{name:'openflow:2'}})"
with graphDB_Driver.session() as graphDB_Session:
graphDB_Session.run(cqlCreate_1)
cqlCreate_3=f"merge(m:node{{name:'2e:38:64:0e:e8:1f'}})"
with graphDB_Driver.session() as graphDB_Session:
graphDB_Session.run(cqlCreate_3)
cqlCreate_2=f"MATCH (a:node),(b:node) WHERE a.name ='openflow:2'\
and b.name='2e:38:64:0e:e8:1f'\
merge (a)-[c:connect{{p:0}}]-(b)"
with graphDB_Driver.session() as graphDB_Session:
graphDB_Session.run(cqlCreate_2)
cqlUpdate=f"MATCH (a:node)-[c:connect]-(b:node) where a.name='openflow:2'\
and b.name='2e:38:64:0e:e8:1f'\
set c.p=200"
with graphDB_Driver.session() as graphDB_Session:
graphDB_Session.run(cqlUpdate)
如果你想在节点之间找到一个 :connect 关系(你不关心匹配的属性是什么)然后更新属性,那么只 MERGE 关系,不要包含属性图案。然后使用 SET 将属性设置为新值:
MATCH (a:node), (b:node)
WHERE a.name = 'sw1' AND b.name = 'sw2'
MERGE (a)-[r:connect]-(b)
SET r.packets_transmitted = 0, r.packets_recieved = 0
如果只应在创建时设置属性,则改为使用 ON CREATE SET。
编辑:我删除了 MERGE 模式中的方向,这将允许它合并到节点之间现有的 :connect 关系,无论方向如何。
答案: 创建关系时不需要建立关系属性,因为每次都执行整个代码,这会导致关系重复。下面是更正后的代码:
cqlCreate_1=f"merge(n:node{{name:'openflow:2'}})"
with graphDB_Driver.session() as graphDB_Session:
graphDB_Session.run(cqlCreate_1)
cqlCreate_3=f"merge(m:node{{name:'2e:38:64:0e:e8:1f'}})"
with graphDB_Driver.session() as graphDB_Session:
graphDB_Session.run(cqlCreate_3)
cqlCreate_2=f"MATCH (a:node),(b:node) WHERE a.name ='openflow:2'\
and b.name='2e:38:64:0e:e8:1f'\
merge **(a)-[c:connect]-(b)**"
with graphDB_Driver.session() as graphDB_Session:
graphDB_Session.run(cqlCreate_2)
cqlUpdate=f"MATCH (a:node)-[c:connect]-(b:node) where a.name='openflow:2'\
and b.name='2e:38:64:0e:e8:1f'\
set c.p=200"
with graphDB_Driver.session() as graphDB_Session:
graphDB_Session.run(cqlUpdate)