Created
January 10, 2025 15:28
-
-
Save boxysean/28602c05fc981c1b924976c746ad0a5e to your computer and use it in GitHub Desktop.
Sample dbt macro for loading data with COPY INTO statement
This file contains hidden or 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
{% macro external_source_load() %} | |
{% set files_to_load = [ | |
["jaffle_shop_customers.csv", "jaffle_shop_customers_load", [ | |
"ID NUMBER", | |
"FIRST_NAME VARCHAR", | |
"LAST_NAME VARCHAR", | |
]], | |
] | |
%} | |
{% for csv_file_name, table_name, columns in files_to_load %} | |
{% set result = columns|join(", ") %} | |
{% do run_query("CREATE TABLE IF NOT EXISTS analytics.dbt_smcintyre." + table_name + " (" + result + ");")%} | |
{% do run_query("COPY INTO analytics.dbt_smcintyre." + table_name + " FROM @s3_dbt_tutorial_public/" + csv_file_name + " file_format = (type=csv field_delimiter=',' skip_header=1);") %} | |
{% endfor %} | |
{% endmacro %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment