This topic describes how to create a Tablestore sink connector to export data from a topic in an ApsaraMQ for Kafka instance to Tablestore.
Prerequisites
-
ApsaraMQ for Kafka
-
You have enabled the connector feature for the instance. For more information, see Enable connectors.
-
You have created a source topic for the instance. For more information, see Step 1: Create a topic.
-
-
Tablestore
-
You have activated Tablestore and created an instance. For more information, see Activate Tablestore and create an instance.
-
Notes
-
You can only export data from a source topic in an ApsaraMQ for Kafka instance to Tablestore within the same region. For information about connector limits, see Usage limits.
-
When you create a connector, ApsaraMQ for Kafka automatically creates a service-linked role.
-
If a service-linked role does not exist, ApsaraMQ for Kafka automatically creates one, enabling ApsaraMQ for Kafka to export data to Tablestore.
-
If a service-linked role already exists, ApsaraMQ for Kafka does not create a new one.
For more information about service-linked roles, see Service-linked roles.
-
Procedure
This topic describes how to use a Tablestore sink connector to export data from a source topic in an ApsaraMQ for Kafka instance to Tablestore.
-
Optional: Create the topics and group required for the Tablestore sink connector.
If you do not need to customize the topics and group, you can skip this step and select Auto in the next step.
ImportantSome topics required by the Tablestore sink connector must use the Local storage engine. For ApsaraMQ for Kafka instances of major version 0.10.2, you cannot manually create topics that use the Local storage engine. These topics can only be created automatically.
-
Verify the result
Create topics for the Tablestore sink connector
In the ApsaraMQ for Kafka console, manually create the five topics required for the Tablestore sink connector: a task offset topic, a task configuration topic, a task status topic, a dead-letter queue topic, and an error data topic. The required number of partitions and the storage engine vary for each topic. For more information, see List of source service parameters.
Log on to the ApsaraMQ for Kafka console.
On the Overview page, select a region in the Resource Distribution section.
ImportantYou must create topics in the same region as your application, which is the region where the ECS instance is deployed. Topics cannot be used across regions. For example, if a topic is created in the China (Beijing) region, the message producer and consumer must also run on an ECS instance in the China (Beijing) region.
On the Instances page, click the name of the target instance.
In the navigation pane on the left, click Topics.
On the Topics page, click Create Topic.
In the Create Topic panel, set the topic properties, and then click OK.
Parameter
Description
Example
Name
The name of the topic.
NoteIn Kafka, topic names such as
xxx_xxxandxxx.xxxare considered the same. An error is reported if you try to create a topic with a duplicate name.demo
Description
A brief description of the topic.
demo test
Partitions
The number of partitions in the topic.
12
Storage Engine
NoteCurrently, you can select a storage engine type only for non-Serverless Professional Edition instances. For other instances, this parameter is not supported and is set to Cloud Storage by default.
The storage engine for topic messages.
ApsaraMQ for Kafka supports the following two storage engines.
Cloud Storage: The underlying layer uses Alibaba Cloud disks. This storage engine features low latency, high performance, high durability, and high reliability. It uses a distributed three-replica mechanism. If the Instance Edition of the instance is Standard (High Write), the storage engine can only be Cloud Storage.
Local Storage: Uses the native Kafka in-sync replica (ISR) replication algorithm and a distributed three-replica mechanism.
Cloud Storage
Message Type
The type of topic messages.
Normal Message: By default, messages with the same key are distributed to the same partition, and messages within a partition are stored in the order they are sent. If a machine in the cluster goes down, messages may become disordered. When you set Storage Engine to Cloud Storage, Normal Message is selected by default.
Partitionally Ordered Message: By default, messages with the same key are distributed to the same partition, and messages within a partition are stored in the order they are sent. If a machine in the cluster goes down, the order of messages is still guaranteed within the partition. However, sending messages to some partitions may fail. Normal operations resume after the partitions recover. When you set Storage Engine to Local Storage, Partitionally Ordered Message is selected by default.
Normal Message
Log Cleanup Policy
The cleanup policy for topic logs.
When you set Storage Engine to Local Storage, you must configure the Log Cleanup Policy. You can set Storage Engine to Local Storage only for Professional Edition instances. Standard Edition instances are not supported.
ApsaraMQ for Kafka supports the following two cleanup policies.
Delete: The default message cleanup policy. If disk capacity is sufficient, messages are retained for the maximum retention period. If disk capacity is insufficient (generally, when disk usage exceeds 85%), old messages are deleted early to ensure service availability.
Compact: Uses the Kafka Log Compaction cleanup policy. The Log Compaction policy ensures that for messages with the same key, the latest value is always retained. This is mainly applicable to scenarios such as recovering state after a system breakdown or reloading the cache after a system restart. For example, when using Kafka Connect or Confluent Schema Registry, you must use a Kafka Compact Topic to store system state or configuration information.
ImportantCompact topics are generally used only in certain ecosystem components, such as Kafka Connect or Confluent Schema Registry. Do not set this property for topics in other message sending and receiving scenarios. For more information, see ApsaraMQ for Kafka Demo Library.
Compact
Tag
The tags of the topic.
demo
After the topic is created, it is displayed in the list on the Topics page.
Create a group for Tablestore sink connector
You can manually create a group for the Tablestore sink connector data synchronization task in the ApsaraMQ for Kafka console. The name of this group must be connect-Task Name. For more information, see Source service parameters.
Log on to the ApsaraMQ for Kafka console.
On the Overview page, select a region in the Resource Distribution section.
On the Instances page, click the name of the target instance.
In the navigation pane on the left, click Groups.
On the Groups page, click Create Group.
In the Create Group panel, enter a name for the group in the Group ID text box, enter a brief description in the Description text box, add tags to the group, and then click OK.
After the group is created, it is displayed in the list on the Groups page.
Create and deploy a Tablestore sink connector
Create and deploy a Tablestore sink connector to synchronize data from an ApsaraMQ for Kafka instance to a Tablestore table.
Log on to the ApsaraMQ for Kafka console.
On the Overview page, select a region in the Resource Distribution section.
In the left-side navigation pane, click Connectors.
On the Connectors page, select the instance to which the connector belongs from the Select Instance drop-down list and click Create Connector.
-
In the Create Connector wizard, complete the following steps.
-
On the Configure Basic Information tab, configure the following parameters and then click Next.
Parameter
Description
Example
Name
The name of the connector. The name must meet the following requirements:
-
The name can be up to 48 characters long and can contain only digits, lowercase letters, and hyphens (-). It cannot start with a hyphen (-).
-
The name must be unique within an ApsaraMQ for Kafka instance.
The connector's data synchronization task uses a consumer group named
connect-task-name. If you do not create this consumer group manually, the system creates it automatically.kafka-ts-sink
Instance
By default, the instance name and instance ID are displayed.
demo alikafka_post-cn-st21p8vj****
-
-
On the Configure Source Service tab, set Data Source to ApsaraMQ for Kafka, configure the following parameters, and then click Next.
NoteIf you have already created the required topic and group, select Manual for resource creation and enter their information. Otherwise, select Auto.
Table 1. Parameters for the source service
Parameter
Description
Example
Data Source Topic
The source topic for data synchronization.
ts-test-input
Consumer Thread Concurrency
The number of concurrent consumer threads for the data source topic. The default value is 6. Valid values:
-
1
-
2
-
3
-
6
-
12
6
Consumer Offset
The starting position for message consumption. Valid values:
-
Earliest Offset: Consumes messages from the earliest offset.
-
Latest Offset: Consumes messages from the latest offset.
Earliest Offset
VPC ID
The ID of the virtual private cloud (VPC) where the data synchronization task runs. This parameter is displayed after you click Configure Runtime Environment. This parameter defaults to the VPC of the ApsaraMQ for Kafka instance and requires no configuration.
vpc-bp1xpdnd3l***
vSwitch ID
The ID of the vSwitch where the data synchronization task runs. This parameter is displayed after you click Configure Runtime Environment. The vSwitch must be in the same VPC as the ApsaraMQ for Kafka instance. This parameter defaults to the vSwitch specified when the ApsaraMQ for Kafka instance was deployed.
vsw-bp1d2jgg81***
Failure Handling Policy
The policy for handling message-sending failures in a topic partition. This parameter is displayed after you click Configure Runtime Environment. Valid values:
-
Continue Subscription: Continues the subscription to the topic partition where the error occurred and prints error logs.
-
Stop Subscription: Stops the subscription to the topic partition where the error occurred and prints error logs.
Note-
For more information about how to view logs, see Manage connectors.
-
For more information about how to find solutions based on error codes, see Error codes.
Continue Subscription
Resource Creation Method
The method for creating the required topics and group. This parameter is displayed after you click Configure Runtime Environment.
-
Auto
-
Manual
Auto
Connector Consumer Group
The consumer group used by the data synchronization task. This parameter is displayed after you click Configure Runtime Environment. The name of the group must be in the format connect-task-name.
connect-cluster-kafka-ots-sink
Task Offset Topic
The topic that stores consumer offsets. This parameter is displayed after you click Configure Runtime Environment.
-
Topic: The topic name must start with connect-offset.
-
Partitions: The number of partitions for the topic must be greater than 1.
-
Storage engine: The storage engine for the topic must be Local Storage.
-
cleanup.policy: The log cleanup policy for the topic must becompact.
connect-offset-kafka-ots-sink
Task Configuration Topic
The topic that stores task configurations. This parameter is displayed after you click Configure Runtime Environment.
-
Topic: The topic name must start with connect-config.
-
Partitions: The number of partitions for the topic must be 1.
-
Storage engine: The storage engine for the topic must be Local Storage.
-
cleanup.policy: The log cleanup policy for the topic must becompact.
connect-config-kafka-ots-sink
Task Status Topic
The topic that stores task status. This parameter is displayed after you click Configure Runtime Environment.
-
Topic: The topic name must start with connect-status.
-
Partitions: The recommended number of partitions is 6.
-
Storage engine: The storage engine for the topic must be Local Storage.
-
cleanup.policy: The log cleanup policy for the topic must becompact.
connect-status-kafka-ots-sink
Dead-letter Queue Topic
The topic used to store error data from the Kafka Connect framework. This parameter is displayed after you click Configure Runtime Environment. To save topic resources, you can use the same topic for the dead-letter queue and the Error Data Topic.
-
Topic: The topic name must start with connect-error.
-
Partitions: The recommended number of partitions is 6.
-
Storage engine: The storage engine for the topic can be Local Storage or Cloud Storage.
connect-error-kafka-ots-sink
Error Data Topic
The topic used to store error data from the sink. This parameter is displayed after you click Configure Runtime Environment. To save topic resources, you can use the same topic for the Dead-letter Queue Topic and the error data topic.
-
Topic: The topic name must start with connect-error.
-
Partitions: The recommended number of partitions is 6.
-
Storage engine: The storage engine for the topic can be Local Storage or Cloud Storage.
connect-error-kafka-ots-sink
-
-
On the Configure Destination Service tab, set Destination Service to Tablestore, configure the following parameters, and then click Create.
Parameter
Description
Example
Instance Name
The name of the Tablestore instance.
k00eny67****
Automatically Create Destination Table
Specifies whether to automatically create a table in Tablestore.
-
Yes: A table is automatically created in Tablestore to store synchronized data based on the configured table name.
-
No: An existing table is used to store synchronized data.
Yes
Destination Table Name
The name of the table that stores the synchronized data. If you set Automatically Create Destination Table to No, the table name must be the same as the name of an existing table in the Tablestore instance.
kafka_table
Tablestore
The type of the table that stores the synchronized data.
-
Wide Column Model
-
TimeSeries Model
Wide Column Model
Message Key Format
The input format of the message key. Valid values are String and JSON. The default value is JSON. This parameter is displayed only when Tablestore is set to Wide Column Model.
-
String: The message key is parsed directly as a string.
-
JSON: The message key must be in JSON format.
String
Message Value Format
The input format of the message value. Valid values are String and JSON. The default value is JSON. This parameter is displayed only when Tablestore is set to Wide Column Model.
-
String: The message value is parsed directly as a string.
-
JSON: The message value must be in JSON format.
String
JSON Message Field Conversion
The method for processing fields in a JSON message. This parameter is displayed if you set Message Key Format or Message Value Format to JSON. Valid values:
-
Write All as String: Converts all fields to the String type in Tablestore.
-
Automatically Identify Field Types: Converts String and Boolean fields in the JSON message body to the corresponding String and Boolean types in Tablestore. Integer and Float data types in the JSON message body are converted to the Double type in Tablestore.
Write All as String
Primary Key Mode
Specifies the primary key mode. You can extract table primary keys from different parts of ApsaraMQ for Kafka message records, including the coordinates (topic, partition, and offset), key, and value. This parameter is displayed only when Tablestore is set to Wide Column Model. The default value is kafka.
-
kafka: Uses <connect_topic>_<connect_partition> and <connect_offset> as the primary keys of the data table.
-
record_key: Uses fields in the record key as the primary keys of the data table.
-
record_value: Uses fields in the record value as the primary keys of the data table.
kafka
Primary Key Column Names
The primary key column names of the data table and their corresponding data types. String and Integer data types are supported. This means that fields in the record key or value that match the configured column names are used as the primary keys for the data table.
This parameter is displayed if you set Message Key Format to JSON and Primary Key Mode to record_key, or if you set Message Value Format to JSON and Primary Key Mode to record_value.
Click Create to add a column name. You can configure a maximum of four column names.
None
Write Mode
Specifies the write mode. Valid values are put and update. The default value is put. This parameter is displayed only when Tablestore is set to Wide Column Model.
-
put: Overwrites existing data.
-
update: Updates existing data.
put
Delete Mode
If an ApsaraMQ for Kafka message record contains null values, you can choose whether to delete rows or attribute columns. This parameter is displayed when Primary Key Mode is set to record_key. Valid values:
-
none: The default value. No deletion is allowed.
-
row: Allows row deletion.
-
column: Allows attribute column deletion.
-
row_and_column: Allows row and attribute column deletion.
The delete behavior depends on the write mode:
-
If Write Mode is put, any delete mode results in overwrite writes to the Tablestore data table, even when the value contains null fields.
-
If Write Mode is update and the delete mode is none or row, records in which all value fields are null are treated as dirty data. If only some value fields are null, the connector ignores the null fields and writes the non-null fields to the Tablestore data table. If the delete mode is column or row_and_column, the connector deletes rows and attribute columns for null fields and then writes the data to the Tablestore data table.
None
Metric Name Field
This field is mapped to the measurement name field (_m_name) in the Tablestore TimeSeries Model. The measurement name specifies the name of the physical quantity or monitoring metric for the time-series data, such as temperature or speed. This field cannot be empty. This parameter is displayed when you select the TimeSeries Model for Tablestore.
measurement
Data Source Field
Map this field to the data source field (_data_source) in the Tablestore TimeSeries Model. This field acts as an identifier for the source of time-series data, such as a machine name or device ID, and can be empty. This parameter is displayed when the TimeSeries model is selected for Tablestore.
source
Tag Field
Use one or more fields as the tag fields (_tags) for a Tablestore TimeSeries Model. Each tag is a key-value pair of strings. The key is the name of the configured field, and the value is the content of the field. Tags are part of the time series metadata. A time series is uniquely identified by the combination of its metric name, data source, and tags. Tags can be empty. This parameter is displayed when Tablestore is selected as the time series model.
tag1, tag2
Timestamp Field
Maps this field to the timestamp field (_time) in the Tablestore TimeSeries Model. It represents the point in time for this row of time-series data, such as the time when a physical quantity is generated. When data is written to Tablestore, the timestamp field is converted to microseconds for writing and storage. This parameter is displayed when you select the TimeSeries Model for Tablestore.
time
Timestamp Unit
Configure this parameter based on the actual timestamp field. This parameter is displayed only when Tablestore is set to TimeSeries Model. Valid values:
-
SECONDS
-
MILLISECONDS
-
MICROSECONDS
-
NANOSECONDS
MILLISECONDS
Whether to Map All Non-primary Key Fields
Specifies whether to map all non-primary key fields as data fields. Non-primary key fields are fields that are not already mapped as metric name, data source, tag, or timestamp. This parameter is displayed only when Tablestore is set to TimeSeries Model. Valid values:
-
Yes: Fields are automatically mapped and their data types are determined. Numeric types are all converted to the Double type.
-
No: You must specify the fields and types to be mapped.
Yes
Configure Mapping for All Non-primary Key Fields
The field types corresponding to the non-primary key field names of the time-series table. Double, Integer, String, Binary, and Boolean data types are supported. This parameter is displayed if you set Whether to Map All Non-primary Key Fields to No.
String
After the connector is created, you can view it on the Connectors page.
-
-
-
After the connector is created, find it on the Connectors page and click Deploy in the Actions column.
-
Click OK.
Send a test message
After you deploy the Tablestore sink connector, you can send a message to the source topic in ApsaraMQ for Kafka to verify that data is synchronized to Tablestore.
On the Connectors page, find the target connector and click Test in the Actions column.
In the Send Message panel, send a test message.
Set Sending Method to Console.
In the Message Key text box, enter the key of the message. For example, demo.
In the Message Content text box, enter the content of the test message. For example, {"key": "test"}.
Set Send to Specified Partition to specify whether to send the message to a specific partition.
Click Yes and enter the partition ID in the Partition ID text box. For example, 0. To query the partition ID, see View partition status.
Click No to not specify a partition.
Set Sending Method to Docker. Run the Docker command in the Run the Docker container to produce a sample message section to send the message.
Set Sending Method to SDK. Select an SDK for the required language or framework and a connection type to send messages.
View table data
After you send a message to the data source topic in ApsaraMQ for Kafka, view the data in the Tablestore table to confirm that the message was received.
-
Log on to the Tablestore console.
-
On the Overview page, click the instance name or click Instances in the Actions column.
-
On the Instance Details tab, find the target table in the Tables section.
-
Click the table name. On the Table Manage page, click the Data Management tab to view the data.