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.
Make sure that the version of the installed Tablestore SDK for Go supports the data delivery feature.
Prerequisites
OSS is activated. A bucket is created in the region in which 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.
A TableStoreClient 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 data 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 (-). The name must start and end with a lowercase letter or digit. |
TaskConfig | The configurations of the delivery task, which includes the following content:
|
TaskType | The type of the delivery task. Default value: BaseIncTask. Valid values:
|
Examples
The following sample code provides an example on how to create a delivery task for a data table:
func CreateTaskSample(client *tablestore.TableStoreClient) {
createTask := &tablestore.CreateDeliveryTaskRequest{
TableName: "<TABLE_NAME>",
TaskName: "<TASK_NAME>",
TaskType: tablestore.BaseIncTask,
TaskConfig: &tablestore.OSSTaskConfig{
OssPrefix: "sample/year=$yyyy/month=$MM",
OssBucket: "datadeliverytest",
OssEndpoint: "oss-cn-hangzhou.aliyuncs.com",
OssRoleName: "acs:ram::17************45:role/aliyunserviceroleforotsdatadelivery",
Schema: []*tablestore.TaskSchema{
{
ColumnName: "PK1",
OssColumnName: "PK1",
Type: tablestore.ParquetInt64,
},
{
ColumnName: "PK2",
OssColumnName: "PK2",
Type: tablestore.ParquetUtf8,
},
{
ColumnName: "Col1",
OssColumnName: "Col1",
Type: tablestore.ParquetDouble,
},
},
},
}
createResp, err := client.CreateDeliveryTask(createTask)
if err != nil {
log.Fatal("create delivery task failed ", err)
}
fmt.Println("create delivery task success ", createResp.RequestId)
}