dotConnect for Snowflake Documentation
On this page
    Bulk Data Loading
    On this page

    The SnowflakeInternalLoader, SnowflakeAmazonS3Loader, and SnowflakeAzureLoader components are used for bulk uploading a large number of records to Snowflake. A loader writes the rows to compressed CSV files, uploads the files to a Snowflake stage, and then loads them into the table with the COPY INTO command. After the data is loaded, the temporary files are deleted from the stage.

    Choosing a Loader

    The loaders differ in the stage they use to deliver the files to Snowflake:

    Class

    Stage

    What you need

    SnowflakeInternalLoaderThe internal stage of the target table (the table stage)The OWNERSHIP privilege on the target table.
    SnowflakeAmazonS3LoaderAn Amazon S3 bucketAn AWS access key ID and secret key.
    SnowflakeAzureLoaderAn Azure Blob Storage containerAn Azure storage account name and key.

    All loaders are descendants of the SnowflakeLoaderBase class and work in the same way. The loaders don't create tables: they append the data to an existing table.

    The role used in the connection needs the following privileges:

    SnowflakeAmazonS3Loader and SnowflakeAzureLoader pass the storage credentials directly in the COPY INTO statement. The text of this statement, with the AWS keys or the Azure SAS token, is visible in dbMonitor, so use credentials with access limited to the bucket or container that the loader uses. If your Snowflake account requires storage integrations for loading data from cloud storage (the REQUIRE_STORAGE_INTEGRATION_FOR_STAGE_OPERATION account parameter is set to TRUE), these loaders can't be used. For more information on the privileges, see Choosing an internal stage for local files in the Snowflake documentation.

    Using a Loader

    To start uploading data, create a loader, assign the connection to its Connection property, and specify the table name in the TableName property. The table name can include the database and the schema, for example, YOUR_DATABASE.YOUR_SCHEMA.DEPT. If you don't specify them, the table is searched in the database and the schema of the connection.

    The table and column names are case-sensitive: specify them exactly as they are stored in Snowflake. The loader encloses a name in double quotes if it contains lowercase letters, spaces, or special characters, and Snowflake stores names created without quotes in upper case. For example, for a table created with the statement CREATE TABLE dept (deptno INT), specify the table name DEPT and the column name DEPTNO. With dept, the loader looks for a table named "dept" in lower case and fails when it sends the first batch: SnowflakeInternalLoader with an error saying that the stage "%dept" doesn't exist, and the other loaders with an error saying that the table doesn't exist.

    Then you can tweak the following properties:

    Note that both properties specify a size in bytes, not a number of rows.

    After this, you need to specify the columns to load data to. You can do it either manually, using the Columns property, or use the CreateColumns method to retrieve the columns from the table specified in the TableName property. The CreateColumns method doesn't add the IDENTITY (AUTOINCREMENT) columns of the table, so Snowflake generates their values. A column whose default value is a sequence (DEFAULT seq.NEXTVAL) is not an identity column, and CreateColumns adds it. If you don't set a value for such a column, the loader writes NULL to it, not the next value of the sequence. To let Snowflake fill it from the sequence, add the columns manually and leave this column out. If you don't specify any columns, they are created from the table when you call the Open method.

    To add a column manually, call the Columns.Add method with the column name, the type (a SnowflakeType or DbType value), the size, the precision, and the scale. The loader writes the values as text, so you can pass 0 for the size, the precision, and the scale, as in the example below.

    Then call the Open method and use the SetValue method to set the values of the row fields. To switch to the next row, call the NextRow method. When a data file reaches the UploadBatchSize size, it is uploaded to the stage, and when the uploaded data reaches the BatchSize size, the data is loaded to the table.

    After you have finished loading data, call the Close method. This method uploads the last file to the stage, loads the remaining data to the table, and deletes the temporary files from the stage. If you call Close(false), the data of the current batch that has not been loaded yet is not sent to the table, and the files are deleted from the stage as well. Files already uploaded to the stage stay there until Close is called, so call Close(false) if an error occurs while you load data, as in the example below. The Dispose method of an open loader calls Close() and loads the current batch to the table. If you create the loader in a using block and an exception occurs inside it, call Close(false) in a catch block before the loader is disposed, or the data of the current batch is loaded.

    The data is loaded in batches, each with its own COPY INTO command that runs with the ON_ERROR = ABORT_STATEMENT option. If a value of a batch can't be loaded, for example, it doesn't fit the column type, the command fails, an exception is thrown, and none of the rows of this batch is loaded.

    The following example demonstrates how to use the SnowflakeInternalLoader class. The dept table is created beforehand, for example, with the statement CREATE TABLE dept (deptno INT, dname VARCHAR(14), loc VARCHAR(13)), so the table and column names are specified in upper case.

    using (var connection = new SnowflakeConnection("Domain=myorg-myaccount.snowflakecomputing.com;User=your_user;Password=********;Database=your_database;Schema=your_schema;Warehouse=your_warehouse;License Key=**********")) {
        connection.Open();
    
        SnowflakeInternalLoader loader = new SnowflakeInternalLoader("DEPT", connection);
        loader.Columns.Add("DEPTNO", DbType.Int64, 0, 0, 0);
        loader.Columns.Add("DNAME", DbType.String, 0, 0, 0);
    
        try {
            loader.Open();
            for (int i = 1; i <= 100; i++) {
                loader.SetValue("DEPTNO", i);
                loader.SetValue("DNAME", "Dept " + i);
                loader.NextRow();
            }
            loader.Close();
        }
        catch {
            // Delete the uploaded files from the stage without loading the current batch
            loader.Close(false);
            throw;
        }
    }
    Using connection As New SnowflakeConnection("Domain=myorg-myaccount.snowflakecomputing.com;User=your_user;Password=********;Database=your_database;Schema=your_schema;Warehouse=your_warehouse;License Key=**********")
        connection.Open()
    
        Dim loader As New SnowflakeInternalLoader("DEPT", connection)
        loader.Columns.Add("DEPTNO", DbType.Int64, 0, 0, 0)
        loader.Columns.Add("DNAME", DbType.String, 0, 0, 0)
    
        Try
            loader.Open()
            For i As Integer = 1 To 100
                loader.SetValue("DEPTNO", i)
                loader.SetValue("DNAME", "Dept " & i)
                loader.NextRow()
            Next
            loader.Close()
        Catch
            ' Delete the uploaded files from the stage without loading the current batch
            loader.Close(False)
            Throw
        End Try
    End Using

    SnowflakeAmazonS3Loader

    SnowflakeAmazonS3Loader uploads the files to an Amazon S3 bucket, and Snowflake reads them from there. Specify the following properties:

    SnowflakeAmazonS3Loader loader = new SnowflakeAmazonS3Loader("DEPT", connection);
    loader.AWSAccessKeyID = "your_access_key_id";
    loader.AWSSecretKey = "your_secret_access_key";
    loader.S3BucketName = "my-bucket";
    loader.S3BucketRegion = SnowflakeAmazonS3BucketRegion.EU;
    Dim loader As New SnowflakeAmazonS3Loader("DEPT", connection)
    loader.AWSAccessKeyID = "your_access_key_id"
    loader.AWSSecretKey = "your_secret_access_key"
    loader.S3BucketName = "my-bucket"
    loader.S3BucketRegion = SnowflakeAmazonS3BucketRegion.EU

    SnowflakeAzureLoader

    SnowflakeAzureLoader uploads the files to an Azure Blob Storage container, and Snowflake reads them from there. The loader creates a container with a generated name when you call the Open method and deletes it when you call the Close method. Specify the following properties:

    SnowflakeAzureLoader loader = new SnowflakeAzureLoader("DEPT", connection);
    loader.StorageAccountName = "your_storage_account";
    loader.StorageAccountKey = "your_storage_account_key";
    Dim loader As New SnowflakeAzureLoader("DEPT", connection)
    loader.StorageAccountName = "your_storage_account"
    loader.StorageAccountKey = "your_storage_account_key"

    SnowflakeInternalLoader

    SnowflakeInternalLoader loads the data through the table stage of the target table and removes the files from the stage with the REMOVE command after the data is loaded. It doesn't need any additional settings or accounts, because the files are uploaded to the storage that Snowflake provides for the stage, whichever cloud platform (Amazon Web Services, Microsoft Azure, or Google Cloud) your Snowflake account is hosted on. The loader runs the PUT command only to get the location of the stage and temporary credentials, and then uploads the files to this storage itself. Because of this, your application must be able to connect to the cloud storage endpoint (Amazon S3, Azure Blob Storage, or Google Cloud Storage) in addition to Snowflake, which can matter if a firewall restricts outgoing connections.

    If your Snowflake account is hosted on Amazon Web Services, the loader supports a fixed list of AWS regions. For a stage in a region that isn't in this list, for example, an AWS GovCloud region, the loader throws NotSupportedException with the message "S3 Region: <region>". In this case, use SnowflakeAmazonS3Loader or SnowflakeAzureLoader.

    Data Types

    When you add a column to the Columns collection, specify its type with the SnowflakeType or DbType value. The loader converts the values to text in the CSV file: binary values are written in the hexadecimal format, and date and time values are written in the format that Snowflake accepts.