Graph examples
Declare a relationship in the schema before using these requests. Start with direct neighbors, then choose a bounded traversal, path calculation or whole-graph analysis. Several algorithms require a table that relates to itself.
- Declare the relationDefine how one record links to another.
- Load the named sampleUse the records required by the chosen recipe.
- Ask a graph questionInspect direct links, paths, groups or relative scores.
- Read the result shapeCheck IDs, direction and traversal limits before using the result.
Follow and analyze connections
- Find directly linked records — Read records one relationship away.
- Find records that link to a target — Follow a relationship backwards to find rows that point to a selected record.
- Explore nearby connections — Breadth-first search visits nearby records before more distant ones.
- Check whether records are connected — Check for a directed path between two records within a chosen number of hops.
- Find the lowest path cost — Dijkstra’s algorithm finds the total cost of the cheapest route.
- List routes without repeated nodes — Find routes between two records without repeating a node inside each route.
- Rank a chosen set of linked records — PageRank estimates importance from incoming links.
- Find connected groups of three — Find triples where every pair is linked.
- Separate disconnected groups — Find groups connected within themselves but disconnected from each other.
- Find closely connected communities — Louvain groups records with more links inside their group than between groups.
- Group records using their neighbors — Label propagation repeatedly lets nearby nodes adopt a common label.
- Find records that connect groups — Betweenness measures how often a node lies on shortest paths between other nodes.
- Find influential neighbors — Eigenvector centrality scores a node higher when it connects to other high-scoring nodes.
- Find the best few routes — Return up to k routes ordered by cost when an application needs alternatives rather than one distance.
- Sample a route through the graph — A random walk chooses one neighbor at each step.
- Find nodes with similar graph roles — Node2Vec learns embeddings from graph walks.
- Combine links with row features — GraphSAGE learns embeddings from relationships and numeric row features.
Prepare the social graph
The community and graph-learning recipes use social.users, a relation named follows, and IDs such as alice and erin. This is different from the following relation used in the profile-writing example. Use a disposable test database: do not replace an existing social.users definition.
Run this Bash setup once using your tenant-scoped connection. It creates five people and their links. The four numbers in feat are deliberately small sample features for the GraphSAGE recipe, not real user attributes.
Create and load the five sample people
curl --fail-with-body -X POST "https://$OC_HOST/v1/tenants/$OC_TENANT/schemas" \
-H "Authorization: Bearer $OC_TOKEN" -H "Content-Type: text/plain" \
--data-binary @- <<'TOML'
namespace = "social"
table = "users"
primary_key = ["id"]
[[columns]]
name = "id"
ty = "str"
required = true
[[columns]]
name = "follows"
ty = "list"
[[columns]]
name = "feat"
ty = "list"
[[relations]]
name = "follows"
from_col = "follows"
bidirectional = true
target = { namespace = "social", table = "users", pk = "id" }
TOML
curl --fail-with-body -X POST "https://$OC_HOST/v1/tenants/$OC_TENANT/rows/social.users/_batch" \
-H "Authorization: Bearer $OC_TOKEN" -H "Content-Type: application/json" \
-d '[
{"id":"alice","follows":["bob","carol"],"feat":[1.0,0.0,0.5,0.2]},
{"id":"bob","follows":["carol","dave"],"feat":[0.9,0.1,0.4,0.2]},
{"id":"carol","follows":["alice","dave"],"feat":[0.8,0.2,0.6,0.1]},
{"id":"dave","follows":["erin"],"feat":[0.1,0.9,0.2,0.8]},
{"id":"erin","follows":[],"feat":[0.0,1.0,0.1,0.9]}
]'Alice can reach Erin through Bob or Carol, then Dave. Erin has no outgoing links. This small graph helps check direction, endpoints and response shape; it is not a meaningful benchmark or a dataset for judging a trained model's quality. Scores in individual recipes illustrate their format and need not match this sample dataset.
Prepare orders linked to products
The neighbor, reverse, BFS and path recipes use order o001, product p001, and the relation bought_product. Create this separate sample dataset in a test database. It uses o001 without an underscore; the SQL examples use different IDs.
Create one product and the order that points to it
curl --fail-with-body -X POST "https://$OC_HOST/v1/tenants/$OC_TENANT/schemas" \
-H "Authorization: Bearer $OC_TOKEN" -H "Content-Type: text/plain" \
--data-binary @- <<'TOML'
namespace = "shop"
table = "products"
primary_key = ["id"]
[[columns]]
name = "id"
ty = "str"
required = true
TOML
curl --fail-with-body -X POST "https://$OC_HOST/v1/tenants/$OC_TENANT/schemas" \
-H "Authorization: Bearer $OC_TOKEN" -H "Content-Type: text/plain" \
--data-binary @- <<'TOML'
namespace = "shop"
table = "orders"
primary_key = ["id"]
[[columns]]
name = "id"
ty = "str"
required = true
[[columns]]
name = "product_id"
ty = "str"
[[relations]]
name = "bought_product"
from_col = "product_id"
bidirectional = true
target = { namespace = "shop", table = "products", pk = "id" }
TOML
curl --fail-with-body -X POST "https://$OC_HOST/v1/tenants/$OC_TENANT/rows/shop.products" \
-H "Authorization: Bearer $OC_TOKEN" -H "Content-Type: application/json" \
-d '{"id":"p001"}'
curl --fail-with-body -X POST "https://$OC_HOST/v1/tenants/$OC_TENANT/rows/shop.orders" \
-H "Authorization: Bearer $OC_TOKEN" -H "Content-Type: application/json" \
-d '{"id":"o001","product_id":"p001"}'The forward lookup starts at o001 and returns p001. The reverse lookup starts at p001 and returns o001. Larger response examples on the recipe pages require more rows and links.
Before adapting a recipe
Use a test database, load the named sample data, and check the expected result. Read each page’s limits before changing the query. See connection and SDK notes.