Data Loading in Snowflake
Data Loading in Snowflake refers to the process of importing data from various sources (like CSVs, JSON files, databases, or external cloud storage) into Snowflake's tables for querying and analytics.
Here’s a comprehensive guide to loading data into Snowflake:
πΉ 1. Data Loading Methods
✅ From Local Files
Use SnowSQL CLI (Snowflake command-line tool)
Load into a named stage or directly into a table
✅ From Cloud Storage
AWS S3
Azure Blob Storage
Google Cloud Storage
✅ Via Snowpipe (Streaming Ingestion)
For real-time or near-real-time data loading
Automatically loads data when new files arrive in cloud storage
✅ Using Web UI (Snowsight)
For ad hoc file uploads (CSV, TSV, JSON, etc.)
Simple GUI interface
πΉ 2. Steps for Basic Data Load
Step 1: Create a Table
sql
Copy
Edit
CREATE OR REPLACE TABLE employees (
id INT,
name STRING,
hire_date DATE
);
Step 2: Stage the Data (Optional but Recommended)
Upload your file to an internal or external stage.
Create a named internal stage:
sql
Copy
Edit
CREATE OR REPLACE STAGE my_stage;
Upload file using SnowSQL:
bash
Copy
Edit
snowsql -a <account> -u <user> -f upload.sql
-- Inside upload.sql
PUT file://path/to/employees.csv @my_stage;
Step 3: Copy into Table
sql
Copy
Edit
COPY INTO employees
FROM @my_stage/employees.csv
FILE_FORMAT = (TYPE = 'CSV' FIELD_OPTIONALLY_ENCLOSED_BY = '"' SKIP_HEADER = 1);
πΉ 3. Using File Formats
You can predefine a file format for reuse:
sql
Copy
Edit
CREATE OR REPLACE FILE FORMAT my_csv_format
TYPE = 'CSV'
FIELD_OPTIONALLY_ENCLOSED_BY = '"'
SKIP_HEADER = 1;
Then use it during the COPY:
sql
Copy
Edit
COPY INTO employees
FROM @my_stage/employees.csv
FILE_FORMAT = my_csv_format;
πΉ 4. Monitoring & Validation
Use VALIDATION_MODE to preview errors:
sql
Copy
Edit
COPY INTO employees
FROM @my_stage/employees.csv
FILE_FORMAT = my_csv_format
VALIDATION_MODE = RETURN_ERRORS;
Query load history:
sql
Copy
Edit
SELECT * FROM INFORMATION_SCHEMA.LOAD_HISTORY
WHERE TABLE_NAME = 'EMPLOYEES';
πΉ 5. Using Snowpipe for Continuous Loads
sql
Copy
Edit
CREATE OR REPLACE PIPE my_pipe AS
COPY INTO employees
FROM @my_stage
FILE_FORMAT = (FORMAT_NAME = 'my_csv_format');
You can configure it to auto-ingest via cloud notifications (like AWS SNS).
Learn Data Engineering Snowflake course
Read More
A Step-by-Step Guide to Creating Tables in Snowflake
Snowflake’s Virtual Warehouses Explained
Setting Up Your First Database in Snowflake
Visit Our Quality Thought Training Institute in Hyderabad
Comments
Post a Comment