Skip to content

Latest commit

 

History

History
412 lines (281 loc) · 15.6 KB

getting-started-with-scalardb-by-using-kotlin.mdx

File metadata and controls

412 lines (281 loc) · 15.6 KB
tags
Community
Enterprise Standard
Enterprise Premium

Getting Started with ScalarDB by Using Kotlin

import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';

This getting started tutorial explains how to configure your preferred database in ScalarDB and set up a basic electronic money application by using Kotlin. Since Kotlin has Java interoperability, you can use ScalarDB directly from Kotlin.

:::warning

The electronic money application is simplified for this tutorial and isn't suitable for a production environment.

:::

Prerequisites for this sample application

:::note

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.

:::

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-kotlin-sample

Set up your database for ScalarDB

Select your database, and follow the instructions to configure it for ScalarDB.

For a list of databases that ScalarDB supports, see Databases.

Run MySQL locally

You can run MySQL in Docker Compose by using the `docker-compose.yml` file in the `scalardb-samples/scalardb-kotlin-sample` directory.

To start MySQL, run the following command:

```console
docker compose up -d mysql
```

<h3>Configure ScalarDB</h3>

The **database.properties** file in the `scalardb-samples/scalardb-kotlin-sample` directory contains database configurations for ScalarDB. Please uncomment the properties for MySQL in the **database.properties** file so that the configuration looks as follows:

```properties
# For MySQL
scalar.db.storage=jdbc
scalar.db.contact_points=jdbc:mysql://localhost:3306/
scalar.db.username=root
scalar.db.password=mysql
```

Run PostgreSQL locally

You can run PostgreSQL in Docker Compose by using the `docker-compose.yml` file in the `scalardb-samples/scalardb-kotlin-sample` directory.

To start PostgreSQL, run the following command:

```console
docker compose up -d postgres
```

<h3>Configure ScalarDB</h3>

The **database.properties** file in the `scalardb-samples/scalardb-kotlin-sample` directory contains database configurations for ScalarDB. Please uncomment the properties for PostgreSQL in the **database.properties** file so that the configuration looks as follows:

```properties
# For PostgreSQL
scalar.db.storage=jdbc
scalar.db.contact_points=jdbc:postgresql://localhost:5432/
scalar.db.username=postgres
scalar.db.password=postgres
```

Run Oracle Database locally

You can run Oracle Database in Docker Compose by using the `docker-compose.yml` file in the `scalardb-samples/scalardb-kotlin-sample` directory.

To start Oracle Database, run the following command:

```console
docker compose up -d oracle
```

<h3>Configure ScalarDB</h3>

The **database.properties** file in the `scalardb-samples/scalardb-kotlin-sample` directory contains database configurations for ScalarDB. Please uncomment the properties for Oracle Database in the **database.properties** file so that the configuration looks as follows:

```properties
# For Oracle
scalar.db.storage=jdbc
scalar.db.contact_points=jdbc:oracle:thin:@//localhost:1521/FREEPDB1
scalar.db.username=SYSTEM
scalar.db.password=Oracle
```

Run SQL Server locally

You can run SQL Server in Docker Compose by using the `docker-compose.yml` file in the `scalardb-samples/scalardb-kotlin-sample` directory.

To start SQL Server, run the following command:

```console
docker compose up -d sqlserver
```

<h3>Configure ScalarDB</h3>

The **database.properties** file in the `scalardb-samples/scalardb-kotlin-sample` directory contains database configurations for ScalarDB. Please uncomment the properties for SQL Server in the **database.properties** file so that the configuration looks as follows:

```properties
# For SQL Server
scalar.db.storage=jdbc
scalar.db.contact_points=jdbc:sqlserver://localhost:1433;encrypt=true;trustServerCertificate=true
scalar.db.username=sa
scalar.db.password=SqlServer22
```

Run Amazon DynamoDB Local

You can run Amazon DynamoDB Local in Docker Compose by using the `docker-compose.yml` file in the `scalardb-samples/scalardb-kotlin-sample` directory.

To start Amazon DynamoDB Local, run the following command:

```console
docker compose up -d dynamodb
```

<h3>Configure ScalarDB</h3>

The **database.properties** file in the `scalardb-samples/scalardb-kotlin-sample` directory contains database configurations for ScalarDB. Please uncomment the properties for Amazon DynamoDB Local in the **database.properties** file so that the configuration looks as follows:

```properties
# For DynamoDB Local
scalar.db.storage=dynamo
scalar.db.contact_points=sample
scalar.db.username=sample
scalar.db.password=sample
scalar.db.dynamo.endpoint_override=http://localhost:8000
```
To use Azure Cosmos DB for NoSQL, you must have an Azure account. If you don't have an Azure account, visit [Create an Azure Cosmos DB account](https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/quickstart-portal#create-account).
<h3>Configure Cosmos DB for NoSQL</h3>

Set the **default consistency level** to **Strong** according to the official document at [Configure the default consistency level](https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/how-to-manage-consistency#configure-the-default-consistency-level).

<h3>Configure ScalarDB</h3>

The following instructions assume that you have properly installed and configured the JDK in your local environment and properly configured your Cosmos DB for NoSQL account in Azure.

The **database.properties** file in the `scalardb-samples/scalardb-kotlin-sample` directory contains database configurations for ScalarDB. Be sure to change the values for `scalar.db.contact_points` and `scalar.db.password` as described.

```properties
# For Cosmos DB
scalar.db.storage=cosmos
scalar.db.contact_points=<COSMOS_DB_FOR_NOSQL_URI>
scalar.db.password=<COSMOS_DB_FOR_NOSQL_KEY>
```

:::note

You can use the primary key or the secondary key in your Azure Cosmos DB account as the value for scalar.db.password.

:::

Run Cassandra locally

You can run Apache Cassandra in Docker Compose by using the `docker-compose.yml` file in the `scalardb-samples/scalardb-kotlin-sample` directory.

To start Apache Cassandra, run the following command:
```console
docker compose up -d cassandra
```

<h3>Configure ScalarDB</h3>

The **database.properties** file in the `scalardb-samples/scalardb-kotlin-sample` directory contains database configurations for ScalarDB. Please uncomment the properties for Cassandra in the **database.properties** file so that the configuration looks as follows:

```properties
# For Cassandra
scalar.db.storage=cassandra
scalar.db.contact_points=localhost
scalar.db.username=cassandra
scalar.db.password=cassandra
```

Load the database schema

You need to define the database schema (the method in which the data will be organized) in the application. For details about the supported data types, see Data type mapping between ScalarDB and other databases.

For this tutorial, a file named schema.json already exists in the scalardb-samples/scalardb-kotlin-sample directory. To apply the schema, go to the scalardb Releases page and download the ScalarDB Schema Loader that matches the version of ScalarDB that you are using to the scalardb-samples/scalardb-kotlin-sample directory.

Then, based on your database, run the following command, replacing <VERSION> with the version of the ScalarDB Schema Loader that you downloaded:

```console java -jar scalardb-schema-loader-.jar --config database.properties --schema-file schema.json --coordinator ```

:::note

The --coordinator option is specified because a table with transaction set to true exists in the schema. For details about configuring and loading a schema, see ScalarDB Schema Loader.

::: console java -jar scalardb-schema-loader-<VERSION>.jar --config database.properties --schema-file schema.json --coordinator

:::note

The --coordinator option is specified because a table with transaction set to true exists in the schema. For details about configuring and loading a schema, see ScalarDB Schema Loader.

::: console java -jar scalardb-schema-loader-<VERSION>.jar --config database.properties --schema-file schema.json --coordinator

:::note

The --coordinator option is specified because a table with transaction set to true exists in the schema. For details about configuring and loading a schema, see ScalarDB Schema Loader.

::: console java -jar scalardb-schema-loader-<VERSION>.jar --config database.properties --schema-file schema.json --coordinator

:::note

The --coordinator option is specified because a table with transaction set to true exists in the schema. For details about configuring and loading a schema, see ScalarDB Schema Loader.

::: console java -jar scalardb-schema-loader-<VERSION>.jar --config database.properties --schema-file schema.json --coordinator --no-backup --no-scaling

:::note

The --coordinator option is specified because a table with transaction set to true exists in the schema. For details about configuring and loading a schema, see ScalarDB Schema Loader.

Also, --no-backup and --no-scaling options are specified because Amazon DynamoDB Local does not support continuous backup and auto-scaling.

::: console java -jar scalardb-schema-loader-<VERSION>.jar --config database.properties --schema-file schema.json --coordinator

:::note

The --coordinator option is specified because a table with transaction set to true exists in the schema. For details about configuring and loading a schema, see ScalarDB Schema Loader.

::: console java -jar scalardb-schema-loader-<VERSION>.jar --config database.properties --schema-file schema.json --coordinator --replication-factor=1

:::note

The --coordinator option is specified because a table with transaction set to true exists in the schema. For details about configuring and loading a schema, see ScalarDB Schema Loader.

In addition, the --replication-factor=1 option has an effect only when using Cassandra. The default replication factor is 3, but to facilitate the setup in this tutorial, 1 is used so that you only need to prepare a cluster with one node instead of three nodes. However, keep in mind that a replication factor of 1 is not suited for production.

:::

Execute transactions and retrieve data in the basic electronic money application

After loading the schema, you can execute transactions and retrieve data in the basic electronic money application that is included in the repository that you cloned.

The application supports the following types of transactions:

  • Create an account.
  • Add funds to an account.
  • Send funds between two accounts.
  • Get an account balance.

:::note

When you first execute a Gradle command, Gradle will automatically install the necessary libraries.

:::

Create an account with a balance

You need an account with a balance so that you can send funds between accounts.

To create an account for customer1 that has a balance of 500, run the following command:

./gradlew run --args="-action charge -amount 500 -to customer1"

Create an account without a balance

After setting up an account that has a balance, you need another account for sending funds to.

To create an account for merchant1 that has a balance of 0, run the following command:

./gradlew run --args="-action charge -amount 0 -to merchant1"

Add funds to an account

You can add funds to an account in the same way that you created and added funds to an account in Create an account with a balance.

To add 500 to the account for customer1, run the following command:

./gradlew run --args="-action charge -amount 500 -to customer1"

The account for customer1 will now have a balance of 1000.

Send electronic money between two accounts

Now that you have created two accounts, with at least one of those accounts having a balance, you can send funds from one account to the other account.

To have customer1 pay 100 to merchant1, run the following command:

./gradlew run --args="-action pay -amount 100 -from customer1 -to merchant1"

Get an account balance

After sending funds from one account to the other, you can check the balance of each account.

To get the balance of customer1, run the following command:

./gradlew run --args="-action getBalance -id customer1"

You should see the following output:

...
The balance for customer1 is 900
...

To get the balance of merchant1, run the following command:

./gradlew run --args="-action getBalance -id merchant1"

You should see the following output:

...
The balance for merchant1 is 100
...

Stop the database

To stop the database, stop the Docker container by running the following command:

docker compose down

Reference

To see the source code for the electronic money application used in this tutorial, see ElectronicMoney.kt.