Here's the step by step guideline for stream processing with Apache Kafka and Flink using Amazon MSK and Amazon Kinesis Data Analytics (KDA).
- Create an EC2 Windows instance on a VPC where you will create your Kafka cluster using Amazon MSK. Create an IAM role for EC2 (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html). For test purpose, you can assign AdministratorAccess policy on that role. Select the role when you are creating the EC2 instance. You will use this role to create the Kafka cluster and also to configure the Kinesis Data Analytics application.
- Configure below tools on your newly created instance
a) Install AWS CLI and configure aws region (ap-southeast-2).
b) Install git
c) Download and install IntelliJ community edition: https://www.jetbrains.com/idea/
d) Download and configure Apache Maven (it's important to environment variable) : https://maven.apache.org/install.html
You can follow the MSK getting started guideline here: https://docs.aws.amazon.com/msk/latest/developerguide/getting-started.html. In the below example I am using default VPC with 3 existing subnets. For security group, create a security group manually and for Inbound rules assign the same security group name as a source for "all traffic". After you finish creating the security group, attach it with the existing EC2 instance that you have created earlier. To create a cluster run the below AWS CLI command after changing the subnet and security group name in the CreateMSK.json file.
aws kafka create-cluster --cli-input-json file://CreateMSK.json
- Clone the msk_kda_example repository on your local Windows machine
- Open KDA-kafka-to-kafka-app using IntelliJ and review the code. You don't need to change anything for this demo.
- Open command prompt and change your directory path to KDF-kafka-to-kafka-app.
- Example
C:\Users\mypath\Documents\msk_kda_example\KDA-kafka-to-kafka-app
. - Run
mvn package -Dflink.version=1.8.2
. - The command should return BUILD SUCCESS info.
- If the build is successful you will see a jar file in the target folder - \msk_kda_example\KDF-kafka-to-kafka-app\target\kdfKafkaToKafka-1.2.jar.
- Go to your AWS management console and create a S3 bucket and a subfolder on that bucket.
- Upload the kdfKafkaToKafka-1.2.jar to your S3 bucket.
- Create an IAM role for Kinesis Data Analytics (KDA) application. For test purpose, you can assign AdministratorAccess policy on that role.
- Open CreateKDAKafkaToKafkaApp.json and change below configurations.
- ServiceExecutionRole --> specify the role arn that you have created for your KDA application.
- BucketARN --> Change the S3 bucket name.
- FileKey --> Change the path where you upload the kdfKafkaToKafka-1.2.jar file.
- bootstrap.servers --> From AWS management console go to MSK and click on the Kafka cluster that you have created earlier. Click on "View client information" and copy the TSL bootstrap server information. Replace bootstrap.servers setting with that copied information for KafkaSource and KafkaSink PropertyGroupId.
- Change the SecurityGroupIds and SubnetIds as similar to your MSK cluster.
- Run below AWS CLI command to create the KDA APP.
aws kinesisanalyticsv2 create-application --cli-input-json file://CreateKDAKafkaToKafkaApp.json
- If will create a KDA application called meetupDemo1.
- Open StartKDAKafkaToKafkaApp.json and change below configurations.
- ApplicationName --> meetupDemo1.
- Run below AWS CLI command to start the KDA App.
aws kinesisanalyticsv2 start-application --cli-input-json file://StartKDAKafkaToKafkaApp.json
- Go to AWS Kinesis data analytics console and monitor whether your KDA app has started.
- Click on Application Details. If the application is running successfully, You will see an Application graph with a visual representation of the data flow consisting of operators and intermediate results of your Flink app.
- Working with Kafka from a Linux machine is easier than Windows. For that, create an EC2 instance with Amazon Linux AMI.
- For the test, assign the same security group that you have created earlier for your Kafka cluster.
- Install Java -
sudo yum install java-1.8.0
- Download Apache kafka -
wget https://archive.apache.org/dist/kafka/2.2.1/kafka_2.12-2.2.1.tgz
- Run this command -
tar -xzf kafka_2.12-2.2.1.tgz
- Go to the kafka_2.12-2.2.1 directory
- From AWS management console go to MSK and click on the Kafka cluster that you have created earlier. Click on "View client information". Copy the Zookeeper configuration.
- run the following command on your EC2 Linux instance. Replace with your cluster Zookeeper configuration.
export ZOOK="ZookeeperConnectString"
- Go to your Kafka directory and run the following command
bin/kafka-topics.sh --create --zookeeper $ZOOK --replication-factor 3 --partitions 1 --topic sourcetopic
bin/kafka-topics.sh --create --zookeeper $ZOOK --replication-factor 3 --partitions 1 --topic destinationtopic
- run the following command to see the list of topics on your cluster. It will return three topics name on a newly created cluster.
bin/kafka-topics.sh --list --zookeeper $ZOOK
- From AWS management console go to MSK and click on the Kafka cluster that you have created earlier. Click on "View client information". Copy the Plaintext bootstrap server information.
- run the following command
export BOOTSERVER="Replace with Plaintext Bootstrap servers configuration"
- From your EC2 Linux instance run the following command. It will return an empty result, but keep this running for the time being.
bin/kafka-console-consumer.sh --bootstrap-server $BOOTSERVER --topic destinationtopic --from-beginning
- Switch back to your Windows machine.
- Open kafkaDG project with IntelliJ that you have cloned earlier from git repo.
- Open the testProducer.java file. Change the broker list with your MSK clusters' Plaintext broker configuration.
- Run the java application.
- You will see that your java app is sending data to your sourcetopic and you will also see your EC2 consumer running on Linux started reading data from the destinationtopic topic. Here the KDA app is replicating data from your sourcetopic to the destinationtopic.
- To view the activities of your flink application, go to the KDA console, and monitor your KDA application.