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
Install and Launch Flink
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 topicto-flink
to store data that needs to be imported into Flink for analysis and computation.
Read Data from AutoMQ into Flink for Analysis.
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
Writing the Flink Job Code
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.Submitting the Job 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 fromfrom-flink
and check the results:
to-flink
Topic and observe the stream processing results:
from-flink
Topic correctly outputs the following word count results
