I an using py2neo to create a KG from external database.
Is there a way to create nodes + relationships in bulk ?
My code looks like:
import py2neo as pn
for k in range(0,n):
id = xxx
node_properties = {...}
node = pn.Node(label, id=id, **node_properties)
and sth similar for the relationships.
For very large sets, it takes ages to finish, as each node is create separately.
Is there a way to upload a table directly as a bulk and not go through the loop ?
Another question:
Is it possible to pass the labels (node class) for example as variable ?
I'd like to create nodes with different labels dynamically, not just the attributes.
I tried to pass something like
numbers = [{"labels": "NodeClass", "rel": "relation", "parent_id" : 4435, "value": random.random(), "name": generate_random_string()} for _ in range(k)]
query = """
WITH $nodes AS batch
UNWIND batch AS node
MATCH (g {global_id: node.parent_id})
CREATE (g)-[r:rel]->(n:node.labels)
SET n.value = node.value, n.name = node.name
"""
# SET n:node.labels
session = driver.session()
result = session.run(query, nodes=numbers)
but it seems neo4j doesn't allow passing labels as variables...
Yes, you cannot use a variable for a label when matching or creating a node. You can use an apoc procedure to create a node and specify the label(s) dynamically.
Create the node then create the relationship between the two nodes.
You can set a label dynamically starting with version 5.24.