Area: API
Sub-Area: GraphQL Queries
Applies to: OSS + Cloud
Issue
You want to list all datasets that belong to one platform instance through the GraphQL API. For example, all Snowflake datasets from one instance. Your query returns a validation error, or it returns no results.
You Might Be Asking
- How do I get all datasets for a specific platform instance?
- What GraphQL query filters by platform instance?
- Can I use the UI to discover the correct GraphQL query?
- How do I page through a large number of datasets?
Cause
Three things commonly break this query:
-
platformandplatformInstanceare URN-typed search fields. Filter with full URNs, not bare names such assnowflakeorprod-us-east-1. - The platform instance field on a dataset is
dataPlatformInstance { instanceId }. There is noplatformInstancefield to select. -
SearchResult.entityis theEntityinterface. It only hasurn,typeandrelationships. Fields such asnamemust go inside an inline fragment like... on Dataset { name }.
The filters argument on search is deprecated. Use orFilters.
A platform instance is an optional string that you set with platform_instance in the ingestion recipe. You choose the value. It must be unique within the platform. Not every dataset has one.
Solution
Build the filter values
| Filter field | Value format | Example |
|---|---|---|
platform |
Data platform URN | urn:li:dataPlatform:snowflake |
platformInstance |
urn:li:dataPlatformInstance:(<data platform urn>,<instance>) |
urn:li:dataPlatformInstance:(urn:li:dataPlatform:snowflake,prod_us) |
<instance> is the platform_instance value from your recipe.
Method 1: Copy the query from the UI
- Open DataHub in your browser.
- Open Developer Tools and go to the Network tab.
- In DataHub, search and filter by platform instance.
- Find the GraphQL request and copy its payload.
The payload shows the exact URN values the UI sends.
Method 2: Query with search
Use search for small result sets. Send the request as a POST to /api/graphql. The endpoint only accepts POST. See GraphQL setup.
query {
search(
input: {
type: DATASET
query: "*"
start: 0
count: 20
orFilters: [
{
and: [
{ field: "platform", values: ["urn:li:dataPlatform:snowflake"] }
{
field: "platformInstance"
values: ["urn:li:dataPlatformInstance:(urn:li:dataPlatform:snowflake,prod_us)"]
}
]
}
]
}
) {
start
count
total
searchResults {
entity {
urn
type
... on Dataset {
name
dataPlatformInstance {
instanceId
}
}
}
}
}
}
The two criteria inside one and list are combined with AND. Separate entries in orFilters are combined with OR. See the search guide.
Method 3: Page through all results with scrollAcrossEntities
DataHub docs say search* queries are for shallow pagination (under about 50 results) and can't page beyond 10,000 results. For large sets, use scrollAcrossEntities. See deep pagination.
ScrollAcrossEntitiesInput requires query. It has no filters field and no start field. Sending either one fails validation. Sort by urn so the order stays stable.
The script below posts to the GraphQL endpoint with an access token. GraphQL requests authenticate with the header Authorization: Bearer <access-token>. A personal access token carries your privileges. Set GRAPHQL_URL to your instance, for example https://<your-instance>.acryl.io/api/graphql. See authentication.
import requests
GRAPHQL_URL = "https://<your-instance>.acryl.io/api/graphql"
TOKEN = "<access-token>"
QUERY = """
query scroll($input: ScrollAcrossEntitiesInput!) {
scrollAcrossEntities(input: $input) {
nextScrollId
total
searchResults {
entity {
urn
... on Dataset {
name
dataPlatformInstance { instanceId }
}
}
}
}
}
"""
input_ = {
"types": ["DATASET"],
"query": "*",
"count": 100,
"orFilters": [
{
"and": [
{"field": "platform", "values": ["urn:li:dataPlatform:snowflake"]},
{
"field": "platformInstance",
"values": [
"urn:li:dataPlatformInstance:(urn:li:dataPlatform:snowflake,prod_us)"
],
},
]
}
],
"sortInput": {"sortCriteria": [{"field": "urn", "sortOrder": "ASCENDING"}]},
}
datasets = []
while True:
response = requests.post(
GRAPHQL_URL,
json={"query": QUERY, "variables": {"input": input_}},
headers={"Authorization": f"Bearer {TOKEN}"},
)
body = response.json()
if body.get("errors"):
raise RuntimeError(body["errors"])
page = body["data"]["scrollAcrossEntities"]
datasets.extend(r["entity"] for r in page["searchResults"])
if not page["nextScrollId"]:
break
input_["scrollId"] = page["nextScrollId"]
print(f"Found {len(datasets)} datasets")
Check the response body for errors on every request. Leave out scrollId on the first request. On each later request, pass the nextScrollId from the previous page as scrollId. Stop when nextScrollId is null. If the total is an exact multiple of count, you get one extra request that returns no results.
With minimal fields, a page can return 1,000 to 2,000 results. For complex requests with many nested objects, lower count to 20 to 25. See page size guidance.
Method 4: DataHub CLI
Use a recent CLI. The query is positional:
datahub search "*" --filter entity_type=dataset --filter platform=snowflake
The CLI converts a bare platform name such as snowflake to the platform URN. The recognized filter keys are entity_type, entity_subtype, platform, domain, container, env, owner, tag, glossary_term and status. Platform instance isn't one of them. To filter by platform instance, use the GraphQL queries above.
datahub get takes a single URN, with --urn or as the first argument, and an optional --aspect.