Home > Mobile >  How to create bulk node relationships using py2neo
How to create bulk node relationships using py2neo

Time:07-09

I need to populate a database in neo4j using a json of the following that contains data of some processes. Among them the name of the process, its parents and its children (if any). Here is a part of the json as an example:

[
    {
        "process": "IPTV_Subscriptions", 
        "parents": ["IPTV_Navigation","DeviceCertifications-insertion"],
        "childs": ["villa_iptv", "villa_ott", "villa_calicux"]
    },
    { 
        "process": "IPTV_Navigation", 
        "parents": [],
        "childs": ["IPTV_Subscriptions"],
    },
    {
        "process": "DeviceCertifications-getter", 
        "parents": [],
        "childs": ["DeviceCertifications-insertion"]
    },  
    {
        "process": "DeviceCertifications-insertion",
        "parents": ["DeviceCertifications-getter"],
        "childs": ["IPTV_Subscriptions"]
    }
]

With the following Python code I generated, I found that I can create each node with the processes contained in the json in bulk:

import json
from py2neo import Graph
from py2neo.bulk import create_nodes, create_relationships

graph  = Graph("bolt://localhost:7687", auth = ("yyyy", "xxxx"))

#Opening json
f = open('/app/conf/data.json',)
processs = json.load(f)

data=[]
for i in processs:
    proc=[]
    proc.append(i["process"])
    data.append(proc)

keys = ["process"]
create_nodes(graph.auto(), data, labels={"process"}, keys=keys)

And checking in neo4j, I see that the nodes are already created.

Query result verifying nodes from neo4j desktop

But now I need to make the relationships. For each process, from the json I know which are the parents and children of that node.

I wanted to take the documentation as an enter image description here

  • Related