Create a Sample Application That Uses ScalarDB
This tutorial describes how to create a sample e-commerce application by using ScalarDB.
Overview
The sample e-commerce application shows how users can order and pay for items by using a line of credit.
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.
To reference the sample application source code, see src/main/java/sample/Sample.java.
Since the focus of the sample application is to demonstrate using ScalarDB, application-specific error handling, authentication processing, and similar functions are not included in the sample application. For details about exception handling in ScalarDB, see Handle exceptions.
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, 17 or 21)
- OpenJDK LTS version (8, 11, 17, or 21) from Eclipse Temurin, Amazon Corretto, or Microsoft
- 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.
Set up ScalarDB
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-sample
Start Cassandra
Cassandra is already configured for the sample application, as shown in database.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.json.
To apply the schema, go to the ScalarDB Releases page and download the ScalarDB Schema Loader that matches the version of ScalarDB that you want to use to the scalardb-samples/scalardb-sample folder.
Then, run the following command, replacing <VERSION> with the version of the ScalarDB Schema Loader that you downloaded:
$ java -jar scalardb-schema-loader-<VERSION>.jar --config database.properties --schema-file schema.json --coordinator