|
| 1 | +--- |
| 2 | +tags: |
| 3 | + - Community |
| 4 | + - Enterprise |
| 5 | +displayed_sidebar: docsEnglish |
| 6 | +--- |
| 7 | + |
| 8 | +# Write a ScalarDL Application with TableStore |
| 9 | + |
| 10 | +import JavadocLink from "/src/theme/JavadocLink.js"; |
| 11 | + |
| 12 | +This document explains how to write ScalarDL applications with TableStore. You will learn how to interact with ScalarDL TableStore in your applications, handle errors, and validate your data. |
| 13 | + |
| 14 | +## Use the ScalarDL TableStore Client SDK |
| 15 | + |
| 16 | +You have two options to interact with ScalarDL TableStore: |
| 17 | + |
| 18 | +- Using [commands](scalardl-tablestore-command-reference.mdx), as shown in [Get Started with ScalarDL TableStore](getting-started-tablestore.mdx) |
| 19 | +- Using the [TableStore Java Client SDK](https://javadoc.io/doc/com.scalar-labs/scalardl-tablestore-java-client-sdk/) |
| 20 | + |
| 21 | +Using commands is a convenient way to try TableStore without writing an application. For building TableStore-based applications, however, the TableStore Client SDK is recommended, as it runs more efficiently without launching a separate process for each operation. |
| 22 | + |
| 23 | +The TableStore Client SDK is available on [Maven Central](https://central.sonatype.com/artifact/com.scalar-labs/scalardl-tablestore-java-client-sdk). You can install it in your application by using a build tool such as Gradle. For example, in Gradle, you can add the following dependency to `build.gradle`, replacing `VERSION` with the version of ScalarDL that you want to use. |
| 24 | + |
| 25 | +```gradle |
| 26 | +dependencies { |
| 27 | + implementation group: 'com.scalar-labs', name: 'scalardl-tablestore-java-client-sdk', version: '<VERSION>' |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +The Client SDK APIs for TableStore are provided by a service class called <JavadocLink packageName="scalardl-tablestore-java-client-sdk" path="com/scalar/dl/tablestore/client/service" className="TableStoreClientService" />. The following is a code snippet that shows how to use `TableStoreClientService` to manage table authenticity. `TableStoreClientService` provides the same functionalities as the TableStore client commands shown in [Get Started with ScalarDL TableStore](getting-started-tablestore.mdx). |
| 32 | + |
| 33 | +```java |
| 34 | + // TableStoreClientServiceFactory should always be reused. |
| 35 | + TableStoreClientServiceFactory factory = new TableStoreClientServiceFactory(); |
| 36 | + |
| 37 | + // TableStoreClientServiceFactory creates a new TableStoreClientService object in every create |
| 38 | + // method call but reuses the internal objects and connections as much as possible for better |
| 39 | + // performance and resource usage. |
| 40 | + TableStoreClientService service = factory.create(new ClientConfig(new File(properties))); |
| 41 | + try { |
| 42 | + // execute a SQL statement. |
| 43 | + String sql = "SELECT * FROM employee WHERE id = '1001'"; |
| 44 | + ExecutionResult result = service.executeStatement(sql); |
| 45 | + result.getResult().ifPresent(System.out::println); |
| 46 | + } catch (ClientException e) { |
| 47 | + System.err.println(e.getStatusCode()); |
| 48 | + System.err.println(e.getMessage()); |
| 49 | + } |
| 50 | + |
| 51 | + factory.close(); |
| 52 | +``` |
| 53 | + |
| 54 | +:::note |
| 55 | + |
| 56 | +You should always use `TableStoreClientServiceFactory` to create `TableStoreClientService` objects. `TableStoreClientServiceFactory` caches objects that are required to create `TableStoreClientService` and reuses them on the basis of the given configurations, so the `TableStoreClientServiceFactory` object should always be reused. |
| 57 | + |
| 58 | +::: |
| 59 | + |
| 60 | +For more information about `TableStoreClientServiceFactory` and `TableStoreClientService`, see the [`scalardl-tablestore-java-client-sdk` Javadoc](https://javadoc.io/doc/com.scalar-labs/scalardl-tablestore-java-client-sdk/latest/index.html). |
| 61 | + |
| 62 | +## Handle errors |
| 63 | + |
| 64 | +If an error occurs in your application, the Client SDK will return an exception with a status code and an error message with an error code. You should check the status code and the error code to identify the cause of the error. For details about the status code and the error codes, see [Status codes](how-to-write-applications.mdx#status-codes) and [Error codes](how-to-write-applications.mdx#error-codes). |
| 65 | + |
| 66 | +### Implement error handling |
| 67 | + |
| 68 | +The SDK throws <JavadocLink packageName="scalardl-java-client-sdk" path="com/scalar/dl/client/exception" className="ClientException" /> when an error occurs. You can handle errors by catching the exception as follows: |
| 69 | + |
| 70 | +```java |
| 71 | +TableStoreClientService service = ...; |
| 72 | +try { |
| 73 | + // interact with ScalarDL TableStore through a TableStoreClientService object |
| 74 | +} catch (ClientException e) { |
| 75 | + // e.getStatusCode() returns the status of the error |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +## Validate your data |
| 80 | + |
| 81 | +In ScalarDL, you occasionally need to validate your data to make sure all the data is in a valid state. Since you can learn the basics of how ScalarDL validates your data in [Write a ScalarDL Application in Java](how-to-write-applications.mdx#validate-your-data), this section mainly describes how you can perform the validation in TableStore. |
| 82 | + |
| 83 | +When validating [assets](data-modeling.mdx#asset) (records, index records, and table schema here) in TableStore, you need to specify a table and a primary or index key if necessary. An example code for validating assets in TableStore is as follows: |
| 84 | + |
| 85 | +```java |
| 86 | + TableStoreClientService service = ... |
| 87 | + String tableName = "employee"; |
| 88 | + String primaryKeyColumn = "id"; |
| 89 | + String indexKeyColumn = "department"; |
| 90 | + TextNode primaryKeyValue = TextNode.valueOf("1001"); |
| 91 | + TextNode indexKeyValue = TextNode.valueOf("sales"); |
| 92 | + try { |
| 93 | + LedgerValidationResult result1 = |
| 94 | + service.validateRecord(tableName, primaryKeyColumn, primaryKeyValue); |
| 95 | + LedgerValidationResult result2 = |
| 96 | + service.validateIndexRecord(tableName, indexKeyColumn, indexKeyValue); |
| 97 | + LedgerValidationResult result3 = service.validateTableSchema(tableName); |
| 98 | + // You can also specify age range. |
| 99 | + // LedgerValidationResult result1 = |
| 100 | + // service.validateRecord(tableName, primaryKeyColumn, primaryKeyValue, startAge, endAge); |
| 101 | + // LedgerValidationResult result2 = |
| 102 | + // service.validateIndexRecord(tableName, indexKeyColumn, indexKeyValue, startAge, endAge); |
| 103 | + // LedgerValidationResult result3 = service.validateTableSchema(tableName, startAge, endAge); |
| 104 | + } catch (ClientException e) { |
| 105 | + } |
| 106 | +``` |
| 107 | + |
| 108 | +:::note |
| 109 | + |
| 110 | +TableStore internally assigns a dedicated asset ID to an asset that represents a record, an index record, and a table schema. The asset ID consists of a prefix to show the asset type and a key; for example, a prefix `rec_`, a primary key column name, and a primary key value are used for asset IDs of records. You will see such raw asset IDs in `AssetProof` in `LedgerValidationResult`. |
| 111 | + |
| 112 | +::: |
0 commit comments