Last active
October 24, 2024 21:39
-
-
Save prrao87/65b69ff459b72642fea6e1a618ad9199 to your computer and use it in GitHub Desktop.
Bulk update workflow using `LOAD FROM` and `MERGE` with Polars DataFrames
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import polars as pl | |
import kuzu | |
import shutil | |
shutil.rmtree("test_db", ignore_errors=True) | |
db = kuzu.Database("test_db") | |
conn = kuzu.Connection(db) | |
# Get a JSON object of persons and products purchased | |
data = [ | |
{ | |
"name": "Karissa", | |
"products": ["iPhone", "MacBook"] | |
}, | |
{ | |
"name": "Rhea", | |
"products": ["Airpods", "MacBook"] | |
} | |
] | |
df = pl.DataFrame(data) | |
print(df) | |
conn.execute("CREATE NODE TABLE Person(name STRING, PRIMARY KEY(name))") | |
conn.execute("CREATE NODE TABLE Product(name STRING, PRIMARY KEY(name))") | |
conn.execute("CREATE REL TABLE Purchased(FROM Person TO Product)") | |
conn.execute( | |
""" | |
COPY Person FROM ( | |
LOAD FROM df | |
RETURN name | |
); | |
""" | |
) | |
conn.execute( | |
""" | |
COPY Product FROM ( | |
LOAD FROM df | |
WITH products | |
UNWIND products AS product | |
RETURN DISTINCT product | |
); | |
""" | |
) | |
conn.execute( | |
""" | |
COPY Purchased FROM ( | |
LOAD FROM df | |
WITH * | |
UNWIND products AS product | |
RETURN name, product | |
); | |
""" | |
) | |
conn.execute( | |
""" | |
LOAD FROM df | |
WITH * | |
UNWIND products AS prod | |
MERGE (p:Person {name: name}) | |
MERGE (product:Product {name: prod}) | |
MERGE (p)-[:Purchased]->(product); | |
""" | |
) | |
# Query the database | |
res = conn.execute("MATCH (p:Person)-[:Purchased]->(product:Product) RETURN p.name, product.name;") | |
print(res.get_as_pl()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment