Getting Started with Apache Flink and MapR Event Store

Contributed by

9 min read


Apache Flink is an open source platform for distributed stream and batch data processing. Flink is a streaming data flow engine with several APIs to create a data streams-oriented application. For an overview of Apache Flink, download a free pdf of the book Introduction to Apache Flink: Stream Processing for Real Time and Beyond.

It is very common for Flink applications to use Apache Kafka for data input and output.

This article will provide you with easy steps for using Apache Flink with MapR Event Store. MapR Event Store for Apache Kafka is a distributed messaging system for streaming event data at scale, and it’s integrated into the MapR Data Platform, based on the Apache Kafka API (0.9.0)


The first step is to create a Java application. The easiest way to do this is to use the flink-quickstart-java archetype, which contains the core dependencies and packaging tasks. This article is similar to the Apache Flink Quick Start Example, with a clear focus on data input and output with MapR Event Store.

In this application, we will create two jobs:

  • WriteToKafka : that generates random strings and post them to a MapR Event Store Topic using the Kafka Flink Connector and its Producer API.
  • ReadFromKafka : that reads the same topic and prints the messages in the standard output using the Kafka Flink Connector and its Consumer. API.

The full project is available on GitHub:

Let’s create the project using Apache Maven:

mvn archetype:generate \
      -DarchetypeArtifactId=flink-quickstart-java \
      -DgroupId=com.mapr.demos \
      -DartifactId=mapr-streams-flink-demo \
      -Dversion=1.0-SNAPSHOT \

Maven will create the following structure:

$ tree mapr-streams-flink-demo/
├── pom.xml
└── src
    └── main
        ├── java
        │   └── com
        │       └── mapr
        │           └── demos
        │               ├──
        │               ├──
        │               ├──
        │               └──
        └── resources

This project is configured to create a Jar file that contains your Flink project code and also includes all dependencies needed to run it.

The project contains some other sample jobs. We do not need them for this article, so you can either keep them for educational purposes or simply remove them from the project.

Add Kafka & MapR Event Store Dependencies

Open the pom.xml and add the following dependencies to your project:

1- Add MapR Maven Repository

in the <repositories> element add:


2- Add MapR Event Store libraries

in the <dependencies>:


3- Add Flink Kafka Connector libraries

As a first step, we have to add the Flink Kafka connector as a dependency so that we can use the Kafka sink. Add this to the pom.xml file in the dependencies section:

You must add now the Flink Kafka Connector dependency to use the Kafka sink. Add the following entry in the <dependencies> element:


4- Exclude Kafka Client to allow use of MapR Event Store Client

As you may know, MapR Event Store uses the Kafka 0.9.0 API to produce and consume messages. So we need now to remove (exclude) the Apache Kafka Client API to be sure that Flink can use MapR Event Store.

In the Flink Kafka Connector dependency, add the following exclusion:


The Flink project is now ready to use the DataStream using the Kafka Connector so you can send and receive messages from MapR Event Store.

Let’s now create a Stream in MapR and write some simple Flink code to use it.

Create the MapR Event Store and Topic

A stream is a collection of topics that you can manage as a group by:

  1. Setting security policies that apply to all topics in that stream
  2. Setting a default number of partitions for each new topic that is created in the stream
  3. Set a time-to-live for messages in every topic in the stream

You can find more information about MapR Event Store concepts in the documentation.

On your Mapr Cluster or Sandbox, run the following commands:

$ maprcli stream create -path /apps/application-stream -produceperm p -consumeperm p -topicperm p
$ maprcli stream topic create -path /apps/application-stream -topic flink-demo

Install and use MapR Kafka utilities

Install the mapr-kafka package on your cluster :

yum install mapr-kafka

Open two terminal windows and run the producer and consumer Kafka utilities using the following commands:


/opt/mapr/kafka/kafka-0.9.0/bin/ --broker-list --topic /apps/application-stream:flink-demo=


/opt/mapr/kafka/kafka-0.9.0/bin/ --new-consumer --bootstrap-server --topic /apps/application-stream:flink-demo

In the producer window, you can post some messages and see them in the consumer windows. We will use these tools to follow the interactions between MapR Event Store and Flink.

Let’s now use the Flink Kafka Connector to send messages to MapR Event Store and consume them.


The producer generates messages using the SimpleStringGenerator() class and send the string to the /apps/application-stream:flink-demo topic.

  public static void main(String[] args) throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

    Properties properties = new Properties();
    // properties.setProperty("bootstrap.servers", "<kafka-broker>:9092"); // not used by MapR Event Store
    properties.setProperty("", "200");

    DataStream<String> stream = env.addSource(new SimpleStringGenerator());
    stream.addSink(new FlinkKafkaProducer09<>("/apps/application-stream:flink-demo", new SimpleStringSchema(), properties));


The SimpleStringGenerator() method code is available here.

The main steps are:

  • create a new StreamExecutionEnvironment the basis of any Flink application
  • create a new DataStream in the application environment, the SimpleStringGenerator class implements the [SourceFunction]( the base interface for all streams data sources in Flink.
  • add the FlinkKafkaProducer09 sink to the streams; since MapR Event Store is based on Kafka API 0.9, it is possible to use the FlinkKafkaProducer09 class; with 2 small differences:
    • the broker list (first parameter) is not used since MapR Event Store use the cluster location defined in the /opt/mapr/conf/mapr-clusters.conf class.
    • the topic name include the path and name of the MapR Stream stream in which the topic is located for example /apps/application-stream:flink-demo


The consumer simply reads the messages from the /apps/application-stream:flink-demo topic, and print them into the console.

 public static void main(String[] args) throws Exception {
    // create execution environment
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

    Properties properties = new Properties();
    // properties.setProperty("bootstrap.servers", "<kafka-broker>:9092"); // not used by MapR Event Store
    properties.setProperty("", "flink_consumer");

    DataStream<String> stream = env.addSource(new FlinkKafkaConsumer09<>(
        "/apps/application-stream:flink-demo", new SimpleStringSchema(), properties) ); MapFunction<String, String>() {
      private static final long serialVersionUID = -6867736771747690202L;

      public String map(String value) throws Exception {
        return "Stream Value: " + value;


The main steps are:

  • Create a new StreamExecutionEnvironment, which is the basis of any Flink application.
  • Create a set of properties with the consumer information. In this application, we can only set the consumer Note that the bootstrap.servers property is not used by MapR Event Store, so no need to set it.
  • Use the FlinkKafkaConsumer09 to get the message from the MapR Event Store topic. /apps/application-stream:flink-demo

Build and Run the application

Let’s run the application directly from Maven (or from your favorite IDE).

1- Build the project:

$ mvn clean package

2- Run the Flink Producer Job

$ mvn exec:java -Dexec.mainClass=com.mapr.demos.WriteToKafka

3- Run the Flink Consumer Job

$ mvn exec:java -Dexec.mainClass=com.mapr.demos.ReadFromKafka

In the terminal, you should see the messages generated from the producer

You can now deploy and execute this job on your Flink cluster.


In this article, you have learned how to use Flink with MapR Event Store to write and read data streams. The key element is the configuration of the Maven Dependencies to configure the project to use MapR Event Store libraries instead of Kafka ones.

This blog post was published October 13, 2016.

50,000+ of the smartest have already joined!

Stay ahead of the bleeding edge...get the best of Big Data in your inbox.

Get our latest posts in your inbox

Subscribe Now