Harpal
03/14/2022, 4:12 PMdbt run
commands and parsing command line arguments/variables.
Please see the comments for more info dabparrotHarpal
03/14/2022, 4:14 PMdbt run
command is as follows:
my_dbt_resource = dbt_cli_resource.configured({"project_dir": "/path/to/my/working/directory"})
@job(resource_defs={"dbt": my_dbt_resource})
def my_dbt_job():
dbt_run_op()
However I’m looking to run the following dbt command in Dagster…
`dbt run --vars '{"my_variable": "0.2"}' -m +final_model_in_dbt_dag`
It runs the dbt model named final_model_in_dbt_dag
and all of it’s parent nodes in the DAG while passing in a command line variable my_variable
(essentially running the DAG from start to end)
Does anyone know how to structure the op/job such that it takes a the dbt --vars
argument via dagster?owen
03/14/2022, 4:18 PMdbt_cli_resource
can be configured with flags that will be passed into all the underlying commands, so in your example, you would have:
my_dbt_resource = dbt_cli_resource.configured(
{
"project_dir": "/path/to/my/working/directory",
"vars": {"my_variable": 0.2},
"m": "+final_model_in_dbt_dag",
}
)
Harpal
03/14/2022, 4:19 PMHarpal
03/14/2022, 5:53 PMHarpal
03/14/2022, 5:56 PMowen
03/14/2022, 6:00 PMdbt run
) in order to create all those assets. If you scroll down a little bit in that link you'll see how to model this with software defined assets (which allow you to visualize those dependencies on top of the underlying operation). That would be using the load_assets_from_dbt_project
function.owen
03/14/2022, 6:00 PMHarpal
03/14/2022, 6:48 PMfrom dagster_dbt import dbt_cli_resource, load_assets_from_dbt_project
DBT_PROJECT_DIR = "/path/to/my/dbt/project/dir"
dbt_assets = load_assets_from_dbt_project(DBT_PROJECT_DIR)
analytics_assets = AssetGroup(
dbt_assets,
resource_defs={
"dbt": dbt_cli_resource.configured(DBT_CONFIG),
},
)
I’ve put the code snippet in its own assets.py
file (a you did in the video) in the same directory as the previously mentioned dbt_job.py
file but there were no observable changes to the dagit UI.
Have I missed anything here?
The Assets
page of dagit has all 8 tables by name but the materializations page is bare (See screenshot below).owen
03/14/2022, 6:51 PMHarpal
03/14/2022, 6:55 PMassets.py
in the Jobs
menu on the left hand side of the Dagit UI.
Good to know that I’m on the right tracks though! I’ll keep digging.owen
03/14/2022, 6:57 PManalytics_assets = AssetGroup(
dbt_assets,
resource_defs={
"dbt": dbt_cli_resource.configured(DBT_CONFIG),
},
).build_job("my_job")
owen
03/14/2022, 6:58 PMHarpal
03/14/2022, 7:19 PMmy_job
to show up 😕
Not even an error message.
*Must be a problem with my config
from dagster_dbt import dbt_cli_resource, load_assets_from_dbt_project
DBT_PROJECT_DIR = "/path/to/my/dbt/project/dir"
dbt_assets = load_assets_from_dbt_project(DBT_PROJECT_DIR)
analytics_assets = AssetGroup(
dbt_assets,
resource_defs={
"dbt": dbt_cli_resource.configured(DBT_CONFIG),
},
).build_job("my_job")
owen
03/14/2022, 8:00 PMowen
03/14/2022, 8:01 PMHarpal
03/14/2022, 8:27 PMDBT_CONFIG
?owen
03/14/2022, 8:29 PM{"project_dir": "path to your dbt project"}
owen
03/14/2022, 8:29 PMload_assets_from_dbt_project
🤔Harpal
03/14/2022, 8:40 PMrepo.py
file from path.to.assets import analytics_assets
2. Added the new assets_analytics
job it to the bottom of this list:
@repository
def repo():
return [
a_list,
of_all,
jobs,
analytics_assets,
]
Then i’m greeted with this behemoth of an error/warning message…
(Removing the error messages stops the error messages but still doesn’t show the new job.)
2022-03-14 20:35:08 +0000 - dagster.daemon.SchedulerDaemon - WARNING - Could not load location repo.py to check for schedules due to the following error: dagster_dbt.errors.DagsterDbtCliFatalRuntimeError: Fatal error in the dbt CLI (return code 2)
Stack Trace:
File "/Users/user/.pyenv/versions/3.9.8/envs/machine-learning-3.9.8/lib/python3.9/site-packages/dagster/grpc/server.py", line 212, in __init__
self._loaded_repositories = LoadedRepositories(
File "/Users/user/.pyenv/versions/3.9.8/envs/machine-learning-3.9.8/lib/python3.9/site-packages/dagster/grpc/server.py", line 97, in __init__
loadable_targets = get_loadable_targets(
File "/Users/user/.pyenv/versions/3.9.8/envs/machine-learning-3.9.8/lib/python3.9/site-packages/dagster/grpc/utils.py", line 27, in get_loadable_targets
else loadable_targets_from_python_file(python_file, working_directory)
File "/Users/user/.pyenv/versions/3.9.8/envs/machine-learning-3.9.8/lib/python3.9/site-packages/dagster/core/workspace/autodiscovery.py", line 18, in loadable_targets_from_python_file
loaded_module = load_python_file(python_file, working_directory)
File "/Users/user/.pyenv/versions/3.9.8/envs/machine-learning-3.9.8/lib/python3.9/site-packages/dagster/core/code_pointer.py", line 79, in load_python_file
return import_module_from_path(module_name, python_file)
File "/Users/user/.pyenv/versions/3.9.8/envs/machine-learning-3.9.8/lib/python3.9/site-packages/dagster/seven/__init__.py", line 47, in import_module_from_path
spec.loader.exec_module(module)
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "dagster/repo.py", line 25, in <module>
from dagster.sector_classification.jobs.assets import analytics_assets
File "/Users/hdot/vs_code/machine-learning/dagster/sector_classification/jobs/assets.py", line 5, in <module>
dbt_assets = load_assets_from_dbt_project(DBT_PROJECT_DIR)
File "/Users/hdot/.pyenv/versions/3.9.8/envs/machine-learning-3.9.8/lib/python3.9/site-packages/dagster_dbt/asset_defs.py", line 172, in load_assets_from_dbt_project
manifest_json, cli_output = _load_manifest_for_project(
File "/Users/hdot/.pyenv/versions/3.9.8/envs/machine-learning-3.9.8/lib/python3.9/site-packages/dagster_dbt/asset_defs.py", line 18, in _load_manifest_for_project
cli_output = execute_cli(
File "/Users/hdot/.pyenv/versions/3.9.8/envs/machine-learning-3.9.8/lib/python3.9/site-packages/dagster_dbt/cli/utils.py", line 90, in execute_cli
raise DagsterDbtCliFatalRuntimeError(logs=logs, raw_output=raw_output)
2022-03-14 20:36:10,931 35121 utils Executing command: dbt --log-format json ls --project-dir /Users/user/machine-learning/dbt --profiles-dir /Users/user/machine-learning/dbt/config --select * --resource-type model --output json
2022-03-14 20:36:12 +0000 - dagster.builtin - ERROR - Encountered an error while reading the project:
2022-03-14 20:36:12,146 35121 utils Encountered an error while reading the project:
2022-03-14 20:36:12 +0000 - dagster.builtin - ERROR - ERROR: Runtime Error
Could not find profile named 'moonfire_dbt'
2022-03-14 20:36:12,150 35121 utils ERROR: Runtime Error
Could not find profile named 'moonfire_dbt'
2022-03-14 20:36:12 +0000 - dagster.builtin - ERROR - Encountered an error:
Runtime Error
Could not run dbt
2022-03-14 20:36:12,159 35121 utils Encountered an error:
Runtime Error
Could not run dbt
2022-03-14 20:36:12,240 35121 utils dbt exited with return code 2
2022-03-14 20:36:12,243 35121 api Started Dagster code server for file dagster/repo.py in process 35121
Harpal
03/14/2022, 8:56 PMrepo.py
.owen
03/14/2022, 9:04 PM2022-03-14 20:36:10,931 35121 utils Executing command: dbt --log-format json ls --project-dir /Users/hdot/vs_code/machine-learning/dbt --profiles-dir /Users/hdot/vs_code/machine-learning/dbt/config --select * --resource-type model --output json
2022-03-14 20:36:12 +0000 - dagster.builtin - ERROR - Encountered an error while reading the project:
2022-03-14 20:36:12,146 35121 utils Encountered an error while reading the project:
2022-03-14 20:36:12 +0000 - dagster.builtin - ERROR - ERROR: Runtime Error
Could not find profile named 'moonfire_dbt'
2022-03-14 20:36:12,150 35121 utils ERROR: Runtime Error
Could not find profile named 'moonfire_dbt'
owen
03/14/2022, 9:05 PMmoonfire_dbt
profile is not available in ...machine-learning/dbt/config
owen
03/14/2022, 9:06 PMdbt ls --project-dir /Users/hdot/vs_code/machine-learning/dbt --profiles-dir /Users/hdot/vs_code/machine-learning/dbt/config --select * --resource-type model --output json
is the command that needs to succeedHarpal
03/15/2022, 3:58 PMowen
03/15/2022, 4:11 PMHarpal
03/15/2022, 4:28 PManalytics_assets
to the list of jobs/sensors/assets/scehdules etc.
• I had to move my profiles.yml
file into a new directory called`dbt/config` (not sure If I can change this default)
• add the following line of code to assets.py
from dagster import AssetGroup
I totally missed the import statement - my bad.
The dagster command being ran in the Dagit launchpad automatically assumed that the asset exists in dbt/config
. Not sure if that’s a feature or a bug tbh. But was fairly easy to debug once DBT and dagster were setup correctly.