Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
01-17-2023 11:07 AM
Hello:
I am new to Neo4j Python Driver, and I am looking for some help on blogs which I am having difficulty finding the right one which shows the code of MERGE creating new nodes with relationships in Python.
Any help with these links is greatly appreciated.
Thanks
Solved! Go to Solution.
01-26-2023 03:43 AM
Have a look at the UNWIND clause in Cypher. That might be what you're looking for.
Something along the lines of this maybe?
import neo4j
URI = "neo4j://localhost:7687"
AUTH = ("neo4j", "pass")
def work(tx, query, **kwargs):
return list(tx.run(query, **kwargs))
with neo4j.GraphDatabase.driver(URI, auth=AUTH) as driver:
with driver.session(database="neo4j") as session:
nets = [5, 5, 69, 45, 10, 42, 58, 86, 93, 43, 57, 75, 75, 27, 90, 24,
27, 5, 24, 45, 4]
result = session.execute_write(
work,
"UNWIND $nets AS net "
"MERGE (n:Network {net: net}) "
"RETURN n",
nets=nets
)
print(result)
01-17-2023 05:28 PM
https://neo4j.com/docs/python-manual/current/query-simple/#_write_to_the_database has multiple examples of MERGE and with a single node and with relationships
01-18-2023 08:03 PM
Thanks for your reply. I am using MERGE to create Node along with property as shown below. The problem is that one node is created with all the property values rather than creating that many nodes. I am passing "net" as my dataframe column in MERGE but only one node is created. Not sure what I am missing?
net |
5 |
5 |
69 |
45 |
10 |
42 |
58 |
86 |
93 |
43 |
57 |
75 |
75 |
27 |
90 |
24 |
27 |
5 |
24 |
45 |
4 |
01-18-2023 08:18 PM
As I mentioned above, from below you can see that one node is created instead of multiple nodes. Any help is greatly appreciated.
01-26-2023 03:43 AM
Have a look at the UNWIND clause in Cypher. That might be what you're looking for.
Something along the lines of this maybe?
import neo4j
URI = "neo4j://localhost:7687"
AUTH = ("neo4j", "pass")
def work(tx, query, **kwargs):
return list(tx.run(query, **kwargs))
with neo4j.GraphDatabase.driver(URI, auth=AUTH) as driver:
with driver.session(database="neo4j") as session:
nets = [5, 5, 69, 45, 10, 42, 58, 86, 93, 43, 57, 75, 75, 27, 90, 24,
27, 5, 24, 45, 4]
result = session.execute_write(
work,
"UNWIND $nets AS net "
"MERGE (n:Network {net: net}) "
"RETURN n",
nets=nets
)
print(result)
All the sessions of the conference are now available online