Querying Your Graph
Tutorial
Querying Your Graph
Execute Cypher queries and work with results
What You'll Learn
- Basic Queries - Execute MATCH and RETURN statements
- Parameters - Use parameterized queries safely
- Result Handling - Process query results as rows or DataFrames
- Aggregations - Use COUNT, SUM, COLLECT and other aggregations
# Cell 1 — ParametersUSERNAME = "_FILL_ME_IN_" # Set your email before running# Cell 2 — Connectfrom graph_olap import GraphOLAPClientclient = GraphOLAPClient(username=USERNAME)
# Cell 3 — Provisionfrom notebook_setup import provisionpersonas, conn = provision(USERNAME)analyst = personas["analyst"]admin = personas["admin"]ops = personas["ops"]client = analyst
print(f"Connected | {conn.query_scalar('MATCH (n) RETURN count(n)')} nodes")
1
Basic Query Execution
Using conn.query()
# Basic query executionresult = conn.query(""" MATCH (c:Customer) RETURN c.id AS name, c.bk_sectr AS sector ORDER BY c.id LIMIT 5""")
result.show()
2
Parameterized Queries
Safe parameter passing
# Parameterized queries (safe from injection)result = conn.query( "MATCH (c:Customer {id: $name}) RETURN c.bk_sectr AS sector", parameters={"name": "MR LAU XIAOMING"})result.show()
# Never do this (Cypher injection risk):# conn.query(f"MATCH (c) WHERE c.id = '{user_input}' RETURN c")
3
Working with Results
Rows, scalars, and DataFrames
# Get results as a Polars DataFramedf = conn.query_df(""" MATCH (a:Customer)-[:SHARES_ACCOUNT]->(b:Customer) RETURN a.id AS customer, b.id AS shares_account_with ORDER BY a.id""")
df
4
Aggregation Queries
Counting, summing, collecting
# Get single value with query_scalartotal = conn.query_scalar("MATCH (c:Customer) RETURN count(c)")print(f"Total customers: {total}")
# Multiple aggregationsresult = conn.query(""" MATCH (c:Customer) RETURN count(c) AS total, count(DISTINCT c.bk_sectr) AS sectors, count(DISTINCT c.acct_stus) AS id_types""")row = result.rows[0]print(f"Customers: {row[0]}, Sectors: {row[1]}, ID types: {row[2]}")Key Takeaways
conn.query()executes Cypher and returnsQueryResult- Always use parameters for user input (prevents injection)
query_df()returns Polars DataFrame for analysisquery_scalar()returns single value for aggregations