メインコンテンツまでスキップ
バージョン: 3.5 (unsupported)

ScalarDB Schema Loader

ScalarDB has its own data model and schema that maps to the implementation-specific data model and schema. In addition, ScalarDB stores internal metadata, such as transaction IDs, record versions, and transaction statuses, to manage transaction logs and statuses when you use the Consensus Commit transaction manager.

Since managing the schema mapping and metadata for transactions can be difficult, you can use ScalarDB Schema Loader, which is a tool to create schemas that doesn't require you to need in-depth knowledge about schema mapping or metadata.

You have two options to specify general CLI options in Schema Loader:

  • Pass the ScalarDB properties file and database-specific or storage-specific options.
  • Pass database-specific or storage-specific options without the ScalarDB properties file. (Deprecated)
注記

This tool supports only basic options to create, delete, or repair a table. If you want to use the advanced features of a database, you must alter your tables with a database-specific tool after creating the tables with this tool.

Set up Schema Loader

Select your preferred method to set up Schema Loader, and follow the instructions.

You can download the release versions of Schema Loader from the ScalarDB Releases page.

Run Schema Loader

This section explains how to run Schema Loader.

Available commands

Select how you would like to configure Schema Loader for your database. The preferred method is to use the properties file since other, database-specific methods are deprecated.

The following commands are available when using the properties file:

Usage: java -jar scalardb-schema-loader-<VERSION>.jar [-D] [--coordinator]
[--no-backup] [--no-scaling] -c=<configPath>
[--compaction-strategy=<compactionStrategy>] [-f=<schemaFile>]
[--replication-factor=<replicaFactor>]
[--replication-strategy=<replicationStrategy>] [--ru=<ru>]
Create/Delete schemas in the storage defined in the config file
-c, --config=<configPath>
Path to the config file of ScalarDB
--compaction-strategy=<compactionStrategy>
The compaction strategy, must be LCS, STCS or TWCS
(supported in Cassandra)
--coordinator Create/delete coordinator table
-D, --delete-all Delete tables
-f, --schema-file=<schemaFile>
Path to the schema json file
--no-backup Disable continuous backup (supported in DynamoDB)
--no-scaling Disable auto-scaling (supported in DynamoDB, Cosmos DB)
--replication-factor=<replicaFactor>
The replication factor (supported in Cassandra)
--replication-strategy=<replicationStrategy>
The replication strategy, must be SimpleStrategy or
NetworkTopologyStrategy (supported in Cassandra)
--ru=<ru> Base resource unit (supported in DynamoDB, Cosmos DB)

For a sample properties file, see database.properties.

注記

The following database-specific methods have been deprecated. Please use the commands for configuring the properties file instead.

Usage: java -jar scalardb-schema-loader-<VERSION>.jar --cassandra [-D]
[-c=<compactionStrategy>] -f=<schemaFile> -h=<hostIp>
[-n=<replicationStrategy>] [-p=<password>] [-P=<port>]
[-R=<replicationFactor>] [-u=<user>]
Create/Delete Cassandra schemas
-c, --compaction-strategy=<compactionStrategy>
Cassandra compaction strategy, must be LCS, STCS or TWCS
-D, --delete-all Delete tables
-f, --schema-file=<schemaFile>
Path to the schema json file
-h, --host=<hostIp> Cassandra host IP
-n, --network-strategy=<replicationStrategy>
Cassandra network strategy, must be SimpleStrategy or
NetworkTopologyStrategy
-p, --password=<password>
Cassandra password
-P, --port=<port> Cassandra Port
-R, --replication-factor=<replicationFactor>
Cassandra replication factor
-u, --user=<user> Cassandra user

Create namespaces and tables

To create namespaces and tables by using a properties file, run the following command, replacing the contents in the angle brackets as described:

$ java -jar scalardb-schema-loader-<VERSION>.jar --config <PATH_TO_SCALARDB_PROPERTIES_FILE> -f <PATH_TO_SCHEMA_FILE> [--coordinator]

If --coordinator is specified, a Coordinator table will be created.

注記

The following database-specific CLI arguments have been deprecated. Please use the CLI arguments for configuring the properties file instead.

$ java -jar scalardb-schema-loader-<VERSION>.jar --cassandra -h <CASSANDRA_IP> [-P <CASSANDRA_PORT>] [-u <CASSANDRA_USER>] [-p <CASSANDRA_PASSWORD>] -f <PATH_TO_SCHEMA_FILE> [-n <NETWORK_STRATEGY>] [-R <REPLICATION_FACTOR>]
  • If -P <CASSANDRA_PORT> is not supplied, it defaults to 9042.
  • If -u <CASSANDRA_USER> is not supplied, it defaults to cassandra.
  • If -p <CASSANDRA_PASSWORD> is not supplied, it defaults to cassandra.
  • <NETWORK_STRATEGY> should be SimpleStrategy or NetworkTopologyStrategy

Delete tables

You can delete tables by using the properties file. To delete tables, run the following command, replacing the contents in the angle brackets as described:

$ java -jar scalardb-schema-loader-<VERSION>.jar --config <PATH_TO_SCALARDB_PROPERTIES_FILE> -f <PATH_TO_SCHEMA_FILE> [--coordinator] -D 

If --coordinator is specified, the Coordinator table will be deleted as well.

注記

The following database-specific CLI arguments have been deprecated. Please use the CLI arguments for configuring the properties file instead.

$ java -jar scalardb-schema-loader-<VERSION>.jar --cassandra -h <CASSANDRA_IP> [-P <CASSANDRA_PORT>] [-u <CASSANDRA_USER>] [-p <CASSANDRA_PASSWORD>] -f <PATH_TO_SCHEMA_FILE> -D

Sample schema file

The following is a sample schema. For a sample schema file, see schema_sample.json.

{
"sample_db.sample_table": {
"transaction": false,
"partition-key": [
"c1"
],
"clustering-key": [
"c4 ASC",
"c6 DESC"
],
"columns": {
"c1": "INT",
"c2": "TEXT",
"c3": "BLOB",
"c4": "INT",
"c5": "BOOLEAN",
"c6": "INT"
},
"secondary-index": [
"c2",
"c4"
]
},

"sample_db.sample_table1": {
"transaction": true,
"partition-key": [
"c1"
],
"clustering-key": [
"c4"
],
"columns": {
"c1": "INT",
"c2": "TEXT",
"c3": "INT",
"c4": "INT",
"c5": "BOOLEAN"
}
},

"sample_db.sample_table2": {
"transaction": false,
"partition-key": [
"c1"
],
"clustering-key": [
"c4",
"c3"
],
"columns": {
"c1": "INT",
"c2": "TEXT",
"c3": "INT",
"c4": "INT",
"c5": "BOOLEAN"
}
}
}

The schema has table definitions that include columns, partition-key, clustering-key, secondary-index, and transaction fields.

  • The columns field defines columns of the table and their data types.
  • The partition-key field defines which columns the partition key is composed of.
  • The clustering-key field defines which columns the clustering key is composed of.
  • The secondary-index field defines which columns are indexed.
  • The transaction field indicates whether the table is for transactions or not.
    • If you set the transaction field to true or don't specify the transaction field, this tool creates a table with transaction metadata if needed.
    • If you set the transaction field to false, this tool creates a table without any transaction metadata (that is, for a table with Storage API).

You can also specify database or storage-specific options in the table definition as follows:

{
"sample_db.sample_table3": {
"partition-key": [
"c1"
],
"columns": {
"c1": "INT",
"c2": "TEXT",
"c3": "BLOB"
},
"compaction-strategy": "LCS",
"ru": 5000
}
}

The database or storage-specific options you can specify are as follows:

The compaction-strategy option is the compaction strategy used. This option should be STCS (SizeTieredCompaction), LCS (LeveledCompactionStrategy), or TWCS (TimeWindowCompactionStrategy).

Scale for performance when using Cosmos DB for NoSQL or DynamoDB

When using Cosmos DB for NoSQL or DynamoDB, you can scale by using Request Units (RUs) or auto-scaling.

RUs

You can scale the throughput of Cosmos DB for NoSQL and DynamoDB by specifying the --ru option. When specifying this option, scaling applies to all tables or the ru parameter for each table.

If the --ru option is not set, the default values will be 400 for Cosmos DB for NoSQL and 10 for DynamoDB.

注記
  • Schema Loader abstracts Request Units for Cosmos DB for NoSQL and Capacity Units for DynamoDB with RU. Therefore, be sure to set an appropriate value depending on the database implementation.
  • Be aware that Schema Loader sets the same value to both read capacity unit and write capacity unit for DynamoDB.

Auto-scaling

By default, Schema Loader enables auto-scaling of RUs for all tables: RUs scale between 10 percent and 100 percent of a specified RU depending on the workload. For example, if you specify -r 10000, the RUs of each table auto-scales between 1000 and 10000.

注記

Auto-scaling for Cosmos DB for NoSQL is enabled only when this option is set to 4000 or more.

Data-type mapping between ScalarDB and other databases

The following table shows the supported data types in ScalarDB and their mapping to the data types of other databases.

ScalarDBMySQLPostgreSQLOracleSQL Server
BOOLEANbooleanbooleannumber(1)bit
INTintintintint
BIGINTbigintbigintnumber(19)bigint
FLOATdoublefloatbinary_floatfloat(24)
DOUBLEdoubledouble precisionbinary_doublefloat
TEXTlongtexttextvarchar2(4000)varchar(8000)
BLOBlongblobbyteaRAW(2000)varbinary(8000)

However, the following data types in JDBC databases are converted differently when they are used as a primary key or a secondary index key. This is due to the limitations of RDB data types.

ScalarDBMySQLPostgreSQLOracle
TEXTVARCHAR(64)VARCHAR(10485760)VARCHAR2(64)
BLOBVARBINARY(64)RAW(64)

The value range of BIGINT in ScalarDB is from -2^53 to 2^53, regardless of the underlying database.

If this data-type mapping doesn't match your application, please alter the tables to change the data types after creating them by using this tool.

Use Schema Loader in your application

You can check the version of Schema Loader from the Maven Central Repository. For example in Gradle, you can add the following dependency to your build.gradle file, replacing <VERSION> with the version of Schema Loader that you want to use:

dependencies {
implementation group: 'com.scalar-labs', name: 'scalardb-schema-loader', version: '<VERSION>'
}

Create or delete tables

You can create or delete tables that are defined in the schema by using Schema Loader. To do this, you can pass a ScalarDB properties file, schema, and additional options, if needed, as shown below:

public class SchemaLoaderSample {
public static int main(String... args) throws SchemaLoaderException {
Path configFilePath = Paths.get("database.properties");
Path schemaFilePath = Paths.get("sample_schema.json");
boolean createCoordinatorTable = true; // whether creating the coordinator table or not
boolean deleteCoordinatorTable = true; // whether deleting the coordinator table or not

Map<String, String> options = new HashMap<>();

options.put(
CassandraAdmin.REPLICATION_STRATEGY, ReplicationStrategy.SIMPLE_STRATEGY.toString());
options.put(CassandraAdmin.COMPACTION_STRATEGY, CompactionStrategy.LCS.toString());
options.put(CassandraAdmin.REPLICATION_FACTOR, "1");

options.put(DynamoAdmin.REQUEST_UNIT, "1");
options.put(DynamoAdmin.NO_SCALING, "true");
options.put(DynamoAdmin.NO_BACKUP, "true");

// Create tables
SchemaLoader.load(configFilePath, schemaFilePath, options, createCoordinatorTable);

// Delete tables
SchemaLoader.unload(configFilePath, schemaFilePath, deleteCoordinatorTable);

return 0;
}
}

You can also create or delete a schema by passing a serialized-schema JSON string (the raw text of a schema file) as shown below:

// Create tables
SchemaLoader.load(configFilePath, serializedSchemaJson, options, createCoordinatorTable);

// Delete tables
SchemaLoader.unload(configFilePath, serializedSchemaJson, deleteCoordinatorTable);

When configuring ScalarDB, you can use a Properties object as well, as shown below:

// Create tables
SchemaLoader.load(properties, serializedSchemaJson, options, createCoordinatorTable);

// Delete tables
SchemaLoader.unload(properties, serializedSchemaJson, deleteCoordinatorTable);