-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use AssetGroup in hacker news demo (#6965)
- Loading branch information
Showing
8 changed files
with
116 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from hacker_news_assets.resources import RESOURCES_LOCAL, RESOURCES_PROD, RESOURCES_STAGING | ||
|
||
from dagster import AssetGroup, in_process_executor | ||
|
||
prod_assets = AssetGroup.from_package_name(__name__, resource_defs=RESOURCES_PROD) | ||
staging_assets = AssetGroup.from_package_name(__name__, resource_defs=RESOURCES_STAGING) | ||
local_assets = AssetGroup.from_package_name( | ||
__name__, resource_defs=RESOURCES_LOCAL, executor_def=in_process_executor | ||
) |
39 changes: 39 additions & 0 deletions
39
examples/hacker_news_assets/hacker_news_assets/assets/activity_stats.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import json | ||
import os | ||
|
||
import pandas as pd | ||
from dagster_dbt import dbt_cli_resource | ||
from dagster_dbt.asset_defs import load_assets_from_dbt_manifest | ||
from hacker_news_assets.resources.snowflake_io_manager import ( | ||
SHARED_SNOWFLAKE_CONF, | ||
connect_snowflake, | ||
) | ||
|
||
from dagster import MetadataValue | ||
from dagster.utils import file_relative_path | ||
|
||
DBT_PROJECT_DIR = file_relative_path(__file__, "../../hacker_news_dbt") | ||
DBT_PROFILES_DIR = DBT_PROJECT_DIR + "/config" | ||
dbt_staging_resource = dbt_cli_resource.configured( | ||
{"profiles-dir": DBT_PROFILES_DIR, "project-dir": DBT_PROJECT_DIR, "target": "staging"} | ||
) | ||
dbt_prod_resource = dbt_cli_resource.configured( | ||
{"profiles_dir": DBT_PROFILES_DIR, "project_dir": DBT_PROJECT_DIR, "target": "prod"} | ||
) | ||
|
||
|
||
def asset_metadata(_context, model_info): | ||
config = dict(SHARED_SNOWFLAKE_CONF) | ||
config["schema"] = model_info["schema"] | ||
with connect_snowflake(config=config) as con: | ||
df = pd.read_sql(f"SELECT * FROM {model_info['name']} LIMIT 5", con=con) | ||
num_rows = con.execute(f"SELECT COUNT(*) FROM {model_info['name']}").fetchone() | ||
|
||
return {"Data sample": MetadataValue.md(df.to_markdown()), "Rows": num_rows[0]} | ||
|
||
|
||
assets = load_assets_from_dbt_manifest( | ||
json.load(open(os.path.join(DBT_PROJECT_DIR, "target", "manifest.json"))), | ||
runtime_metadata_fn=asset_metadata, | ||
io_manager_key="warehouse_io_manager", | ||
) |
58 changes: 9 additions & 49 deletions
58
examples/hacker_news_assets/hacker_news_assets/jobs/activity_stats.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,14 @@ | ||
import json | ||
import os | ||
from hacker_news_assets.assets import prod_assets, staging_assets | ||
|
||
import pandas as pd | ||
from dagster_dbt import dbt_cli_resource | ||
from dagster_dbt.asset_defs import load_assets_from_dbt_manifest | ||
from hacker_news_assets.resources import RESOURCES_PROD, RESOURCES_STAGING | ||
from hacker_news_assets.resources.snowflake_io_manager import ( | ||
SHARED_SNOWFLAKE_CONF, | ||
connect_snowflake, | ||
) | ||
from dagster import AssetGroup, JobDefinition | ||
|
||
from dagster import MetadataValue, build_assets_job | ||
from dagster.utils import file_relative_path | ||
|
||
DBT_PROJECT_DIR = file_relative_path(__file__, "../../hacker_news_dbt") | ||
DBT_PROFILES_DIR = DBT_PROJECT_DIR + "/config" | ||
dbt_staging_resource = dbt_cli_resource.configured( | ||
{"profiles-dir": DBT_PROFILES_DIR, "project-dir": DBT_PROJECT_DIR, "target": "staging"} | ||
) | ||
dbt_prod_resource = dbt_cli_resource.configured( | ||
{"profiles_dir": DBT_PROFILES_DIR, "project_dir": DBT_PROJECT_DIR, "target": "prod"} | ||
) | ||
def make_activity_stats_job(asset_group: AssetGroup) -> JobDefinition: | ||
return asset_group.build_job( | ||
name="activity_stats", | ||
selection=["comment_daily_stats", "story_daily_stats", "activity_daily_stats"], | ||
) | ||
|
||
|
||
def asset_metadata(_context, model_info): | ||
config = dict(SHARED_SNOWFLAKE_CONF) | ||
config["schema"] = model_info["schema"] | ||
with connect_snowflake(config=config) as con: | ||
df = pd.read_sql(f"SELECT * FROM {model_info['name']} LIMIT 5", con=con) | ||
num_rows = con.execute(f"SELECT COUNT(*) FROM {model_info['name']}").fetchone() | ||
|
||
return {"Data sample": MetadataValue.md(df.to_markdown()), "Rows": num_rows[0]} | ||
|
||
|
||
# this list has one element per dbt model | ||
assets = load_assets_from_dbt_manifest( | ||
json.load(open(os.path.join(DBT_PROJECT_DIR, "target", "manifest.json"))), | ||
runtime_metadata_fn=asset_metadata, | ||
io_manager_key="warehouse_io_manager", | ||
) | ||
activity_stats_staging_job = build_assets_job( | ||
"activity_stats", | ||
assets, | ||
[], | ||
resource_defs={**RESOURCES_STAGING, **{"dbt": dbt_prod_resource}}, | ||
) | ||
|
||
activity_stats_prod_job = build_assets_job( | ||
"activity_stats", | ||
assets, | ||
[], | ||
resource_defs={**RESOURCES_PROD, **{"dbt": dbt_prod_resource}}, | ||
) | ||
activity_stats_prod_job = make_activity_stats_job(prod_assets) | ||
activity_stats_staging_job = make_activity_stats_job(staging_assets) |
65 changes: 20 additions & 45 deletions
65
examples/hacker_news_assets/hacker_news_assets/jobs/hacker_news_api_download.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,24 @@ | ||
from hacker_news_assets.assets.id_range_for_time import id_range_for_time | ||
from hacker_news_assets.assets.items import comments, items, stories | ||
from hacker_news_assets.resources import RESOURCES_LOCAL, RESOURCES_PROD, RESOURCES_STAGING | ||
from hacker_news_assets.resources.hn_resource import hn_api_subsample_client, hn_snapshot_client | ||
|
||
from dagster import build_assets_job, in_process_executor | ||
|
||
DOWNLOAD_TAGS = { | ||
"dagster-k8s/config": { | ||
"container_config": { | ||
"resources": { | ||
"requests": {"cpu": "500m", "memory": "2Gi"}, | ||
from hacker_news_assets.assets import local_assets, prod_assets, staging_assets | ||
|
||
from dagster import AssetGroup, JobDefinition | ||
|
||
|
||
def make_download_job(asset_group: AssetGroup) -> JobDefinition: | ||
return asset_group.build_job( | ||
name="hacker_news_api_download", | ||
selection=["*comments", "*stories"], | ||
tags={ | ||
"dagster-k8s/config": { | ||
"container_config": { | ||
"resources": { | ||
"requests": {"cpu": "500m", "memory": "2Gi"}, | ||
} | ||
}, | ||
} | ||
}, | ||
} | ||
} | ||
|
||
|
||
ASSETS = [id_range_for_time, items, comments, stories] | ||
|
||
|
||
download_prod_job = build_assets_job( | ||
"hacker_news_api_download", | ||
assets=ASSETS, | ||
resource_defs={ | ||
**{"hn_client": hn_api_subsample_client.configured({"sample_rate": 10})}, | ||
**RESOURCES_PROD, | ||
}, | ||
tags=DOWNLOAD_TAGS, | ||
) | ||
|
||
) | ||
|
||
download_staging_job = build_assets_job( | ||
"hacker_news_api_download", | ||
assets=ASSETS, | ||
resource_defs={ | ||
**{"hn_client": hn_api_subsample_client.configured({"sample_rate": 10})}, | ||
**RESOURCES_STAGING, | ||
}, | ||
tags=DOWNLOAD_TAGS, | ||
) | ||
|
||
download_local_job = build_assets_job( | ||
"hacker_news_api_download", | ||
assets=ASSETS, | ||
resource_defs={**{"hn_client": hn_snapshot_client}, **RESOURCES_LOCAL}, | ||
tags=DOWNLOAD_TAGS, | ||
executor_def=in_process_executor, | ||
) | ||
download_prod_job = make_download_job(prod_assets) | ||
download_staging_job = make_download_job(staging_assets) | ||
download_local_job = make_download_job(local_assets) |
36 changes: 9 additions & 27 deletions
36
examples/hacker_news_assets/hacker_news_assets/jobs/story_recommender.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,14 @@ | ||
from hacker_news_assets.assets.comment_stories import comment_stories | ||
from hacker_news_assets.assets.items import comments, stories | ||
from hacker_news_assets.assets.recommender_model import component_top_stories, recommender_model | ||
from hacker_news_assets.assets.user_story_matrix import user_story_matrix | ||
from hacker_news_assets.assets.user_top_recommended_stories import user_top_recommended_stories | ||
from hacker_news_assets.resources import RESOURCES_PROD, RESOURCES_STAGING | ||
from hacker_news_assets.assets import prod_assets, staging_assets | ||
|
||
from dagster import build_assets_job | ||
from dagster import AssetGroup, JobDefinition | ||
|
||
assets = [ | ||
comment_stories, | ||
user_story_matrix, | ||
recommender_model, | ||
component_top_stories, | ||
user_top_recommended_stories, | ||
] | ||
|
||
source_assets = [comments, stories] | ||
def make_story_recommender_job(asset_group: AssetGroup) -> JobDefinition: | ||
return asset_group.build_job( | ||
name="story_recommender", | ||
selection=["comment_stories*"], | ||
) | ||
|
||
story_recommender_prod_job = build_assets_job( | ||
"story_recommender", | ||
assets=assets, | ||
source_assets=source_assets, | ||
resource_defs=RESOURCES_PROD, | ||
) | ||
|
||
story_recommender_staging_job = build_assets_job( | ||
"story_recommender", | ||
assets=assets, | ||
source_assets=source_assets, | ||
resource_defs=RESOURCES_STAGING, | ||
) | ||
story_recommender_prod_job = make_story_recommender_job(prod_assets) | ||
story_recommender_staging_job = make_story_recommender_job(staging_assets) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters