Neo4j - 使用CYPHER设置节点的标签列表,从json文件

时间:2016-03-02 12:34:46

标签: neo4j cypher py2neo

我有一个与py2neo的neo4j Cypher有关的基本操作。我必须从json读取对象,其中json对象结构看起来像

{ "ip":"0.0.2.2",
  "location":"uk",
  "uptime":"30",
  "services:["irqbalance","IPsec","nfsd","iscsi","rdisc","iscsi","irqbalance","sssd"]}

观察属性服务是否包含值列表,我需要将它们用作标签。

这是我的方法,我可以在没有标签的情况下加载json,但无法设置标签。

我的查询:

With {data} As machines
UNWIND machines.Servers as server
MERGE (a{ip:server.ip,uptime:server.uptime,location:server.location})

使用此查询我填充了节点,但是如何在同一查询中设置标签。

1 个答案:

答案 0 :(得分:1)

您无法从参数设置标签。相反,您应该使用标签格式化字符串。

labels = ['Some', 'List', 'With', 'Your', 'Labels']
labels = ':'.join(labels)

query = (
    "With {data} As machines "
    "UNWIND machines.Servers as server "
    "MERGE (a:" + labels + ")"
)

输出:

In [22]: query
Out[22]: 'With {data} As machines UNWIND machines.Servers as server MERGE (a:Some:List:With:Your:Labels)'