cancel
Showing results for 
Search instead for 
Did you mean: 

multiple relationships in shipping graph noe4j using python

laureee777
Node Link

I need help with creating many:many relations in neo4j using python. My database is about shipping products from supplier to buyer. Now I am stuck with creating relations which would describe path of supplied product. My code:

pip install neo4j
pip install py2neo
pip install graphdatascience
from graphdatascience import GraphDataScience
from neo4j import GraphDatabase
import logging
import sys
driver = GraphDatabase.driver("neo4j://localhost:7687",
                              auth=("neo4j", "test"))

def create_country(tx, name,continent,population_mln):
    tx.run("CREATE (a:Country {name: $name,continent: $continent,population_mln: $population_mln})", name=name,continent=continent,population_mln=population_mln)

def create_shop(tx, name,founded,revenue_mlrd):
    tx.run("CREATE (b:Shop {name: $name,founded: $founded,revenue_mlrd: $revenue_mlrd})", name=name,founded=founded,revenue_mlrd=revenue_mlrd)

def create_buyer(tx, name,birth_date,purchases):
    tx.run("CREATE (c:Buyer {name: $name,birth_date: $birth_date,purchases: $purchases})", name=name,birth_date=birth_date,purchases=purchases)

def operates_from(tx, name, shop_name):
    tx.run("MERGE (b:Shop {name: $shop_name}) "
           "MERGE (a:Country {name: $name}) "
           "MERGE (b)-[:OPERATES_from]->(a)",
           name=name, shop_name=shop_name)
def buys_from(tx, name, buyer_name):
    tx.run("MERGE (b:Shop {name: $name}) "
           "MERGE (c:Buyer {name: $buyer_name}) "
           "MERGE (c)-[:buys_from]->(b)",
           name=name, buyer_name=buyer_name)
def lives_in(tx, name, buyer_name):
    tx.run("MERGE (a:Country {name: $name}) "
           "MERGE (c:Buyer {name: $buyer_name}) "
           "MERGE (c)-[:lives_in]->(a)",
           name=name, buyer_name=buyer_name)
with driver.session() as session:
    session.execute_write(create_country, "China","Asia",1412)
    session.execute_write(create_country, "US","North America",331.9)
    session.execute_write(create_country, "UK","Europe",67)
    session.execute_write(create_country, "Poland","Europe",37)
    session.execute_write(create_country, "Lithuania","Europe",2.7)
    session.execute_write(create_country, "Turkey",["Europe","Asia"],85)
    session.execute_write(create_country, "Latvia","Europe",1.8)
    session.execute_write(create_country, "Slovakia","Europe",5.4)
    session.execute_write(create_country, "Spain","Europe",47.3)
    session.execute_write(create_country, "Germany","Europe",83.1)
    session.execute_write(create_shop, "Amazon",1994,469.8)
    session.execute_write(create_shop, "Ebay",1995,10.27)
    session.execute_write(create_shop, "Wish",2011,2.1)
    session.execute_write(create_shop, "AliExpress",2010,134000.5)
    session.execute_write(create_shop, "JD.com",1998,951.6)
    session.execute_write(create_buyer, "Tomas Tomaitis",1998,20)
    session.execute_write(create_buyer, "John Iglesias",1973,2)
    session.execute_write(create_buyer, "Ann Milosh",1989,113)
    session.execute_write(create_buyer, "Saule Saulyte",2000,13)
    session.execute_write(create_buyer, "Sam Booth",1968,1)
    session.execute_write(operates_from, "US", "Amazon")
    session.execute_write(operates_from, "US", "Ebay")
    session.execute_write(operates_from, "US", "Wish")
    session.execute_write(operates_from, "China", "AliExpress")
    session.execute_write(operates_from, "China", "JD.com")
    session.execute_write(create_buyer, "Janis Timma",1989,5)
    session.execute_write(buys_from, "Amazon", "Tomas Tomaitis")
    session.execute_write(buys_from, "Amazon", "Ann Milosh")
    session.execute_write(buys_from, "Wish", "Saule Saulyte")
    session.execute_write(buys_from, "JD.com", "John Iglesias")
    session.execute_write(buys_from, "Ebay", "Sam Booth")
    session.execute_write(buys_from, "AliExpress", "Janis Timma")
    session.execute_write(lives_in, "Latvia","Janis Timma")
    session.execute_write(lives_in, "Lithuania","Saule Saulyte")
    session.execute_write(lives_in, "Spain","John Iglesias")
    session.execute_write(lives_in, "US","Sam Booth")
    session.execute_write(lives_in, "Slovakia","Ann Milosh")
    session.execute_write(lives_in, "Lithuania","Tomas Tomaitis")
    session.execute_write(shipping_through, "US","Janis Timma")
    session.execute_write(lives_in, "Lithuania","Saule Saulyte")
    session.execute_write(lives_in, "Spain","John Iglesias")
    session.execute_write(lives_in, "US","Sam Booth")
    session.execute_write(lives_in, "Slovakia","Ann Milosh")
    session.execute_write(lives_in, "Lithuania","Tomas Tomaitis")

driver.close()

What I need to do is to create relationships through which countries product was shipped. For example from Amazon parcell is shipped to Spain and them shipped to final location Slovakia.

(:Shop)-[:SHIPPING ]->(:Country1)-[:SHIPPING TO]->(:Country2)

1 REPLY 1

glilienfield
Ninja
Ninja

Unlike sql, there is nothing special about many-to-many relationships in neo4j. They are just independent relationships between nodes. In your case, a buyer can purchase from many sellers and a seller can have many buyers. All you need to do is create something like a BUYS_FROM relationship from each buyer to each seller that buyer purchase from. This will give you the equivalent of a many-to-many relationship, as you can start from any buyer or seller mode and find all the related nodes to the other node type. There is no need for a join table like required for relational databases.

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.