Skip to main content

Introduction

Apache Flink is a renowned stream processing engine widely used in event-driven, stream-batch analysis scenarios. AutoMQ is a highly elastic cloud-native Kafka that brings over 10x cost reduction and elasticity benefits through cloud-native modifications to the Kafka storage layer. Thanks to AutoMQ’s 100% compatibility with Kafka, it can easily leverage existing Kafka ecosystem tools to read and write with Flink. This article will demonstrate how Flink can read data from an AutoMQ Topic, perform data analysis, and then write the results back to AutoMQ using a WordCount example.

Environment Setup

This document uses Flink version v1.19.0. Follow the official documentation Flink First Step to deploy a v1.19.0 Flink service.

Install and Launch AutoMQ

Refer to the Deploy Multi-Nodes Cluster on Linux▸ documentation to deploy an AutoMQ cluster. In this example, the AutoMQ version used is v1.0.4.

Prepare Test Topic and Data

Create a topic to-flink to store data that needs to be imported into Flink for analysis and computation.
Use the command-line tool to write a batch of data for word count computation.
The data to be written is as follows, and you can exit the producer by pressing Ctrl+C after finishing the input.
Finally, we hope that the result obtained through Flink computation will be.
After writing is complete, we can try to consume the data to confirm it was successfully written.
Create a topic to receive the results of Flink computations.
Thanks to AutoMQ’s full compatibility with Kafka, we can directly use the Kafka Connector provided by Flink to write the source and sink code to load data from AutoMQ’s Topic.

POM Dependencies

The following Java code defines an AutoMQ source and sink using KafkaSource and KafkaSink, respectively. It first reads the pre-prepared “fruit list” test data from the topic to-flink. Then, it creates a DataStream to perform the WordCount computation and sinks the result into the AutoMQ topic from-flink.
The following code, after being built using mvn, will generate an automq-wordcount-flink-job.jar, which is the job we need to submit to Flink. Execute the following command to submit the task jar to Flink. Through the console, we can see that 15 pieces of data have been received and processed.

Verify the Analysis Results

Use the Kafka bin tools extracted from AutoMQ to consume data from from-flink and check the results:
You can see the output results below. Since it’s processed in a stream and there is no watermark or window calculation set, the word count result is printed out every time a calculation is performed.
Next, we write 5 more data entries to the to-flink Topic and observe the stream processing results:
The data written is
Then we can see that the from-flink Topic correctly outputs the following word count results
We can also see on the console that 20 data entries were correctly received and processed:

Summary

This article demonstrates how AutoMQ integrates with Flink to complete a Word Count analysis workflow. For more configurations and usage of the Kafka Connector, refer to the official Flink documentation Apache Kafka Connector.