| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
This repo is used to store packages helpful in LOADING data into Snowflake.
At the moment, both COPY and MERGE are implemented.
Add the package with the following command:
poetry add git+https://github.com/remoteoss/snowflake-utils.gitIn order to use this package, you can use environment variables to pass the configuration to be used by Snowflake. These environment variables will be automatically picked up by the SnowflakeSettings class.
| Variable | Description |
|---|---|
| SNOWFLAKE_ACCOUNT | The account identifier |
| SNOWFLAKE_USER | The user that should log in |
| SNOWFLAKE_PASSWORD | The password, if using password authentication method. Will be ignored if authenticator is set to externalbrowser |
| SNOWFLAKE_DB | The database to be set as context for the connection |
| SNOWFLAKE_ROLE | The role to be used as primary role in the connection. Can be left empty to use the default one for your user. Remember that since early 2025 all secondary roles are also enabled by default |
| SNOWFLAKE_WAREHOUSE | The warehouse to be used during the connection |
| SNOWFLAKE_AUTHENTICATOR | The authenticator to be used, can be snowflake, externalbrowser or username_password_mfa. Will be ignored if private key file is specified and exists. |
| SNOWFLAKE_PRIVATE_KEY_FILE | The file that contains the private key to be used for authentication. This is the path to the file, not the content. |
| SNOWFLAKE_PRIVATE_KEY_PASSWORD | The optional password for the above file. |
| SNOWFLAKE_SCHEMA | The schema to be set as context for the connection |
| SNOWFLAKE_APPLICATION | The application name to be used for the connection |
When manually initializing the SnowflakeSettings class you can override any of these attributes (and the schema_name attribute to set a schema in your context), depending on your needs.
The library also implements some governance QOL methods, for example to include tags on tables/columns to be used for masking policies. The environment variables GOVERNANCE_{DATABASE|SCHEMA} will be used to determine the default location of PII tags, so that they can be specified using only their name and not the fully qualified one. If they are specified using the fqn, that will be parsed correctly.
The Table object has the following attributes:
Available (public) methods:
You can pass a role, database and schema as an attribute of the Table class to override the corresponding env variables.
In order to use the copy method of the Table class, you need an S3 bucket with the desired files to load into Snowflake, a Storage Integration with access to that bucket and a role that has access to this Storage Integration. You can either use an existing stage or let the library create a temporary one for you.
Additionally, you can either use an existing file format or pass the options for a temporary file format to be created. See this for all the available options.
You don't need to know the schema of the file, Snowflake can infer the schema, but you have the option to input the schema as an attribute of the Table class. You can also use the merge method to include changes and optionally qualify the results to prevent duplicates.
Basic example:
#Given a table TEST in the schema PUBLIC
test_table = Table(name="TEST", schema_name="PUBLIC")
#JSON temporary file format with outer array
json_file_format = InlineFileFormat(definition="TYPE = JSON STRIP_OUTER_ARRAY = TRUE")
#Existing storage integration
storage_integration = "DATA_STAGING"
#S3 Path with the JSON file(s)
path = "s3://example-bucket/example/path"
#Run the copy cmd with full refresh
test_table.copy_into(
path=path,
file_format=json_file_format,
storage_integration=storage_integration,
full_refresh=True,
)
# Merge into an existing table
test_table.merge(
path=path,
file_format=json_file_format,
storage_integration=storage_integration,
primary_keys=['id'],
replication_keys=['updated_at'],
)Example with copy/merge custom to unpack nested fields directly:
column_definitions = {
"id": "$1:id",
"name": "$1:name",
"last_name": "$1:last_name",
}
test_table.copy_custom(
column_definitions=column_definitions,
path=path,
file_format=parquet_file_format,
storage_integration=storage_integration,
full_refresh=True,
sync_tags=True,
)
test_table.merge_custom(
column_definitions=column_definitions,
path=path,
file_format=parquet_file_format,
storage_integration=storage_integration,
primary_keys=["id"],
)Example using an existing stage:
# Create a table that uses an existing stage
stage_table = Table(
name="CUSTOMERS",
schema_name="DATA",
database="PROD",
)
stage_name = f"{stage_table.schema_name}.CUSTOMER_STAGE"
# Create the stage first
with connect() as conn, conn.cursor() as cursor:
cursor.execute(
f"""
CREATE OR REPLACE STAGE {stage_name}
URL='{path}'
STORAGE_INTEGRATION = {storage_integration}
"""
)
# Use the existing stage for copying data
stage_table.copy_into(
file_format=parquet_file_format,
path=f"@{stage_table.schema_name}.CUSTOMER_STAGE",
full_refresh=True,
)Example using different file formats:
# Using an inline JSON file format
json_file_format = InlineFileFormat(
definition="TYPE = JSON STRIP_OUTER_ARRAY = TRUE"
)
# Using an inline CSV file format
csv_file_format = InlineFileFormat(
definition="TYPE = CSV FIELD_DELIMITER = ',' FIELD_OPTIONALLY_ENCLOSED_BY = '\"' NULL_IF = ('NULL', 'null') EMPTY_FIELD_AS_NULL = TRUE"
)
# Using an inline Parquet file format
parquet_file_format = InlineFileFormat(
definition="TYPE = PARQUET"
)
# Using an existing file format
existing_file_format = FileFormat(
database="PROD",
schema_="DATA",
name="STANDARD_CSV_FORMAT"
)
# Using a file format from a string
file_format_from_string = FileFormat.from_string("PROD.DATA.STANDARD_CSV_FORMAT")When initializing the table object you can pass a table structure that contains a dictionary of name: column, where Column is an object that contains the column data type and eventual tags to be applied to the column. TableStructure can also be used to specify the tags to be applied at the table level.
TableStructure can also be used to sanitize column names, if parsed_columns is called with replace_chars=True, which will remove hyphens from column names and replace them with underscores.
TableStructure(
columns={
"id": Column(name="id", data_type="integer", tags={"pii": "personal"}),
"name": Column(name="name", data_type="text"),
"last_name": Column(name="last_name", data_type="text"),
},
tags={"pii": "foo"},
)There are two available types of file formats:
| Back | FazBrowse Home | New Git URL |