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.
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:
|
taskType | The mode in which to deliver data. Default value: BASE_INC. Valid values:
|
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");
}