All Products
Search
Document Center

Tablestore:Create a delivery task

Last Updated:Jun 14, 2024

To deliver data in a Tablestore data table to an Object Storage Service (OSS) bucket, you can call the CreateDeliveryTask operation to create a delivery task.

Important

Tablestore SDK for Java V5.10.3 and later support the data delivery feature. Make sure that you use a version of Tablestore SDK for Java that supports the data delivery feature. For more information about Tablestore SDK for Java, see Version history of Tablestore SDK for Java.

Prerequisites

  • OSS is activated. A bucket is created in the region where a Tablestore instance is deployed. For more information, see Activate OSS.

  • The Tablestore service-linked role (AliyunServiceRoleForOTSDataDelivery) is created in the Tablestore console. The Alibaba Cloud Resource Name (ARN) of the role is recorded. For more information, see Create a data delivery task.

    You can perform the following operations in the RAM console to obtain the ARN of the Tablestore service-linked role (AliyunServiceRoleForOTSDataDelivery):

    On the Roles page, search for AliyunServiceRoleForOTSDataDelivery. Then, click the RAM role name. On the role details page, you can view and copy the ARN information about the role.

  • An OTSClient instance is initialized. For more information, see Initialize an OTSClient instance.

  • A data table is created, and data is written to the data table.

Parameters

Parameter

Description

tableName

The name of the table.

taskName

The name of the delivery task.

The name must be 3 to 16 characters in length and can contain only lowercase letters, digits, and hyphens (-). It must start and end with a lowercase letter or digit.

taskConfig

The configurations of the delivery task. Valid values:

  • ossPrefix: the prefix of the folder in the bucket. Data is delivered from Tablestore to the folder. The path of the destination folder supports the following time variables: $yyyy, $MM, $dd, $HH, and $mm.

    • When the path uses time variables for delivery, OSS folders are dynamically generated based on the time when data is written. This way, data is partitioned based on the naming conventions that are followed when Hive partitions data. Objects in OSS are organized, partitioned, and distributed based on time.

    • When the path does not use time variables, all files are delivered to an OSS folder whose name contains a specific prefix.

  • ossBucket: the name of the OSS bucket.

  • ossEndpoint: the service address of the region where an OSS bucket is deployed.

  • ossStsRole: the Alibaba Cloud Resource Name (ARN) of the Tablestore service linked role.

  • format: the format of the delivered data. The delivered data is stored in the Parquet format. By default, delivery service uses PLAIN to encode data of any type.

  • eventTimeColumn: the event time column. This parameter specifies that data is partitioned based on the time of a column. If you do not specify this parameter, data is partitioned based on the time when the data is written to Tablestore.

  • parquetSchema: specifies the column you want to delivery. You must configure the source fields, destination fields, and destination field types to delivery.

    The order in which fields are sorted in Tablestore can be different from the order of fields in the schema. Parquet data stored in OSS is distributed based on the order of fields in the schema.

    Important

    The data types must be consistent between the source and destination fields. If the data types between the fields are not consistent, the fields are discarded as dirty data. For more information about field type mappings, see the "Data type mappings" section of the Quick start topic.

taskType

The mode in which to deliver data. Default value: BASE_INC. Valid values:

  • INC: the incremental data delivery mode. Only incremental data is synchronized.

  • BASE: the full data delivery mode. All data in tables is scanned and synchronized.

  • BASE_INC: the differential data delivery mode. After the full data is synchronized, Tablestore synchronizes the incremental data. When you synchronize data in incremental mode, you can view the time when data is last delivered and the status of the current delivery task.

Examples

The following sample code provides an example on how to create a delivery task for a data table:

private static void createDeliveryTask(SyncClient client) {
    String tableName = "<TABLE_NANE>";
    String taskName = "<TASK_NAME>";
    OSSTaskConfig taskConfig = new OSSTaskConfig();
    taskConfig.setOssPrefix("sampledeliverytask/year=$yyyy/month=$MM");
    taskConfig.setOssBucket("datadeliverytest");
    taskConfig.setOssEndpoint("oss-cn-hangzhou.aliyuncs.com");
    taskConfig.setOssStsRole("acs:ram::17************45:role/aliyunserviceroleforotsdatadelivery"); // eventColumn is optional. eventColumn specifies that data is partitioned based on the time of a column. If you do not specify this parameter, data is partitioned based on the time when the data is written to Tablestore. 
    EventColumn eventColumn = new EventColumn("PK1", EventTimeFormat.RFC1123);
    taskConfig.setEventTimeColumn(eventColumn);
    taskConfig.addParquetSchema(new ParquetSchema("PK1", "PK1", DataType.UTF8));
    taskConfig.addParquetSchema(new ParquetSchema("PK2", "PK2", DataType.BOOL));
    taskConfig.addParquetSchema(new ParquetSchema("Col1", "Col1", DataType.UTF8));
    CreateDeliveryTaskRequest request = new CreateDeliveryTaskRequest();
    request.setTableName(tableName);
    request.setTaskName(taskName);
    request.setTaskConfig(taskConfig);
    request.setTaskType(DeliveryTaskType.BASE_INC);
    CreateDeliveryTaskResponse response = client.createDeliveryTask(request);
    System.out.println("resquestID: "+ response.getRequestId());
    System.out.println("traceID: " + response.getTraceId());
    System.out.println("create delivery task success");
}