Regional Sales Analytics¶
Combining Graph Traversal with Columnar Aggregation.
In [1]:
Copied!
import os
import shutil
import tempfile
import uni_db
import os
import shutil
import tempfile
import uni_db
In [2]:
Copied!
db_path = os.path.join(tempfile.gettempdir(), "sales_db")
if os.path.exists(db_path):
shutil.rmtree(db_path)
db = uni_db.Database(db_path)
print(f"Opened database at {db_path}")
db_path = os.path.join(tempfile.gettempdir(), "sales_db")
if os.path.exists(db_path):
shutil.rmtree(db_path)
db = uni_db.Database(db_path)
print(f"Opened database at {db_path}")
Opened database at /tmp/sales_db
1. Schema¶
In [3]:
Copied!
db.create_label("Region")
db.create_label("Order")
db.create_edge_type("SHIPPED_TO", ["Order"], ["Region"])
db.add_property("Region", "name", "string", False)
db.add_property("Order", "amount", "float64", False)
db.create_label("Region")
db.create_label("Order")
db.create_edge_type("SHIPPED_TO", ["Order"], ["Region"])
db.add_property("Region", "name", "string", False)
db.add_property("Order", "amount", "float64", False)
2. Ingest Data¶
One region, 100 orders.
In [4]:
Copied!
vids_region = db.bulk_insert_vertices("Region", [{'name': 'North'}])
north = vids_region[0]
orders = [{'amount': 10.0 * (i + 1)} for i in range(100)]
vids_orders = db.bulk_insert_vertices("Order", orders)
edges = [(v, north, {}) for v in vids_orders]
db.bulk_insert_edges("SHIPPED_TO", edges)
db.flush()
vids_region = db.bulk_insert_vertices("Region", [{'name': 'North'}])
north = vids_region[0]
orders = [{'amount': 10.0 * (i + 1)} for i in range(100)]
vids_orders = db.bulk_insert_vertices("Order", orders)
edges = [(v, north, {}) for v in vids_orders]
db.bulk_insert_edges("SHIPPED_TO", edges)
db.flush()
3. Analytical Query¶
Sum of amounts for orders in a region.
In [5]:
Copied!
query = "MATCH (r:Region {name: 'North'})<-[:SHIPPED_TO]-(o:Order) RETURN SUM(o.amount) as total"
results = db.query(query)
print(f"Total Sales for North Region: {results[0]['total']}")
query = "MATCH (r:Region {name: 'North'})<-[:SHIPPED_TO]-(o:Order) RETURN SUM(o.amount) as total"
results = db.query(query)
print(f"Total Sales for North Region: {results[0]['total']}")
Total Sales for North Region: 50500.0