Migrate data from ApsaraMQ for Kafka to MaxCompute

Updated at:

This topic describes how to use the DataWorks Data Integration feature to migrate data from an ApsaraMQ for Kafka cluster to MaxCompute for offline analysis and processing.

Prerequisites

Before you begin, ensure you have completed the following in the same region:

  • ApsaraMQ for Kafka

    • Purchase and deploy an ApsaraMQ for Kafka instance. For more information, see Purchase and deploy an instance. This tutorial uses an instance deployed in the China (Hangzhou) region as an example.

      Note

      Data synchronization supports all ApsaraMQ for Kafka instance deployment versions (from 0.10.x to 2.x), editions (Standard and Professional), and network types (VPC and Public/VPC). You can select the configurations based on your business needs.

    • Create a topic and a consumer group. For more information, see Step 3: Create resources. This tutorial uses a topic named testkafka and a consumer group named console-consumer as an example. The console-consumer consumer group will consume data from the testkafka topic.

  • A MaxCompute project is created. This tutorial uses a project named bigdata_DOC in the China (Hangzhou) region as an example.

  • A DataWorks workspace is created and bound to a MaxCompute compute resource.

Background information

MaxCompute (formerly ODPS) is a big data computing service that provides a fast, fully managed, and maintenance-free cloud data warehouse solution at an exabyte scale.

Built on MaxCompute for storage and computation, DataWorks is a one-stop platform for large-scale offline data processing and analysis that provides visual workflow development and managed scheduling and O&M. The DataWorks console also serves as the MaxCompute console. Together, MaxCompute and DataWorks provide comprehensive data processing and data warehouse management capabilities. They support multiple classic distributed computing models, such as SQL, MapReduce (MR), and Graph, to solve large-scale data computing problems, reduce costs, and ensure data security.

This tutorial shows you how to use DataWorks to import data from ApsaraMQ for Kafka into MaxCompute to explore the value of your big data.

Step 1: Prepare data in ApsaraMQ for Kafka

Write data to the testkafka topic. This data will be migrated to MaxCompute. Because ApsaraMQ for Kafka is used to process streaming data, you can continuously write data to it. To ensure the test results, we recommend that you write more than 10 records.

  1. Log on to the ApsaraMQ for Kafka console.

  2. On the Overview page, select a region in the Resource Distribution section.

  3. On the Instances page, click the name of the target instance.

  4. In the navigation pane on the left, click Topics.

  5. On the Topics page, click the Name of the target topic to open the Topic Details page, and then click Send Message.

  6. In the Start to Send and Consume Message panel, send a test message.

    • Set Sending Method to Console.

      1. In the Message Key text box, enter the key of the message. For example, demo.

      2. In the Message Content text box, enter the content of the test message. For example, {"key": "test"}.

      3. 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.

      4. Follow the on-screen instructions to subscribe to messages using an SDK or by running Docker commands.

    • Set Sending Method to Docker and run the Docker container.

      1. Run the Docker command in the Run the Docker container to produce a sample message section to send the message.

      2. Run the Docker command in the How do I consume a message after the message is sent? section to subscribe to the message.

    • Set Sending Method to SDK. Select an SDK for the required language or framework and a connection type to send and receive messages.

  7. In the left-side navigation pane, click Message Query. On the Message Query page, select a query method, topic, and partition. Then, click Search to view the data in the topic.

    For more information about querying messages, see Message Query. The following shows a partial result from a query by time. The result shows multiple message records. The keys are all demo and the values are all {"key": "test"}. This indicates that you have successfully written the data to the topic.

Step 2: Create a table in DataWorks

Create a DataWorks table to ensure that MaxCompute can receive data from the ApsaraMQ for Kafka instance. For testing purposes, this tutorial uses a non-partitioned table.

  1. Go to Data Studio

    1. Log in to the DataWorks console and select a region in the upper-left corner.

    2. In the navigation pane on the left, choose Workspace.

    3. On the Workspaces page, in the Actions column of the target workspace, choose Shortcuts > DataStudio.

  2. On the Data Studio page, create a new MaxCompute SQL node. Use the following statement to create the testkafka table.

    CREATE TABLE testkafka 
    (
     key             STRING,
     value           STRING,
     partition1      STRING,
     timestamp1      STRING,
     offset          STRING,
     t123            STRING
    ) ;

    The columns in this table are designed to store data from the Kafka source. The Kafka Reader in DataWorks Data Integration provides the following default source columns:

    • key indicates the message key.

    • value indicates the full content of the message.

    • partition indicates the partition where the message resides.

    • headers indicates the header information of the message.

    • offset indicates the offset of the message.

    • timestamp indicates the timestamp of the message.

    You can also use custom names. For more information, see Kafka Reader.

For more information, see Manage tables.

Step 3: Add data sources

Add your ApsaraMQ for Kafka instance as the source in DataWorks, and MaxCompute as the destination.

  1. Create an exclusive resource group for Data Integration.

    The public resource group in DataWorks does not fully support the Kafka plugin. You must use an exclusive resource group for Data Integration to synchronize data. For more information, see Use an exclusive resource group for Data Integration.

  2. Log in to the DataWorks console and select a region in the upper-left corner.

  3. In the navigation pane on the left, choose Workspace.

  4. On the Workspaces page, click the name of the target workspace.

  5. On the Workspace Details page, click Data Sources in the navigation pane on the left.

  6. Add a Kafka data source.

    On the Data Source tab, click Add Connection, and select Kafka.

    On the Add Kafka Data Source page, fill in the following information.

    • Data Source Type: Select Alibaba Cloud Instance Mode

    • Data Source Name: Enter the name of the new data source.

    • Region: Select the appropriate region.

    • Instance ID: The ID of the instance created in the ApsaraMQ for Kafka console.

    • Special Authentication Method: Keep the default.

    • In the Data Integration resource group list, bind the target resource group, and then click Batch Test Network Connectivity in the row of the target resource group. After the test succeeds, click OK.

      For more information, see Configure a data source.

  7. Add a MaxCompute data source.

    1. On the Data Source tab, click Add Connection, and then select MaxCompute.

    2. On the Add MaxCompute Data Source page, fill in the Basic Information.

Step 4: Synchronize data

  1. Log in to the DataWorks console and select a region in the upper-left corner.

  2. In the navigation pane on the left, choose Data Development and O&M > Data Development.

  3. Select Workspace, and click Go to Data Studio.

  4. On the left side of DataStudio, click image, and select Create Node > Data Integration > Batch Synchronization.

    • For Source, select Kafka.

    • For Destination, select MaxCompute (ODPS).

  5. On the node configuration page, configure the following parameters.

    • Data Source-Source: Select the name of the newly added Kafka data source.

    • Data Source-Destination: Select the name of the new MaxCompute data source.

    • Data Source-Topic: Select the topic to which you want to write data.

    • Destination-Table: The created MaxCompute table.

    • Runtime Resource: Select the exclusive resource group that you created in the first step of Step 3: Add a data source.

    • Keep the default values for other parameters.

    You can also click the 切换代码 icon at the top of the configuration section to switch to script mode and configure the node using a script. The following code provides an example:

    {
        "type": "job",
        "version": "2.0",
        "steps": [
            {
                "stepType": "kafka",
                "parameter": {
                    "server": "localhost:9093",
                    "fetchMaxWaitMs": "500",
                    "kafkaConfig": {
                        "group.id": "datax_consumer_group"
                    },
                    "endType": "specific",
                    "column": [
                        "key",
                        "value",
                        "partition",
                        "headers",
                        "offset",
                        "timestamp"
                    ],
                    "timeZone": "Asia/Shanghai",
                    "fetchMinBytes": "1",
                    "endDateTime": "${endDateTime}",
                    "encoding": "UTF-8",
                    "version": "10",
                    "stopWhenPollEmpty": "false",
                    "beginType": "specific",
                    "autoOffsetReset": "none",
                    "envType": 0,
                    "datasource": "kafka_001",
                    "valueType": "string",
                    "topic": "topic_c",
                    "beginDateTime": "${beginDateTime}",
                    "keyType": "string",
                    "sessionTimeoutMs": "30000",
                    "waitTime": "10"
                },
                "name": "Reader",
                "category": "reader"
            },
            {
                "stepType": "odps",
                "parameter": {
                    "partition": "",
                    "truncate": true,
                    "datasource": "odps_001",
                    "envType": 0,
                    "column": [
                        "key",
                        "value",
                        "partition1",
                        "timestamp1",
                        "offset",
                        "t123"
                    ],
                    "emptyAsNull": false,
                    "table": "testkafka"
                },
                "name": "Writer",
                "category": "writer"
            }
        ],
        "setting": {
            "executeMode": null,
            "errorLimit": {
                "record": ""
            },
            "speed": {
                "concurrent": 2,
                "throttle": false
            }
        },
        "order": {
            "hops": [
                {
                    "from": "Reader",
                    "to": "Writer"
                }
            ]
        }
    }
  6. Click Run.

Next steps

Create a data development task to run an SQL statement and check the current table for data synchronized from ApsaraMQ for Kafka. This topic uses select * from testkafka as an example. The procedure is as follows:

  1. Log in to the DataWorks console and select a region in the upper-left corner.

  2. In the navigation pane on the left, choose Workspace.

  3. On the Workspaces page, in the Actions column of the target workspace, choose Shortcuts > DataStudio.

  4. Create a new MaxCompute SQL node and enter SELECT * FROM testkafka.

    On the Run Configuration page, set Computing Resource to the bound MaxCompute project resource and Computing Quota to the interactive configuration group.

    Click Run to view the results.