Tutorial: Download a Data File
This tutorial provides step-by-step instructions for creating a Web Request job to download a CSV data file.
The example used in this tutorial is the Electric Vehicle Population Data file, provided by DATA.GOV.
Prerequisites
Before continuing with this tutorial, ensure you have completed the following prerequisites:
Create a Network Rule
Create an external integration
Open the External Integrations page.
Apply the following configuration to create a new external integration:
Name: wa_gov_data_access_integration Allowed Network Rules: wa_gov_data_access_rule
Click Generate Script to generate a SQL script that creats a new Snowflake External Access Integration and grants Snoweaver access.
Run the Script in a Snowflake worksheet. Ensure that the executing role has the CREATE INTEGRATION privilege, such as the AccountAdmin role.
Note
Currently, Snowflake does not support native applications requesting permissions to create integrations on consumer accounts. This step may be automated in the future should this restriction be lifted.
Return to Snoweaver and register the new external access integraiton via the sidebar
Update the project configuration
Create a web request job
Open the LEARN_DEV project from the top menu in Snoweaver with the LEARN_DEV_DEVELOPER role.
Open the Jobs page.
Specify the following general configuration:
Name: wa_gov_data_get_ev_population Job Type: Web Request Instance Type: Procedure Request Type: GET Response Format: File Number of Threads: 4 Auto Compress: checked External Integration: wa_gov_data_access_integration Enable URL Query Parameters: checked
Use the following Jinja template for specifying the job endpoint URL:
https://data.wa.gov/api/views/f6w7-q2d2/rows.csv
Use the following Jinja template for specifying the query parameters:
{"accesstype":"DOWNLOAD"}
Test the template to validate the output.
Use the following Jinja template for specifying the file path:
ev-data-{{now().isoformat()}}.csv
Test the template to validate the output.
Test and save the job
Click Preview Request to examine the web request generated by the job.
Click Make a Test Call to initiate a web request to the specified endpoint URL. The request requires 10 to 20 seconds to process and complete.
The output will indicate the relative path within the DATA stage of the project where the downloaded file has been saved.
Save the job.
Open a new browser session or tab and log in to the Snowflake console. Navigate to Snoweaver -> LEARN_DEV -> Stages -> DATA using the LEARN_DEV_DEVELOPER role.
Locate and verify the newly generated file within the wa_gov_data_get_ev_population_test_call folder.
Execute the job
Return to the Jobs page within the LEARN_DEV Project.
Click Build to build a job instance.
Open a Snowflake worksheet and execute the following SQL statements to call the job instance:
USE ROLE LEARN_DEV_DEVELOPER; USE SCHEMA SNOWEAVER.LEARN_DEV; CALL WA_GOV_DATA_GET_EV_POPULATION();
Navigate to Snoweaver -> LEARN_DEV -> Stages -> DATA using the LEARN_DEV_DEVELOPER role.
Locate and verify the newly generated file within the wa_gov_data_get_ev_population folder.
Note
Press F5 or ⌘ + R to reload your browser if the folder is not visible.
Return to the Snowflake worksheet and and execute the following SQL statements to analyze the schema of the staged file:
USE SCHEMA LEARN_SW.DEV; CREATE OR REPLACE FILE FORMAT MY_CSV_FORMAT_INFER TYPE = 'csv' PARSE_HEADER = TRUE FIELD_OPTIONALLY_ENCLOSED_BY ='"'; SELECT * FROM TABLE( INFER_SCHEMA( LOCATION=>'@SNOWEAVER.LEARN_DEV.DATA/wa_gov_data_get_ev_population/<Staged File Name>' , FILE_FORMAT=>'MY_CSV_FORMAT_INFER' ) );
Note
Replace <Staged File Name> with the name of the newly generated file.
To query the staged file, you can generate a script as follows:
CREATE OR REPLACE TEMPORARY FILE FORMAT MY_CSV_FORMAT TYPE = 'csv' SKIP_HEADER = 1 FIELD_OPTIONALLY_ENCLOSED_BY ='"'; SELECT $1::TEXT AS "VIN (1-10)", $2::TEXT AS "County", $3::TEXT AS "City", $4::TEXT AS "State", $5::NUMBER(5, 0) AS "Postal Code", $6::NUMBER(4, 0) AS "Model Year", $7::TEXT AS "Make", $8::TEXT AS "Model", $9::TEXT AS "Electric Vehicle Type", $10::TEXT AS "Clean Alternative Fuel Vehicle (CAFV) Eligibility", $11::NUMBER(3, 0) AS "Electric Range", $12::NUMBER(6, 0) AS "Base MSRP", $13::NUMBER(2, 0) AS "Legislative District", $14::NUMBER(9, 0) AS "DOL Vehicle ID", $15::TEXT AS "Vehicle Location", $16::TEXT AS "Electric Utility", $17::NUMBER(11, 0) AS "2020 Census Tract", FROM @SNOWEAVER.LEARN_DEV.DATA/wa_gov_data_get_ev_population/<Staged File Name> (FILE_FORMAT => MY_CSV_FORMAT);