Create a Sample Application That Uses ScalarDB SQL (JDBC)
This sample code is now deprecated. To use ScalarDB SQL, you need to use ScalarDB Cluster, which is available only in the Enterprise edition. For more information, see ScalarDB Cluster.
This tutorial describes how to create a sample e-commerce application that uses ScalarDB SQL (JDBC).
Overview
The sample e-commerce application shows how users can order and pay for items by using a line of credit. The use case described in this tutorial is the same as the basic ScalarDB sample but uses ScalarDB SQL (JDBC).
The database that you will be using in the sample application is Cassandra. Although Cassandra does not provide ACID transaction capabilities, you can make transactions ACID compliant by having your application connect to the database through ScalarDB SQL (JDBC).
Since the focus of the sample application is to demonstrate using ScalarDB SQL (JDBC), application-specific error handling, authentication processing, and similar functions are not included in the sample application. For details about exception handling in ScalarDB SQL (JDBC), see Handle SQLException.
What you can do in this sample application
The sample application supports the following types of transactions:
- Get customer information.
- Place an order by using a line of credit.
- Checks if the cost of the order is below the customer's credit limit.
- If the check passes, records the order history and updates the amount the customer has spent.
- Get order information by order ID.
- Get order information by customer ID.
- Make a payment.
- Reduces the amount the customer has spent.
Prerequisites
- One of the following Java Development Kits (JDKs):
- Oracle JDK LTS version (8, 11, or 17)
- OpenJDK LTS version (8, 11, or 17)
- Docker 20.10 or later with Docker Compose V2 or later
We recommend using the LTS versions mentioned above, but other non-LTS versions may work.
In addition, other JDKs should work with ScalarDB, but we haven't tested them.
In addition, you need access to the ScalarDB SQL repository on GitHub and the packages in the ScalarDB SQL repository, which are private. The packages and repository are available only those who are using ScalarDB Enterprise. If you need a license for ScalarDB Enterprise, please contact us.
After confirming that you have access to the ScalarDB SQL repository and its packages, you will need to set your GitHub username and your personal access token. To specify these properties, you can do one of the following:
- Add to Gradle properties via Terminal
- Add as environment variables
Specify the properties with the -P
option by running the ./gradlew
command as follows, replacing <YOUR_GITHUB_USERNAME>
with your GitHub username and <YOUR_PERSONAL_ACCESS_TOKEN>
with your personal access token:
$ ./gradlew run <OPTIONS> -Pgpr.user=<YOUR_GITHUB_USERNAME> -Pgpr.key=<YOUR_PERSONAL_ACCESS_TOKEN>
Specify the properties as environment variables by running the following commands, replacing <YOUR_GITHUB_USERNAME>
with your GitHub username and <YOUR_PERSONAL_ACCESS_TOKEN>
with your personal access token:
$ export USERNAME=<YOUR_GITHUB_USERNAME>
$ export TOKEN=<YOUR_PERSONAL_ACCESS_TOKEN>
For more details, see how to install ScalarDB SQL.
Set up ScalarDB SQL (JDBC)
The following sections describe how to set up the sample e-commerce application.
Clone the ScalarDB samples repository
Open Terminal, then clone the ScalarDB samples repository by running the following command:
$ git clone https://github.com/scalar-labs/scalardb-samples
Then, go to the directory that contains the sample application by running the following command:
$ cd scalardb-samples/scalardb-sql-jdbc-sample
Start Cassandra
Cassandra is already configured for the sample application, as shown in scalardb-sql.properties
.
To start Cassandra, which is included in the Docker container for the sample application, make sure Docker is running and then run the following command:
$ docker-compose up -d
Starting the Docker container may take more than one minute depending on your development environment.
Load the schema
The database schema (the method in which the data will be organized) for the sample application has already been defined in schema.sql
.
To apply the schema, go to the ScalarDB SQL Releases page and download the CLI tool (scalardb-sql-cli-<VERSION>-all.jar
) for the version of ScalarDB SQL that you want to use to the scalardb-samples/scalardb-sql-jdbc-sample
folder.
Then, run the following command, replacing <VERSION>
with the version of the ScalarDB Schema Loader that you downloaded:
$ java -jar scalardb-sql-cli-<VERSION>-all.jar --config scalardb-sql.properties --file schema.sql
Schema details
As shown in schema.sql
for the sample application, all the tables are created in the sample
namespace.
sample.customers
: a table that manages customers' informationcredit_limit
: the maximum amount of money a lender will allow each customer to spend when using a line of creditcredit_total
: the amount of money that each customer has already spent by using their line of credit
sample.orders
: a table that manages order informationsample.statements
: a table that manages order statement informationsample.items
: a table that manages information of items to be ordered
The Entity Relationship Diagram for the schema is as follows:
Load the initial data
After the Docker container has started, load the initial data by running the following command:
$ ./gradlew run --args="LoadInitialData"
After the initial data has loaded, the following records should be stored in the tables.
sample.customers
table
customer_id | name | credit_limit | credit_total |
---|---|---|---|
1 | Yamada Taro | 10000 | 0 |
2 | Yamada Hanako | 10000 | 0 |
3 | Suzuki Ichiro | 10000 | 0 |
sample.items
table
item_id | name | price |
---|---|---|
1 | Apple | 1000 |
2 | Orange | 2000 |
3 | Grape | 2500 |
4 | Mango | 5000 |
5 | Melon | 3000 |
Execute transactions and retrieve data in the sample application
The following sections describe how to execute transactions and retrieve data in the sample e-commerce application.
Get customer information
Start with getting information about the customer whose ID is 1
by running the following command:
$ ./gradlew run --args="GetCustomerInfo 1"
You should see the following output:
...
{"id": 1, "name": "Yamada Taro", "credit_limit": 10000, "credit_total": 0}
...