Tutorial: Run a Snowflake Container Services job as a Snowflake task¶
Introduction¶
When you run a Snowpark Container Services job service as a Snowflake task, the integration enables scenarios that leverage the robust containerization and scalability of Snowpark Container Services.
In this tutorial you create a task graph with the following two tasks:
-
A SQL task that creates a table (
sales_number) and returns the table name as the return value for use by the dependent job service task.Note
For simplicity, this SQL job only creates a table. In actual work, you use the table to perform more complex computations, such as model training.
-
A dependent job service task that queries the sales_number table and returns results as JSON.
Prerequisites¶
Complete the Tutorial Common Setup required for Snowpark Container Services tutorials provided in this guide.
Step 1: Create example job service image¶
Save the sample code provided for the job service, build an image, and upload it to an image repository you created as part of common setup. This is the job service that you run as a Snowflake task in this tutorial.
Save the code that is provided for the job service¶
Save the followng example job service code files to your local machine:
-
main.py -
requirements.txt -
DOCKERFILE
You should now have a directory with three files.
Build image and upload to image repository¶
Build an image for the linux/amd64 platform that Snowpark Container Services supports, and then upload the image to the image repository in your account. For more information, see Common Setup.
You need the repository URL and the registry hostname before you can build and upload the image. For more information, see Registry and Repositories.
Get information about the repository¶
To get the repository URL, execute the SHOW IMAGE REPOSITORIES SQL command:
-
The
repository_urlcolumn in the output provides the URL, as shown in the following example: -
The host name in the repository URL is registry host name. An example is shown:
Build image and upload it to the repository¶
- Open a terminal window, and then change your directory to the directory that contains the files that you saved.
- To build a Docker image, execute the following
docker buildcommand by using the Docker CLI.
The command ends with a period (.) which specifies current working directory as the PATH for files to use for building the image.
- For
image_name, usemy_task_job_image:latest.Example
- Upload the image to the repository in your Snowflake account. In order for Docker to upload an image on your behalf to your repository,
you must first authenticate Docker with the registry.
-
For Docker to upload an image on your behalf to your repository, first authenticate Docker with the registry.
- We recommend by using Snowflake CLI to authenticate your local Docker instance with the image registry for your Snowflake account. Make sure that you configured Snowflake CLI to connect to Snowflake. For more information, see Configuring Snowflake CLI and connecting to Snowflake.
- To authenticate, execute the following Snowflake CLI command:
-
To upload the image, execute the following command:
Example
-
Step 2: Create and execute a task graph¶
To create a task graph, run the following SQL code:
In the next step, you can view the task and job details in Snowsight.
Python code equivalent to the preceding SQL code¶
The following Snowflake Python code is the equivalent of the preceding SQL code to create the task graph:
Step 3: View task and job details in Snowsight¶
To view the job service details in task history, perform the following steps:
-
Sign in to Snowsight.
-
In the navigation menu, select Catalog » Database Explorer.
-
In the object explorer, locate the database and schema that contain the tasks that you want to view.
-
For the selected schema, select Tasks.
-
Select a specific task.
The task details appear, with additional Graph, and Run History tabs.
-
To view the ID of the job service that you executed as part of the task run, select the Run History tab.
To view the task details in job history, perform the following steps:
-
In the navigation menu, select Monitoring » Services & jobs.
-
Select the Jobs tab.
-
Select the job that you want to view.
The job details page appears. The Overview tab displays the task name if the task ran a job service.
-
To view the task details, select the task name.
-
To view the ID of the job service that you executed as part of the task run, select the Run History tab.