|
| 1 | +## Setting Up the Environment for Snowflake SPL Service |
| 2 | + |
| 3 | +This guide will walk you through installing the required tools, building and uploading the project image, and creating the compute pool and SPL service in Snowflake. |
| 4 | + |
| 5 | +### 1. Install JDK 1.8 |
| 6 | + |
| 7 | +The project requires **JDK 1.8**. Please download it from [Oracle's website](https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html) and set the appropriate `JAVA_HOME` environment variable. |
| 8 | + |
| 9 | +To verify that JDK is installed correctly, run: |
| 10 | +```bash |
| 11 | +java -version |
| 12 | +``` |
| 13 | +Make sure the output shows the correct version (Java 1.8). |
| 14 | + |
| 15 | +### 2. Install Maven |
| 16 | + |
| 17 | +You also need to install **Maven**. On macOS, it can be easily installed via [Homebrew](https://brew.sh/): |
| 18 | +```bash |
| 19 | +brew install maven |
| 20 | +``` |
| 21 | +To verify Maven installation, run: |
| 22 | +```bash |
| 23 | +mvn -version |
| 24 | +``` |
| 25 | +Ensure that the version output matches your requirements. |
| 26 | + |
| 27 | +### 3. Build and Upload the Docker Image |
| 28 | + |
| 29 | +We provide a script to **build** the project and **upload** the resulting Docker image to a repository managed by Snowflake. You can customize the repository location if needed. |
| 30 | + |
| 31 | +Before running the script, set your Snowflake account as an environment variable: |
| 32 | +```bash |
| 33 | +export SNOWFLAKE_ACCOUNT=your-snowflake-account |
| 34 | +``` |
| 35 | +Then run the build script: |
| 36 | +```bash |
| 37 | +./build.sh |
| 38 | +``` |
| 39 | +This script will build the Docker image and upload it to the Snowflake-managed Docker repository. |
| 40 | + |
| 41 | +### 4. Create the Compute Pool and SPL Service in Snowflake |
| 42 | + |
| 43 | +To create the SPL service in Snowflake, run the following SQL command in your Snowflake workbook. This command will create a service that provides endpoints to execute SPL files saved in the specified path. |
| 44 | +```sql |
| 45 | +CREATE SERVICE spl_service |
| 46 | + IN COMPUTE POOL tutorial_compute_pool |
| 47 | + FROM SPECIFICATION $$ |
| 48 | + spec: |
| 49 | + container: |
| 50 | + - name: main |
| 51 | + image: /tutorial_db/data_schema/tutorial_repository/spl_service:latest |
| 52 | + env: |
| 53 | + SNOWFLAKE_WAREHOUSE: tutorial_warehouse |
| 54 | + volumeMounts: |
| 55 | + - name: data |
| 56 | + mountPath: /opt/data |
| 57 | + endpoints: |
| 58 | + - name: spl |
| 59 | + port: 8502 |
| 60 | + public: true |
| 61 | + volumes: |
| 62 | + - name: data |
| 63 | + source: "@tutorial_stage" |
| 64 | + $$ |
| 65 | + MIN_INSTANCES=1 |
| 66 | + MAX_INSTANCES=1; |
| 67 | +``` |
| 68 | + |
| 69 | +The service will provide endpoints to allow users to execute SPL files located in the main path, which is backed by the stage. As long as the SPL file is in the stage, it can be executed via REST APIs. |
| 70 | + |
| 71 | +To **check the status** of the service or **retrieve logs**, use the following SQL commands: |
| 72 | +```sql |
| 73 | +SELECT SYSTEM$GET_SERVICE_STATUS('spl_service'); |
| 74 | +SELECT SYSTEM$GET_SERVICE_LOGS('spl_service', '0', 'main'); |
| 75 | +``` |
| 76 | + |
| 77 | +### 5. Execute SPL Using REST API |
| 78 | + |
| 79 | +To execute SPL via REST API, first get the service endpoint: |
| 80 | +```sql |
| 81 | +SHOW ENDPOINTS IN SERVICE spl_service; |
| 82 | +``` |
| 83 | + |
| 84 | +From the result, copy the `ingress_url` and paste it into your web browser (e.g., Chrome). You can then invoke the SPL using a REST API request like this: |
| 85 | +```bash |
| 86 | +https://your-ingress-url.snowflakecomputing.app/file_test.splx() |
| 87 | +``` |
| 88 | +Replace `your-ingress-url` with the actual URL obtained from the `SHOW ENDPOINTS` command. |
| 89 | + |
| 90 | +This assumes that both the `file_test.splx` and any relevant data are in the `mainPath` backed by the stage. |
| 91 | + |
| 92 | +### 6. Upload Data and SPL Files to Snowflake Stage |
| 93 | + |
| 94 | +To upload data or SPL files into the Snowflake stage, use **SnowSQL** (see [SnowSQL documentation](https://docs.snowflake.com/en/user-guide/snowsql)). For example, to upload the `employee.btx` file: |
| 95 | +```sql |
| 96 | +PUT file:////path-to-esproc/mainPath/employee.btx @tutorial_stage |
| 97 | +AUTO_COMPRESS=FALSE |
| 98 | +OVERWRITE=TRUE; |
| 99 | +``` |
| 100 | +Change `path-to-esproc` to the actual path on your machine. This command will place the file into the `tutorial_stage`, making it accessible for SPL execution. |
| 101 | + |
| 102 | +### Summary |
| 103 | + |
| 104 | +In this guide, you've set up your environment, built the project Docker image, created a Snowflake SPL service, and learned how to execute SPL files. Make sure to verify each step, ensuring everything is correctly configured for smooth operation. |
0 commit comments