Search code examples
pythonjsonpython-3.xneo4jpy2neo

How to create bulk node relationships using py2neo


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 example:

from py2neo import Graph
from py2neo.bulk import create_relationships
g = Graph()
data = [
    (("Alice", "Smith"), {"since": 1999}, "ACME"),
    (("Bob", "Jones"), {"since": 2002}, "Bob Corp"),
    (("Carol", "Singer"), {"since": 1981}, "The Daily Planet"),
]
create_relationships(g.auto(), data, "WORKS_FOR", start_node_key=("Person", "name", "family name"), end_node_key=("Company", "name"))

But it didn't work for me

Having from the json the information of the parents and children, does anyone have an idea of how I can generate the massive relationships? In view of the json example I have, the relationship tags would be ParentOf and ChildOf but I have no idea how they would be generated from python.


Solution

  • Below is the script to create the bulk relationship using py2neo. Let me know if it works for you or not. Another thing, please label your nodes as Process rather than process (notice the upper case P). Then I use the relationship :CHILD_OF. If you want :PARENT_OF then change the tuple in data and swap the first and third item.

    import json
    from py2neo import Graph
    from py2neo.bulk import create_relationships
    graph  = Graph("neo4j://localhost:7687", auth = ("neo4j", "neo4jay"))
    
    #Opening json
    f = open('data2.json',)
    processs = json.load(f)
    
    data=[]
    for i in processs:
        for p in i["parents"]:
            data.append((i["process"],{},p))
    
    create_relationships(graph.auto(), data, "CHILD_OF", start_node_key=("Process", "process"), end_node_key=("Process", "process"))
    

    Result:

    enter image description here