Springboot Integration With Snowflake
Snowflake recently rebranded its platform as Unistore, and the general availability of Unistore was announced at the annual developer conference in November 2024. Unistore introduces new capabilities that allow users to integrate both analytical and transactional data within a single platform. Previously, the common approach for handling data in Snowflake involved moving it from different sources using ETL (Extract, Transform, Load) processes or Snowpipes. However, with the introduction of Unistore, it is now possible to connect applications directly to Snowflake for transactional data operations as well. In this article, we will explore how to connect a Spring Boot application to Snowflake and use JPA (Java Persistence API) to interact with Snowflake database tables via standard JPA repositories.
Snowflake Dependencies
1. Snowflake JDBC Driver
Snowflake provides a JDBC driver to connect your Java application to the Snowflake database. You need to include the following dependency in your pom.xml
<dependency>
<groupId>net.snowflake</groupId>
<artifactId>snowflake-jdbc</artifactId>
<version>3.21.0</version>
</dependency>
2. Hibernate Dialect
The Hibernate dialect is responsible for translating Hibernate's generic HQL (Hibernate Query Language) and criteria queries into the native SQL dialect of the target database. Currently, Hibernate does not include a dialect for Snowflake. You can refer to the Hibernate documentation for available dialects: Hibernate Dialect Documentation.
However, a Snowflake dialect is available in the Snowflake GitHub repository at: Snowflake Hibernate Dialect.
Steps to Use the Snowflake Dialect:
mvn clean package -Dmaven.test.skip=true
./mvnw org.apache.maven.plugins:maven-install-plugin:3.1.1:install-file -Dfile=./target/snowflake-hibernate.jar -DpomFile=./pom.xml
<dependency>
<groupId>net.snowflake</groupId>
<artifactId>snowflake-hibernate</artifactId>
<version>0.0.2-SNAPSHOT</version>
</dependency>
2. Spring Boot Configuration
Your application.yml should be configured as follows
server:
port: 5000
jpa:
properties:
hibernate:
dialect: net.snowflake.hibernate.dialect.SnowflakeDialect
datasource:
password: <Your Snowflake Account Password>
driver-class-name: net.snowflake.client.jdbc.SnowflakeDriver
username: <Your Snowflake Account Username>
url: jdbc:snowflake://<Your Snowflake Account or Server URL>/?useSSL=false&db=DEMO&schema=USER_MANAGEMENT&warehouse=COMPUTE_WH
Recommended by LinkedIn
The account server URL you can obtain from the Snowflake dashboard refer to the image below
The warehouse name can be obtained from the Snowflake Query workbench, refer to the image below
3. Snowflake Tables
You can create tables directly in Snowflake using the Snowflake query dashboard. I prefer using the DBWeaver SQL client, which can connect to multiple databases, including Snowflake.
The connection properties in DBWeaver will look something like this,
Once connected to Snowflake Database, open SQL editor from DBWeaver and you can create tables with standard SQL syntax such as.
CREATE TABLE person (
id BIGINT PRIMARY KEY,
email VARCHAR(100) UNIQUE NOT NULL
);
CREATE SEQUENCE person_sequence START = 1 INCREMENT = 1;
While Snowflake supports auto-increment columns for primary keys similar to PostgreSQL, there may be challenges when working with JPA and the provided dialect. Using sequences is an effective alternative for systems with a high volume of inserts, as they provide better performance.
With the setup described above, you can interact with the Snowflake database using Spring Boot. However, there are some important takeaways to consider:
Code for above implementation can be found at https://github.com/dipurane/snowflake-springboot
Dipesh Interesting read! Integrating data effectively can really enhance performance.
Very helpful
Very informative !!!