-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Example local dlt execution with dbt
- Loading branch information
1 parent
a0c4dc2
commit 1025768
Showing
8 changed files
with
155 additions
and
14 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
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,26 @@ | ||
{% materialization executedlt, supported_languages=['python']%} | ||
|
||
{%- set identifier = model['alias'] -%} | ||
{%- set language = model['language'] -%} | ||
|
||
{% set grant_config = config.get('grants') %} | ||
|
||
{%- set old_relation = adapter.get_relation(database=database, schema=schema, identifier=identifier) -%} | ||
{%- set target_relation = api.Relation.create(identifier=identifier, | ||
schema=schema, | ||
database=database, type='table') -%} | ||
{{ run_hooks(pre_hooks) }} | ||
|
||
{% call noop_statement(name='main', message='Executed DLT pipeline', code=compiled_code, rows_affected=-1, res=None) %} | ||
{%- set res = adapter.submit_local_dlt_job(model, compiled_code) -%} | ||
{% endcall %} | ||
{{ run_hooks(post_hooks) }} | ||
|
||
{% set should_revoke = should_revoke(old_relation, full_refresh_mode=True) %} | ||
{% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %} | ||
|
||
{% do persist_docs(target_relation, model) %} | ||
|
||
{{ return({'relations': [target_relation]}) }} | ||
|
||
{% endmaterialization %} |
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import dlt | ||
from dlt.pipeline import TPipeline | ||
|
||
|
||
@dlt.resource( | ||
columns={"event_tstamp": {"data_type": "timestamp", "precision": 3}}, | ||
primary_key="event_id", | ||
) | ||
def events(): | ||
yield [{"event_id": 1, "event_tstamp": "2024-07-30T10:00:00.123"}, | ||
{"event_id": 2, "event_tstamp": "2025-02-30T10:00:00.321"}] | ||
|
||
|
||
def model(dbt, pipeline: TPipeline): | ||
""" | ||
:param dbt: | ||
:param pipeline: Pre-configured dlt pipeline. dlt target connection and dataset is pre-set using the model config! | ||
:return: | ||
""" | ||
dbt.config(materialized="executedlt") | ||
print("========================================================") | ||
print(f"INFO: DLT Pipeline pipeline_name:{pipeline.pipeline_name}") | ||
print(f"INFO: DLT Pipeline dataset_name:{pipeline.dataset_name}") | ||
print(f"INFO: DLT Pipeline staging:{pipeline.staging}") | ||
print(f"INFO: DLT Pipeline destination:{pipeline.destination}") | ||
print(f"INFO: DLT Pipeline _pipeline_storage:{pipeline._pipeline_storage}") | ||
print(f"INFO: DLT Pipeline _schema_storage:{pipeline._schema_storage}") | ||
print(f"INFO: DLT Pipeline state:{pipeline.state}") | ||
print("========================================================") | ||
load_info = pipeline.run(events()) | ||
print(load_info) | ||
row_counts = pipeline.last_trace.last_normalize_info | ||
print(row_counts) | ||
print("========================================================") | ||
return None |
31 changes: 31 additions & 0 deletions
31
tests/resources/dbttest/models/my_executepython_dlt_model.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,31 @@ | ||
import dlt | ||
|
||
|
||
@dlt.resource( | ||
columns={"event_tstamp": {"data_type": "timestamp", "precision": 3}}, | ||
primary_key="event_id", | ||
) | ||
def events(): | ||
yield [{"event_id": 1, "event_tstamp": "2024-07-30T10:00:00.123"}, | ||
{"event_id": 2, "event_tstamp": "2025-02-30T10:00:00.321"}] | ||
|
||
|
||
def model(dbt, session): | ||
dbt.config(materialized="executepython") | ||
print("========================================================") | ||
print(f"INFO: DLT Version:{dlt.version.__version__}") | ||
print(f"INFO: DBT Duckdb Session:{type(session)}") | ||
print(f"INFO: DBT Duckdb Connection:{type(session._env.conn)}") | ||
print("========================================================") | ||
p = dlt.pipeline( | ||
pipeline_name="dbt_dlt", | ||
destination=dlt.destinations.duckdb(session._env.conn), | ||
dataset_name=dbt.this.schema, | ||
dev_mode=False, | ||
) | ||
load_info = p.run(events()) | ||
print(load_info) | ||
row_counts = p.last_trace.last_normalize_info | ||
print(row_counts) | ||
print("========================================================") | ||
return None |
File renamed without changes.
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,14 @@ | ||
from pathlib import Path | ||
from unittest import TestCase | ||
|
||
from opendbt import OpenDbtProject | ||
|
||
|
||
class TestOpenDbtProject(TestCase): | ||
RESOURCES_DIR = Path(__file__).parent.joinpath("resources") | ||
DBTTEST_DIR = RESOURCES_DIR.joinpath("dbttest") | ||
|
||
def test_run_executedlt_materialization(self): | ||
dp = OpenDbtProject(project_dir=self.DBTTEST_DIR, profiles_dir=self.DBTTEST_DIR, | ||
args=['--vars', 'dbt_custom_adapter: opendbt.examples.DuckDBAdapterV2Custom']) | ||
dp.run(command="run", args=['--select', 'my_executedlt_model']) |
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