Loading Data Into Snowflake

Overview

Now we know how to create database objects, it’s time to load Snowflake data.

Explanation

In this part of the tutorial, we’ll look into the COPY INTO statement, which is able to load data quickly and efficiently into a table. The COPY INTO statement can read various file formats, such as CSV, XML, JSON, AVRO, ORC and Parquet.

Create a Stage

The COPY INTO statement will read data from files stored inside a cloud location. This location can be an internal stage of table or an external stage, such as Azure Blob Storage. Let’s create a stage pointing to a blob container. In the left menu, choose data > databases.

databases in menu

In the list of databases, choose the database and schema for which you want to create a stage.

select database and schema from tree view

Next, click on the Create button in the top right corner and choose Stage > Microsoft Azure.

create new external stage

To create the stage, we need the URL of the container and a SAS token. Both can be retrieved using Azure Storage Explorer. When selecting the blob container, you can see the URL in the properties:

get url of blob container

Right-click on the container and select Get Shared Access Signature.

get sas token

Select a date for into the future and choose the appropriate permissions:

create sas token

Both a URL containing the SAS token and the token itself are generated.

generated SAS token

Copy the token and the URL and paste them into the Snowflake dialog:

create stage dialog with url and token

If you want to see the DDL needed to create the stage using SQL, click on the SQL Preview at the bottom.

show sql to create stage

Once the stage is created, you can view all the files in the browser:

stage is created and files are visible

You can also list the files in the stage using the LIST command (make sure the right database and schema are selected in the context of the worksheet):

list all files in blob container

Load Data with COPY INTO

In this example, we’ll load a simple csv file from the stage we created in the previous paragraph. We can verify the data by using a SELECT statement:

CREATE OR REPLACE FILE FORMAT myformat
    TYPE = 'CSV'
    FIELD_DELIMITER = '|'
    ENCODING = 'WINDOWS1252'
    SKIP_HEADER = 1;
 
SELECT t.$1 AS Movies
FROM @AZUREBLOBCONTAINER/TestCopyInto(FILE_FORMAT => myformat) t;
read directly from file with SELECT

The file format is needed to specify the configuration of the csv file, so Snowflake knows how to read it. Notice we didn’t specify a file name, but only a directory (TestCopyInto). This means Snowflake will try to read any file it finds in the directory and its subdirectories. Let’s load the csv file into a table by using a wizard. In the Data section, go back to the stage and browse to the folder where the file is located. Click the ellipses next to the file and choose Load into table.

load data into table using wizard

In the dialog, leave the default to create a new table and specify a name for the table:

create new table

On the next page, Snowflake will parse the data and it allows you to deselect columns or change their data type.

file is parsed and ready to load

In the bottom left corner, click on Show SQL.

generated sql for copy into

The wizard generates 3 statements:

  • A CREATE TABLE statement
  • A CREATE FILE FORMAT statement
  • And finally the COPY INTO statement to copy the data from the CSV into the newly created table

To go back to the wizard, click on Hide SQL. Hit Load to transfer the data into our new table.

data loaded into table

When you want to load multiple files, you can use pattern matching with regex:

COPY INTO dbo.Movies
FROM @AZUREBLOBCONTAINER/TestCopyInto
    file_format = (format_name = myformat)
    pattern='.*[.]csv';

Keep in mind the destination table must already exists before running COPY INTO. If everything went successfully, you’ll see a line for each file load, together with the number of rows parsed and how many errors were encountered.

copy into successful

The COPY INTO is a powerful and flexible statement. It can also read compressed files (such as gzip compression). To get optimal performance, you can split very large files into files of about 100MB compressed. The COPY into statement will load these parallel into the destination table.

Alternatives

Are there other options to get data into Snowflake? Let’s take a look at some alternatives:

  • Any existing ETL tool should be able to write to Snowflake. There are ODBC and JDBC connectors available. Keep in mind Snowflake is a data warehouse solution, not an OLTP database. If the ETL tool writes data row by row, it’s going to be extremely slow. Batches are preferred, or maybe staging the data in gzipped flat files and then loading them into Snowflake using the COPY INTO statement.
  • There are also .NET and Python libraries available for working with Snowflake. There’s even a Kafka connector.
  • You can use the Snowflake command line tool for uploading data to a stage.
  • The Snowpipe feature allows you to load streaming data (in the form of micro-batches) into Snowflake.
  • And of course, you can use Azure Data Factory. You can load data into Azure Blob Storage using ADF, then run a COPY INTO statement to pull the data into the table, or you can load the data directly using a Copy activity. You can also use ADF as an orchestrator for executing SQL scripts on your Snowflake database with the Script activity.

Additional Information

  • To learn more about SAS tokens in Azure blob storage, check out the documentation.
  • The COPY INTO statement has many optional parameters and can handle multiple file formats. Check out the docs to see what’s possible.
  • You can use COPY INTO also in the opposite direction: to dump data from a table into a stage.
  • You can combine the COPY INTO statement with a SELECT statement (using the $ sign to access individual columns as shown earlier). This can be useful when you want a different column order, leave out columns or if you want to apply some transformations such as checking for white space or null values.

3 Comments

  1. Hi Richard,
    this tutorial is already a couple of years old, so the screenshots are a bit outdated. Snowflake has upgraded to a new UI called “Snowsight”. However, you should be able to switch to the “classic console” and then the screenshots should match.

    Regards,
    Koen

  2. My Snowflake doesn’t have a screen like this page, and I can’t see any way to create a Stage, am I doing something wrong?

Leave a Reply

Your email address will not be published. Required fields are marked *