-
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.
hn local repo that uses duckdb as warehouse (#6968)
- Loading branch information
Showing
14 changed files
with
122 additions
and
28 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 |
---|---|---|
|
@@ -154,3 +154,6 @@ pythonenv*/ | |
|
||
# Vim project-local settings | ||
.vim | ||
|
||
# DuckDB | ||
*.duckdb |
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 +1 @@ | ||
from .repo import hacker_news_assets_prod, hacker_news_assets_staging | ||
from .repo import hacker_news_assets_local, hacker_news_assets_prod, hacker_news_assets_staging |
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
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
57 changes: 57 additions & 0 deletions
57
examples/hacker_news_assets/hacker_news_assets/resources/duckdb_parquet_io_manager.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,57 @@ | ||
import os | ||
|
||
import duckdb | ||
import pandas as pd | ||
|
||
from dagster import Field, check, io_manager | ||
from dagster.seven.temp_dir import get_system_temp_directory | ||
|
||
from .parquet_io_manager import PartitionedParquetIOManager | ||
|
||
|
||
class DuckDBPartitionedParquetIOManager(PartitionedParquetIOManager): | ||
"""Stores data in parquet files and creates duckdb views over those files.""" | ||
|
||
def handle_output(self, context, obj): | ||
if obj is not None: # if this is a dbt output, then the value will be None | ||
yield from super().handle_output(context, obj) | ||
con = self._connect_duckdb(context) | ||
|
||
path = self._get_path(context) | ||
if context.has_asset_partitions: | ||
to_scan = os.path.join(os.path.dirname(path), "*.pq", "*.parquet") | ||
else: | ||
to_scan = path | ||
con.execute("create schema if not exists hackernews;") | ||
con.execute( | ||
f"create or replace view {self._table_path(context)} as " | ||
f"select * from parquet_scan('{to_scan}');" | ||
) | ||
|
||
def load_input(self, context): | ||
check.invariant(not context.has_asset_partitions, "Can't load partitioned inputs") | ||
|
||
if context.dagster_type.typing_type == pd.DataFrame: | ||
con = self._connect_duckdb(context) | ||
return con.execute(f"SELECT * FROM {self._table_path(context)}").fetchdf() | ||
|
||
check.failed( | ||
f"Inputs of type {context.dagster_type} not supported. Please specify a valid type " | ||
"for this input either on the argument of the @asset-decorated function." | ||
) | ||
|
||
def _table_path(self, context): | ||
return f"hackernews.{context.asset_key.path[-1]}" | ||
|
||
def _connect_duckdb(self, context): | ||
return duckdb.connect(database=context.resource_config["duckdb_path"], read_only=False) | ||
|
||
|
||
@io_manager( | ||
config_schema={"base_path": Field(str, is_required=False), "duckdb_path": str}, | ||
required_resource_keys={"pyspark"}, | ||
) | ||
def duckdb_partitioned_parquet_io_manager(init_context): | ||
return DuckDBPartitionedParquetIOManager( | ||
base_path=init_context.resource_config.get("base_path", get_system_temp_directory()) | ||
) |
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
7 changes: 6 additions & 1 deletion
7
examples/hacker_news_assets/hacker_news_dbt/config/profiles.yml
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
2 changes: 1 addition & 1 deletion
2
examples/hacker_news_assets/hacker_news_dbt/models/comment_daily_stats.sql
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
2 changes: 1 addition & 1 deletion
2
examples/hacker_news_assets/hacker_news_dbt/models/story_daily_stats.sql
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
2 changes: 1 addition & 1 deletion
2
examples/hacker_news_assets/hacker_news_dbt/target/manifest.json
Large diffs are not rendered by default.
Oops, something went wrong.
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