Parsing a dbt model in Python in order to extract column names #8668
Unanswered
thomasaarholt
asked this question in
Q&A
Replies: 1 comment
-
If you are using dbt 1.5 or greater, you could utilize programmatic invocations with a Python script like this named # runner.py
from dbt.contracts.graph.manifest import Manifest
from dbt.contracts.results import CatalogArtifact
from dbt.cli.main import dbtRunner, dbtRunnerResult
# initialize
dbt = dbtRunner()
# use 'parse' command to load a Manifest
res: dbtRunnerResult = dbt.invoke(["parse"])
manifest: Manifest = res.result
# use 'docs generate' command to build a catalog
res: dbtRunnerResult = dbt.invoke(["docs", "generate"])
catalog: CatalogArtifact = res.result
for catalog_node in catalog.nodes.values():
# lookup the catalog node within the manifest
manifest_node = manifest.nodes[catalog_node.unique_id]
# use the manifest to get the model path and relation details
print(f"path: {manifest_node.original_file_path}")
print(f"relation_name: {manifest_node.relation_name}")
print(f"database: {manifest_node.database}")
print(f"schema: {manifest_node.schema}")
print(f"alias: {manifest_node.alias}")
# use the catalog to get column details
print("columns:")
for column in catalog_node.columns.values():
print(f"{column.name} ({column.type})")
print() Then run it like this: python runner.py The above assumes you've recently built all your database tables using This script iterates through the catalog nodes, but I think you could also invert it and loop through the manifest nodes instead. I believe Here's some descriptions of the object structures that may be helpful: |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi!
I would like to parse a dbt model in Python, with the sole purpose of extracting the model names. Is there a way I can use the dbt-core library to do this?
Something ala the following dummy code:
Beta Was this translation helpful? Give feedback.
All reactions