Option 1: Load data with the Snowpipe REST API¶
This topic describes how to call the public REST endpoints to load data and retrieve load history reports. The instructions assume you have completed the setup instructions in Data loading preparation using the Snowpipe REST API.
Load data¶
Loading takes place in two steps:
- Step 1:
Stage your data files:
Internal stage: Use the PUT command to stage your files.
External stage: Use the client tools provided by the cloud provider to copy your files to the stage location (Amazon S3, Google Cloud Storage, or Microsoft Azure).
- Step 2:
Submit a request to the insertFiles REST endpoint to load the staged data files.
For your convenience, sample Java and Python programs that illustrate how to submit a REST endpoint are provided in this topic.
Sample program for the Java SDK¶
This example uses the Bouncy Castle Crypto APIs. In order to compile and run this example, you must include the following JAR files in your classpath:
the provider JAR file (
bcprov-jdkversions.jar)the PKIX / CMS / EAC / PKCS / OCSP / TSP / OPENSSL JAR file (
bcpkix-jdkversions.jar)
where versions specifies the versions of the JDK that the JAR file supports.
Before you compile the sample code, replace the following placeholder values:
PRIVATE_KEY_FILE = "/<path>/rsa_key.p8"Specify the local path to the private key file you created in Use key pair authentication & key rotation (in Data loading preparation using the Snowpipe REST API).
return "<private_key_passphrase>"ingetPrivateKeyPassphrase()If you generated an encrypted key, implement the
getPrivateKeyPassphrase()method to return the passphrase for decrypting that key.host = "<account_identifier>.snowflakecomputing.com"Specify your host information in the form of a URL.
The preferred format of the account identifier is as follows:
organization_name-account_nameNames of your Snowflake organization and account. For details, see Format 1 (preferred): Account name in your organization.
Alternatively, specify your account locator, along with the region and cloud platform where the account is hosted, if required. For details, see Format 2: Account locator in a region.
user = "<user_login_name>"Specify your Snowflake login name.
pipe = "<db_name>.<schema_name>.<pipe_name>"Specify the fully-qualified name of the pipe to use to load the data.
files.add("<path>/<filename>", <file_size_in_bytes>)Specify the path to your files to load in the file objects list.
Optionally specify the size of each file, in bytes, to avoid delays when Snowpipe calculates the operations required to load the data.
The path you specify must be relative to the stage where the files are located. Include the complete name for each file, including the file extension. For example, a CSV file that is gzip-compressed might have the extension
.csv.gz.
Sample program for the Python SDK¶
Before you execute the sample code, replace the following placeholder values:
<private_key_path>Specify the local path to the private key file you created in Use key pair authentication & key rotation (in Data loading preparation using the Snowpipe REST API).
return "<private_key_passphrase>"inget_private_key_passphrase()If you generated an encrypted key, implement the
get_private_key_passphrase()function to return the passphrase for decrypting that key.account='<account_identifier>'Specify the unique identifier for your account (provided by Snowflake). See the
hostdescription.host='<account_identifier>.snowflakecomputing.com'Specify the unique hostname for your Snowflake account.
The preferred format of the account identifier is as follows:
organization_name-account_nameNames of your Snowflake organization and account. For details, see Format 1 (preferred): Account name in your organization.
Alternatively, specify your account locator, along with the region and cloud platform where the account is hosted, if required. For details, see Format 2: Account locator in a region.
user='<user_login_name>'Specify your Snowflake login name.
pipe='<db_name>.<schema_name>.<pipe_name>'Specify the fully-qualified name of the pipe to use to load the data.
file_list=['<path>/<filename>', '<path>/<filename>']|staged_file_list=[StagedFile('<path>/<filename>', <file_size_in_bytes>), StagedFile('<path>/<filename>', <file_size_in_bytes>)]Specify the path to your files to load in the file objects list.
The path you specify must be relative to the stage where the files are located. Include the complete name for each file, including the file extension. For example, a CSV file that is gzip-compressed might have the extension
.csv.gz.Optionally specify the size of each file, in bytes, to avoid delays when Snowpipe calculates the operations required to load the data.
View the load history¶
Snowflake provides REST endpoints and an Snowflake Information Schema table function for viewing your load history:
REST endpoints:
Information Schema table function:
Account Usage view:
Note that querying either the Information Schema table function or Account Usage view, unlike calling the REST endpoints, requires a running warehouse.
Delete staged files¶
Delete the staged files after you successfully load the data and no longer require the files. For instructions, see Deleting staged files after Snowpipe loads the data.