-
Notifications
You must be signed in to change notification settings - Fork 193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend custom destination #1107
Merged
Merged
Changes from 10 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
469b225
rename tests file
sh-rp fc15c94
add setting to skip dlt internal tables and columns in custom destina…
sh-rp 890c0e0
add nesting level setting to custom destination
sh-rp 4278853
use correct internal dlt schema item marker
sh-rp 1e276c5
add example for custom destination bigquery
sh-rp cee1e90
fix embedded snippet checker output
sh-rp 7e02f82
add custom destination example to docs
sh-rp f6e7e6f
update custom destination example
sh-rp ed5956b
pin flake8-encodings to fork
sh-rp 6467063
fix snippet marker
sh-rp 5f888ad
ignore google imports
sh-rp cd14cbe
Merge branch 'devel' into d#/custom_destination_enhancements
sh-rp 897bf8b
Docs: fix custom destination (#1113)
AstrakhantsevaAA 5a5645f
pin databind.json python package
sh-rp e663e30
pin databind core
sh-rp 3739c9a
add bigquery extra for snippets tests
sh-rp b3fe660
updates to the readme
sh-rp 8bb0e30
rename function for nesting level test
sh-rp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,8 @@ def destination( | |
batch_size: int = 10, | ||
name: str = None, | ||
naming_convention: str = "direct", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good! please add this to our docs: that the default settings are such that data comes to sink without changing identifiers, un-nested and with dlt identifiers removed. and that it is good to push stuff to queues and REST APIs |
||
skip_dlt_columns_and_tables: bool = True, | ||
max_table_nesting: int = 0, | ||
spec: Type[GenericDestinationClientConfiguration] = GenericDestinationClientConfiguration, | ||
) -> Callable[ | ||
[Callable[Concatenate[Union[TDataItems, str], TTableSchema, TDestinationCallableParams], Any]], | ||
|
@@ -49,6 +51,8 @@ def wrapper( | |
batch_size=batch_size, | ||
destination_name=name, | ||
naming_convention=naming_convention, | ||
skip_dlt_columns_and_tables=skip_dlt_columns_and_tables, | ||
max_table_nesting=max_table_nesting, | ||
**kwargs, # type: ignore | ||
) | ||
|
||
|
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 |
---|---|---|
@@ -1,14 +1,17 @@ | ||
from typing import Optional | ||
from dlt.common.destination import DestinationCapabilitiesContext | ||
from dlt.common.data_writers import TLoaderFileFormat | ||
|
||
|
||
def capabilities( | ||
preferred_loader_file_format: TLoaderFileFormat = "puae-jsonl", | ||
naming_convention: str = "direct", | ||
max_table_nesting: Optional[int] = 0, | ||
) -> DestinationCapabilitiesContext: | ||
caps = DestinationCapabilitiesContext.generic_capabilities(preferred_loader_file_format) | ||
caps.supported_loader_file_formats = ["puae-jsonl", "parquet"] | ||
caps.supports_ddl_transactions = False | ||
caps.supports_transactions = False | ||
caps.naming_convention = naming_convention | ||
caps.max_table_nesting = max_table_nesting | ||
return caps |
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
Empty file.
9 changes: 9 additions & 0 deletions
9
docs/examples/custom_destination_bigquery/.dlt/example.secrets.toml
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 @@ | ||
# you can just paste services.json as credentials | ||
[destination.bigquery.credentials] | ||
client_email = "" | ||
private_key = "" | ||
project_id = "" | ||
token_uri = "" | ||
refresh_token = "" | ||
client_id = "" | ||
client_secret = "" |
Empty file.
71 changes: 71 additions & 0 deletions
71
docs/examples/custom_destination_bigquery/custom_destination_bigquery.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,71 @@ | ||
import dlt | ||
import pandas as pd | ||
import pyarrow as pa | ||
from google.cloud import bigquery | ||
|
||
from dlt.common.configuration.specs import GcpServiceAccountCredentials | ||
|
||
# constants | ||
OWID_DISASTERS_URL = ( | ||
"https://raw.githubusercontent.com/owid/owid-datasets/master/datasets/" | ||
"Natural%20disasters%20from%201900%20to%202019%20-%20EMDAT%20(2020)/" | ||
"Natural%20disasters%20from%201900%20to%202019%20-%20EMDAT%20(2020).csv" | ||
) | ||
# this table needs to be manually created in your gc account | ||
# format: "your-project.your_dataset.your_table" | ||
BIGQUERY_TABLE_ID = "chat-analytics-rasa-ci.ci_streaming_insert.natural-disasters" | ||
|
||
# dlt sources | ||
@dlt.resource(name="natural_disasters") | ||
def resource(url: str): | ||
# load pyarrow table with pandas | ||
table = pa.Table.from_pandas(pd.read_csv(url)) | ||
# we add a list type column to demontrate bigquery lists | ||
table = table.append_column( | ||
"tags", | ||
pa.array( | ||
[["disasters", "earthquakes", "floods", "tsunamis"]] * len(table), | ||
pa.list_(pa.string()), | ||
), | ||
) | ||
# we add a struct type column to demonstrate bigquery structs | ||
table = table.append_column( | ||
"meta", | ||
pa.array( | ||
[{"loaded_by": "dlt"}] * len(table), | ||
pa.struct([("loaded_by", pa.string())]), | ||
), | ||
) | ||
yield table | ||
|
||
# dlt biquery custom destination | ||
# we can use the dlt provided credentials class | ||
# to retrieve the gcp credentials from the secrets | ||
@dlt.destination(name="bigquery", loader_file_format="parquet", batch_size=0) | ||
def bigquery_insert( | ||
items, table, credentials: GcpServiceAccountCredentials = dlt.secrets.value | ||
) -> None: | ||
client = bigquery.Client( | ||
credentials.project_id, credentials.to_native_credentials(), location="US" | ||
) | ||
job_config = bigquery.LoadJobConfig( | ||
autodetect=True, | ||
source_format=bigquery.SourceFormat.PARQUET, | ||
schema_update_options=bigquery.SchemaUpdateOption.ALLOW_FIELD_ADDITION, | ||
) | ||
# since we have set the batch_size to 0, we get a filepath and can load the file directly | ||
with open(items, "rb") as f: | ||
load_job = client.load_table_from_file(f, BIGQUERY_TABLE_ID, job_config=job_config) | ||
load_job.result() # Waits for the job to complete. | ||
|
||
if __name__ == "__main__": | ||
# run the pipeline and print load results | ||
pipeline = dlt.pipeline( | ||
pipeline_name="csv_to_bigquery_insert", | ||
destination=bigquery_insert, | ||
dataset_name="mydata", | ||
full_refresh=True, | ||
) | ||
load_info = pipeline.run(resource(url=OWID_DISASTERS_URL)) | ||
|
||
print(load_info) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the best we can do now. if we have more normalizers with incompatible configs then we'll need to look for something better