Home About Me

Writing Data from Flink into Kafka

Introduction

Flink already comes with a number of built-in sinks, and Kafka is one of them through the FlinkKafkaProducer connector. In this example, we’ll look at how to write data into Kafka from a Flink job.

Setup

Flink supports Kafka 0.8, 0.9, 0.10, and 0.11.

Here we need to install Kafka and add the matching Flink Kafka connector dependency. In this walkthrough, Kafka 0.11 is used:

<dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-connector-kafka-0.11_${scala.binary.version}</artifactId>
            <version>${flink.version}</version>
</dependency>

Before running the job, check whether the local Kafka cluster already has the student-write topic:

./kafka-topics.sh --list --zookeeper localhost:2181

Example output:

Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=on -Dswing.aatext=true
__consumer_offsets
metric
student

If student-write appears after the program starts, that means the job is working and data has been written into the local Kafka cluster from another Kafka cluster.

Code

package com.thinker.kafka;

import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.*;

import java.util.Properties;

/**
 * @author zeekling [[email protected]]
 * @version 1.0
 * @apiNote
 * @since 2020-05-14
 */
public class FlinkSinkToKafka {

    private static final String READ_TOPIC = "student-write";

    public static void main(String[] args) throws Exception {
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        Properties props = new Properties();
        props.put("bootstrap.servers", "localhost:9092");
        props.put("zookeeper.connect", "localhost:2181");
        props.put("group.id", "student-group");
        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("auto.offset.reset", "latest");
        DataStreamSource<String> student = env.addSource(new FlinkKafkaConsumer011<>(
                READ_TOPIC,   //这个 kafka topic 需要和上面的工具类的 topic 一致
                new SimpleStringSchema(),
                props)).setParallelism(1);
        student.print();
        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "localhost:9092");
        properties.setProperty("zookeeper.connect", "localhost:2181");
        properties.setProperty("group.id", "student-write");

        student.addSink(new FlinkKafkaProducer011<String>(
                "student-write",
                new SimpleStringSchema(),
                properties
        )).name("flink-connectors-kafka").setParallelism(1);
        student.print();
        env.execute("flink learning connectors kafka");
    }

}

Running the job

Copy the jars listed below into the corresponding Flink directory, then restart Flink.

Flink Kafka connector jars

Submit the Flink job with the following command:

./bin/flink run -c com.thinker.kafka.FlinkSinkToKafka ~/project/flink-test/target/flink-test-1.0-SNAPSHOT.jar

After the job is submitted successfully, run:

/kafka-topics.sh --list --zookeeper localhost:2181

You should then see the result in the topic list:

Topic list after the job runs