How to Upload CSV to Snowflake
Snowflake allows you to upload a CSV file from your local machines that run on Windows, Linux, or MacOS.
This tutorial will show you how to upload a CSV file from all three platforms to a Snowflake database table.
In this example, the CSV file to be imported is called Enterprises
. It contains three columns (id
, name
, and location
), is located in "test" folder of our local machine, and has the following structure:
1,Microsoft,Washington
2,Apple,California
3,IBM,New York
...
Steps:
- Create a Snowflake stage
create or replace stage enterprises_stage;
- Create a file format using the
FILE FORMAT
command to describe the format of the file to be imported
create or replace file format enterprises_format type = 'csv' field_delimiter = ',';
- Upload your CSV file from local folder to a Snowflake stage using the
PUT
command
-- this step can not be performed by running the command from the Worksheets page on the Snowflake web interface. You'll need to install and use SnowSQL client to do this
-- Windows
put file://C:\test\Enterprises.csv @enterprises_stage;
-- Linux/Mac
put file:///tmp/data/Enterprises.csv @enterprises_stage;
- Check to see if the Snowflake stage is populated with the data from the file
select
c.$1,
c.$2,
c.$3
from @enterprises_stage (file_format => enterprises_format) c;
- You need to create a table within Snowflake database that has the same structure as the CSV file we want to import prior to running the
COPY INTO
command.
create or replace table enterprises (
id integer,
name varchar (100),
location varchar(100)
)
- Load data from a Snowflake stage into a Snowflake database table using a
COPY INTO
command
-- load data as it is organized in a CSV file
copy into test.enterprises from @enterprises_stage;
-- if you want to filter out data from a stage and import only particular columns
copy into test.enterprises from (select c.$1, c.$2 from @enterprises_stage (file_format => enterprises_format) c);
- Check to see if the Snowflake database table is populated with the data
select * from enterprises;
id | name | location
------------+-------------+---------------
1 | Microsoft | Washington
2 | Apple | California
3 | IBM | New York