This topic describes how to create a Function Compute sink connector to synchronize data from a source topic in your ApsaraMQ for Kafka instance to a function in Function Compute.

Prerequisites

For information about the prerequisites, see Prerequisites.

Usage notes

  • You must select event functions as the type of the destination function.
  • If you deliver messages from Message Queue for Apache Kafka to Function Compute that resides in the same region, the data is transmitted over the internal network. In this case, you are not charged the Internet traffic fee.

Step 1: Create Function Compute resources

Create a function in Function Compute. For more information, see Create a function in the Function Compute console.

In this example, an event function named sink-fc is created for the guide-fc-sink-service service that runs in the Python runtime environment. Function code:

# -*- coding: utf-8 -*-
import logging
import json

def handler(event, context):
  logger = logging.getLogger()
  evt = json.loads(event)
  logger.info(evt)
  return "success"

Step 2: Create and start a Function Compute sink connector

  1. Log on to the ApsaraMQ for Kafka console. In the Resource Distribution section of the Overview page, click the name of the region where your instance is deployed.
  2. In the left-side navigation pane, choose Connector Ecosystem Integration > Message Outflow.
  3. On the Message Outflow page, click Create Task.
  4. In the Create Message Outflow Task panel, configure the parameters and click Confirm.
    1. In the Basic Information section, configure the Task Name parameter and select Function Compute from the Message Outflow Task Type drop-down list.
    2. In the Resource Configuration section, configure the parameters. The following table describes the parameters.
      Table 1. Source Message Queue for Apache Kafka
      ParameterDescriptionExample
      RegionSelect the region where the Message Queue for Apache Kafka instance resides. China (Hangzhou)
      Message Queue for Apache Kafka InstanceThe ID of the Message Queue for Apache Kafka instance. alikafka_post-cn-9hdsbdhd****
      TopicThe source topic in the Message Queue for Apache Kafka instance. guide-sink-topic
      Group IDThe ID of the group in the Message Queue for Apache Kafka instance.
      • Quickly Create: The system automatically creates a group whose ID is in the GID_EVENTBRIDGE_xxx format.
      • Use Existing Group: Select the ID of an existing group that is not being used. If you select an existing group that is being used, the publishing and subscription of existing messages are affected.
      Use Existing Group
      Concurrency Quota (Consumers)The number of threads that concurrently consume data in the topic. The following items describe the mapping between threads and partitions in a topic:
      • If the number of partitions in a topic is equal to the number of threads that concurrently consume data in the topic, one thread consumes data in one partition. We recommend that you use this configuration.
      • If the number of partitions in a topic is greater than the number of threads that concurrently consume data in the topic, the threads evenly consume data in all partitions.
      • If the number of partitions in a topic is smaller than the number of threads that concurrently consume data in the topic, one thread consumes data in one partition. The additional threads do not consume data.
      2
      Consumer Offset
      • Latest Offset: Consume messages from the most recent offset.
      • Earliest Offset: Consume messages from the earliest offset.
      Latest Offset
      Network ConfigurationIf cross-border data transmission is required, select Internet. In other cases, select Default Network. Default Network
      Table 2. Target Function Compute
      ParameterDescriptionExample
      ServiceSelect the Function Compute service that you created. guide-fc-sink-service
      FunctionSelect the Function Compute function that you created. sink-fc
      Invocation Mode
      • Asynchronous: sends requests to Function Compute in asynchronous mode. In this mode, the size of the payload is limited to 128 KB.
      • Synchronous: sends requests to Function Compute in synchronous mode. In this mode, the size of the payload is limited to 32 MB. The timeout period for a synchronous request is 5 minutes.
      Synchronous
      Batch PushYou can use the batch push feature to aggregate multiple events in a batch. Batch push is triggered when the condition that is specified by the Messages parameter or the Interval parameter is met. For example, you set the Messages parameter to 100 and the Interval parameter to 15 seconds. If the number of messages reaches 100 in 10 seconds, batch push is immediately triggered. Valid values:
      • Enable
      • Disable: If you set this parameter to Disable, only one message is delivered to Function Compute at a time.
      Enable
      MessagesThe maximum number of messages that can be sent for each function invocation. Requests are sent only when the number of messages in the backlog reaches the specified value. Valid values: [1,10000]. This parameter is required only if the Batch Push parameter is set to Enable. 100
      IntervalThe interval at which you want to invoke the function. The system sends the aggregated messages to Function Compute when the specified interval is reached. Valid values: 0 to 15. Unit: seconds. The value 0 specifies that messages are immediately sent after aggregation. This parameter is required only if the Batch Push parameter is set to Enable. 10
    After you perform the preceding operations, go to the Message Outflow page, find the Function Compute sink connector that you created, and then click Start in the Actions column. If the status in the Status column changes from Starting to Running, the connector is created.

Step 3: Test the Function Compute sink connector

  1. On the Message Outflow page, find the Function Compute sink connector that you created and click the name of the source topic in the Event Source column.
  2. On the Topic Details page, click Send Message.
  3. In the Start to Send and Consume Message panel, configure the parameters based on the following figure and click OK.
    Send messages
  4. On the Message Outflow page, find the Function Compute sink connector that you created and click the name of the destination function in the Event Target column.
  5. Click the Logs tab. Then, click the Requests tab to view the invocation logs.
    Logs