Getting Started with ScalarDB Cluster SQL via JDBC
This tutorial describes how to create a sample application by using ScalarDB Cluster SQL via JDBC.
Prerequisites for this sample application
- OpenJDK LTS version (8, 11, 17, or 21) from Eclipse Temurin
- ScalarDB Cluster running on a Kubernetes cluster
- We assume that you have a ScalarDB Cluster running on a Kubernetes cluster that you deployed by following the instructions in Set Up ScalarDB Cluster on Kubernetes by Using a Helm Chart.
This sample application has been tested with OpenJDK from Eclipse Temurin. ScalarDB itself, however, has been tested with JDK distributions from various vendors. For details about the requirements for ScalarDB, including compatible JDK distributions, please see Requirements.
In this tutorial, we assume that you have a ScalarDB Cluster running on a Kubernetes cluster that you deployed by following the instructions in Set Up ScalarDB Cluster on Kubernetes by Using a Helm Chart.
In addition, you need access to the ScalarDB Cluster GitHub repository, packages in the ScalarDB Cluster repository, and packages in the ScalarDB SQL repository. These repositories are available only to users with a commercial license and permission. To get a license and permission, please contact us.
You also need to set the gpr.user
property to your GitHub username and the gpr.key
property to your personal access token.
To do so, you must either add these properties in ~/.gradle/gradle.properties
or specify the properties by using the -P
option when running the ./gradlew
command as follows:
./gradlew run ... -Pgpr.user=<YOUR_GITHUB_USERNAME> -Pgpr.key=<YOUR_PERSONAL_ACCESS_TOKEN>
Or you can use environment variables, such as USERNAME
for your GitHub username and TOKEN
for your personal access token.
export USERNAME=<YOUR_GITHUB_USERNAME>
export TOKEN=<YOUR_PERSONAL_ACCESS_TOKEN>
For more details, see Developer Guide for ScalarDB Cluster with the Java API.
Sample application
This tutorial illustrates the process of creating a sample e-commerce application, where items can be ordered and paid for with a line of credit by using ScalarDB JDBC.
The following diagram shows the system architecture of the sample application:
+------------------------------------------------------------------------------------------------------------------------------+
| [Kubernetes Cluster] |
| |
| [Pod] [Pod] [Pod] |
+------------------------+ | |
| SQL CLI | | +-------+ +-----------------------+ |
| (indirect client mode) | --+ | +---> | Envoy | ---+ +---> | ScalarDB Cluster Node | ---+ |
+------------------------+ | | | +-------+ | | +-----------------------+ | |
| | | | | | |
| | +---------+ | +-------+ | +--------------------+ | +-----------------------+ | +------------+ |
+--+-> | Service | ---+---> | Envoy | ---+---> | Service | ---+---> | ScalarDB Cluster Node | ---+---> | PostgreSQL | |
| | | (Envoy) | | +-------+ | | (ScalarDB Cluster) | | +-----------------------+ | +------------+ |
+------------------------+ | | +---------+ | | +--------------------+ | | |
| Sample application | | | | +-------+ | | +-----------------------+ | |
| with ScalarDB JDBC | --+ | +---> | Envoy | ---+ +---> | ScalarDB Cluster Node | ---+ |
| (indirect client mode) | | +-------+ +-----------------------+ |
+------------------------+ | |
+------------------------------------------------------------------------------------------------------------------------------+
Step 1. Clone the ScalarDB Samples repository
git clone https://github.com/scalar-labs/scalardb-samples.git
cd scalardb-samples/scalardb-sql-jdbc-sample
Step 2. Modify scalardb-sql.properties
You need to modify scalardb-sql.properties
to connect to ScalarDB Cluster as well.
But before doing so, you need to get the EXTERNAL-IP
address of the service resource of Envoy (scalardb-cluster-envoy
) as follows:
kubectl get svc scalardb-cluster-envoy
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
scalardb-cluster-envoy LoadBalancer 10.105.121.51 localhost 60053:30641/TCP 16h
In this case, the EXTERNAL-IP
address is localhost
.
Next, open scalardb-sql.properties
:
vim scalardb-sql.properties
Then, modify scalardb-sql.properties
as follows:
scalar.db.sql.connection_mode=cluster
scalar.db.sql.cluster_mode.contact_points=indirect:localhost
To connect to ScalarDB Cluster, you need to specify cluster
for the scalar.db.sql.connection_mode
property.
In addition, you will use the indirect
client mode and connect to the service resource of Envoy in this tutorial.
For details about the client modes, see Developer Guide for ScalarDB Cluster with the Java API.
Step 3. Load a schema
To load a schema, you need to use the SQL CLI. You can download the SQL CLI from ScalarDB Releases. After downloading the JAR file, you can use SQL CLI for Cluster by running the following command:
java -jar scalardb-cluster-sql-cli-3.10.6-all.jar --config scalardb-sql.properties --file schema.sql
Step 4. Load the initial data
Before running the sample application, you need to load the initial data by running the following command:
./gradlew run --args="LoadInitialData"