Tutorial 2: Create a Snowpark Container Services Job Service¶
Introduction¶
After completing the Tutorial Common Setup, you are ready to create a job service. In this tutorial, you create a simple job service that connects to Snowflake, executes a SQL SELECT query, and saves the result to a table.
There are two parts to this tutorial:
Part 1: Create and test a job service. You download code provided for this tutorial and follow step-by-step instructions:
- Download the job service code for this tutorial.
- Build a Docker image for Snowpark Container Services, and upload the image to a repository in your account.
- Stage the service specification file, which gives Snowflake the container configuration information. In addition to the name of the image to use to start a container, the specification file specifies three arguments: a SELECT query, a virtual warehouse to execute the query, and the name of the table to save the result to.
- Execute the job service. Using the EXECUTE JOB SERVICE command, you can execute the job service by providing the specification file and the compute pool where Snowflake can run the container. And finally, verify the service results.
Part 2: Understand the job service code. This section provides an overview of the job service code and highlights how different components collaborate.
1: Download the job service code¶
Code (a Python application) is provided to implement a job service.
- Download SnowparkContainerServices-Tutorials.zip.
- Unzip the content, which includes one directory for each tutorial. The
Tutorial-2directory has the following files:main.pyDockerfilemy_job_spec.yaml
2: Build and upload an image¶
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 (see Common Setup).
You will need information about the repository (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. An example is shown: -
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 change to the directory containing the files you unzipped.
-
To build a Docker image, execute the following
docker buildcommand using the Docker CLI. Note the command specifies current working directory (.) as thePATHfor files to use for building the image.- For
image_name, usemy_job_image:latest.
Example
- For
-
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 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
-
3: Stage the specification file¶
- To upload your service specification file (
my_job_spec.yaml) to the stage, use one of the following options:-
The Snowsight web interface: For instructions, see Choosing an internal stage for local files.
-
The SnowSQL CLI: Execute the following PUT command:
For example:
-
Linux or macOS
-
Windows
You can also specify a relative path.
The command sets OVERWRITE=TRUE so that you can upload the file again, if needed (for example, if you fixed an error in your specification file). If the PUT command is executed successfully, information about the uploaded file is printed out.
-
-
4: Execute the job service¶
Now you are ready to create a job.
-
To start a job service, run the EXECUTE JOB SERVICE command:
Note the following:
-
FROM and SPEC provide the stage name and the name of the job service specification file. When the job service is executed, it runs the SQL statement and saves the result to a table as specified in
my_job_spec.yaml.The SQL statement is not executed within the Docker container. Instead, the running container connects to Snowflake and runs the SQL statement in a Snowflake warehouse.
-
COMPUTE_POOL provides the compute resources where Snowflake executes the job service.
-
You can optionally include the QUERY_WAREHOUSE parameter to specify a default warehouse for the container to execute SQL statements. However, the job service code in this tutorial specifies an environment variable to define the warehouse, so the preceding command omits the default.
-
EXECUTE JOB SERVICE returns output that includes the job name, as shown in the following sample output:
-
-
The job service runs a simple query and saves result to the results table. You can verify the job service successfully completed by querying the results table:
Sample output:
-
If you want to debug execution of your job service, execute SHOW SERVICE CONTAINERS IN SERVICE to determine if the job service is still running, if it failed to start, or why it failed if it did. Also, assuming your code outputs useful logs to standard output or standard error, you can access the logs using SYSTEM$GET_SERVICE_LOGS.
-
To get the job service status, execute SHOW SERVICE CONTAINERS IN SERVICE:
Sample output:
-
To get the job service log information, use the system function SYSTEM$GET_SERVICE_LOGS:
-
5: Clean up¶
If you do not plan to continue with Tutorial 4, you should remove billable resources you created. For more information, see Step 5 in Tutorial 4.
6: Reviewing the job service code¶
This section covers the following topics:
- Examining the files provided: Review various code files that implement the job service.
- Building and testing an image locally. The section provides an explanation of how you can locally test the Docker image before uploading it to a repository in your Snowflake account.
Examining the files provided¶
The zip file you downloaded at the beginning of the tutorial includes the following files:
main.pyDockerfilemy_job_spec.yaml
This section provides an overview of the code.
main.py file¶
In the code:
-
Python code executes at
main, which then executes therun_job()function: -
The
run_job()function reads the environment variables and uses them to set default values for various parameters. The container uses these parameters to connect to Snowflake. Note that:- You can override the parameter values, used in the service, using the
containers.envandcontainers.argsfields in the service specification. For more information, see Service specification reference. - When the image runs in Snowflake, Snowflake populates some of these parameters (see source code) automatically. However, when testing the image locally, you need to explicitly provide these parameters (as shown in the next section, Building and testing an image locally).
- You can override the parameter values, used in the service, using the
Dockerfile¶
This file contains all the commands to build an image using Docker.
my_ job_ spec.yaml File (Service Specification)¶
Snowflake uses information you provide in this specification to configure and run your job service.
In addition to the container.name and container.image required fields (see Service specification reference),
the specification includes the optional container.args field to list the arguments:
--queryprovides the query to execute when the service runs.--result_tableidentifies the table to save the query results.
Building and testing an image locally¶
You can test the Docker image locally before uploading it to a repository in your Snowflake account. In local testing, your container runs standalone (it is not a job service that Snowflake executes).
Use the following steps to test the Tutorial 2 Docker image:
-
To create a Docker image, in the Docker CLI, execute the
docker buildcommand: -
To launch your code, execute the
docker runcommand, providing<orgname>-<acctname>,<username>, and<password>:When testing the image locally, note that, in addition to the three arguments (a query, the warehouse to run the query, and a table to save the result to), you also provide the connection parameters for the container running locally to connect to Snowflake.
When you run the container as a service, Snowflake provides these parameters to the container as the environment variables. For more information, see Configure Snowflake Client.
The job service executes the query (
select current_time() as time,'hello') and writes result to the table (tutorial_db.data_schema.results). If the table does not exist, it is created. If the table exists, the job service adds a row.Sample result of querying the results table:
What’s next?¶
You can now test Tutorial 4, which shows how service-to-service communication works.