Skip to content

Commit

Permalink
Add support for Snowflake Container Service
Browse files Browse the repository at this point in the history
  • Loading branch information
Ishiihara committed Dec 1, 2024
1 parent e358195 commit 2887d32
Show file tree
Hide file tree
Showing 12 changed files with 229 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
.idea/
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM openjdk:8-jdk-alpine
WORKDIR /app
COPY bin /app/bin
COPY config /app/config
COPY importlibs /app/importlibs
COPY jdbc /app/jdbc
COPY lib /app/lib
COPY mainPath /app/mainPath
COPY target /app/target
# Set executable permission for esprocx.sh
RUN chmod +x /app/bin/ServerConsole2.sh
ENTRYPOINT ["/bin/sh", "/app/bin/ServerConsole2.sh", "-h"]
104 changes: 104 additions & 0 deletions SNOWFLAKE_README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
## Setting Up the Environment for Snowflake SPL Service

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.

### 1. Install JDK 1.8

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.

To verify that JDK is installed correctly, run:
```bash
java -version
```
Make sure the output shows the correct version (Java 1.8).

### 2. Install Maven

You also need to install **Maven**. On macOS, it can be easily installed via [Homebrew](https://brew.sh/):
```bash
brew install maven
```
To verify Maven installation, run:
```bash
mvn -version
```
Ensure that the version output matches your requirements.

### 3. Build and Upload the Docker Image

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.

Before running the script, set your Snowflake account as an environment variable:
```bash
export SNOWFLAKE_ACCOUNT=your-snowflake-account
```
Then run the build script:
```bash
./build.sh
```
This script will build the Docker image and upload it to the Snowflake-managed Docker repository.

### 4. Create the Compute Pool and SPL Service in Snowflake

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.
```sql
CREATE SERVICE spl_service
IN COMPUTE POOL tutorial_compute_pool
FROM SPECIFICATION $$
spec:
container:
- name: main
image: /tutorial_db/data_schema/tutorial_repository/spl_service:latest
env:
SNOWFLAKE_WAREHOUSE: tutorial_warehouse
volumeMounts:
- name: data
mountPath: /opt/data
endpoints:
- name: spl
port: 8502
public: true
volumes:
- name: data
source: "@tutorial_stage"
$$
MIN_INSTANCES=1
MAX_INSTANCES=1;
```

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.

To **check the status** of the service or **retrieve logs**, use the following SQL commands:
```sql
SELECT SYSTEM$GET_SERVICE_STATUS('spl_service');
SELECT SYSTEM$GET_SERVICE_LOGS('spl_service', '0', 'main');
```

### 5. Execute SPL Using REST API

To execute SPL via REST API, first get the service endpoint:
```sql
SHOW ENDPOINTS IN SERVICE spl_service;
```

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:
```bash
https://your-ingress-url.snowflakecomputing.app/file_test.splx()
```
Replace `your-ingress-url` with the actual URL obtained from the `SHOW ENDPOINTS` command.

This assumes that both the `file_test.splx` and any relevant data are in the `mainPath` backed by the stage.

### 6. Upload Data and SPL Files to Snowflake Stage

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:
```sql
PUT file:////path-to-esproc/mainPath/employee.btx @tutorial_stage
AUTO_COMPRESS=FALSE
OVERWRITE=TRUE;
```
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.

### Summary

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.
3 changes: 3 additions & 0 deletions bin/ServerConsole2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
source /app/bin/setEnv2.sh
"$EXEC_JAVA" -Xms128m -Xmx1024m -cp "$START_HOME"/app/target/classes:"$START_HOME"/app/lib/*:"$START_HOME"/jdbc/* -Duser.language="$language" -Dstart.home="$START_HOME"/app com.scudata.ide.spl.ServerConsole $1 $2
4 changes: 4 additions & 0 deletions bin/setEnv2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
START_HOME=/
JAVA_HOME=/usr/bin/
EXEC_JAVA=$JAVA_HOME/java
language=en
35 changes: 35 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash

# Input arguments with default fallbacks
SNOWFLAKE_ACCOUNT=${1:-$SNOWFLAKE_ACCOUNT}
REPOSITORY_NAME=${2:-$REPOSITORY_NAME}

# Validate inputs
if [ -z "$SNOWFLAKE_ACCOUNT" ]; then
echo "Error: Snowflake account not specified."
echo "Usage: $0 <snowflake_account> [repository_name] or set SNOWFLAKE_ACCOUNT environment variable."
exit 1
fi

if [ -z "$REPOSITORY_NAME" ]; then
REPOSITORY_NAME="tutorial_db/data_schema/tutorial_repository"
echo "Repository not specified. Using default: $REPOSITORY_NAME"
fi

# Construct dynamic variables
IMAGE_HOST="$SNOWFLAKE_ACCOUNT.registry.snowflakecomputing.com"
REPOSITORY="$IMAGE_HOST/$REPOSITORY_NAME"
IMAGE_NAME="$REPOSITORY/spl-service:latest"

# Maven clean and package
echo "Building Maven project..."
mvn clean package -Prelease || { echo "Maven build failed! Exiting."; exit 1; }

# Docker build and push
echo "Building Docker image..."
docker build --rm --platform linux/amd64 -t "$IMAGE_NAME" . || { echo "Docker build failed! Exiting."; exit 1; }

echo "Pushing Docker image..."
docker push "$IMAGE_NAME" || { echo "Docker push failed! Exiting."; exit 1; }

echo "Docker image pushed successfully: $IMAGE_NAME"
20 changes: 11 additions & 9 deletions config/raqsoftConfig.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>

<Config Version="3">
<Runtime>
<DBList>
<DBList>
</DBList>
<Esproc>
<charSet>GBK</charSet>
Expand All @@ -11,7 +12,7 @@
<dateFormat>yyyy-MM-dd</dateFormat>
<timeFormat>HH:mm:ss</timeFormat>
<dateTimeFormat>yyyy-MM-dd HH:mm:ss</dateTimeFormat>
<mainPath />
<mainPath>/opt/data</mainPath>>
<tempPath />
<bufSize>65536</bufSize>
<parallelNum>1</parallelNum>
Expand All @@ -20,14 +21,15 @@
<nullStrings>nan,null,n/a</nullStrings>
<fetchCount>9999</fetchCount>
<extLibsPath/>
<customFunctionFile>customFunctions.properties</customFunctionFile>
<customFunctionFile>customFunctions.properties</customFunctionFile>

</Esproc>
<Logger>
<Level>INFO</Level>
</Logger>
</Runtime>
<JDBC>
<load>Runtime,Server</load>
<gateway></gateway>
</Runtime>
<JDBC>
<load>Runtime,Server</load>
<gateway></gateway>
</JDBC>
</Config>
</Config>
Binary file added mainPath/demo.splx
Binary file not shown.
Binary file added mainPath/employee.btx
Binary file not shown.
Binary file added mainPath/file_test.splx
Binary file not shown.
42 changes: 42 additions & 0 deletions spl_service.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
CREATE SERVICE spl_service
IN COMPUTE POOL tutorial_compute_pool
FROM SPECIFICATION $$
spec:
container:
- name: main
image: /tutorial_db/data_schema/tutorial_repository/spl_service:latest
env:
SNOWFLAKE_WAREHOUSE: tutorial_warehouse
volumeMounts:
- name: data
mountPath: /opt/data
endpoints:
- name: spl
port: 8502
public: true
volumes:
- name: data
source: "@tutorial_stage"
$$
MIN_INSTANCES=1
MAX_INSTANCES=1;

ALTER SERVICE spl_service
FROM SPECIFICATION $$
spec:
container:
- name: main
image: /tutorial_db/data_schema/tutorial_repository/spl_service:latest
env:
SNOWFLAKE_WAREHOUSE: tutorial_warehouse
volumeMounts:
- name: data
mountPath: /opt/data
endpoints:
- name: spl
port: 8502
public: true
volumes:
- name: data
source: "@tutorial_stage"
$$
16 changes: 16 additions & 0 deletions spl_service_spec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
spec:
container:
- name: main
image: /tutorial_db/data_schema/tutorial_repository/spl_service:latest
env:
SNOWFLAKE_WAREHOUSE: tutorial_warehouse
volumeMounts:
- name: data
mountPath: /opt/data
endpoints:
- name: spl
port: 8503
public: true
volumes:
- name: data
source: "@tutorial_stage"

0 comments on commit 2887d32

Please sign in to comment.