Source, operator and sink in DataStream API

A DataStream represents the data records and the operators. There are pre-implemented sources and sinks for Flink, and you can also use custom defined connectors to maintain the dataflow with other functions.

DataStream<String> source = env
                "Kafka Source")
        .map(record -> record.getId()
                + "," + record.getName()
                + "," + record.getDescription())
        .name("To Output String")

FileSink<String> sink = FileSink
                new Path(params.getRequired(K_HDFS_OUTPUT)),
                new SimpleStringEncoder<String>("UTF-8"))

		.name("FS Sink")
source = env.from_source(source=kafka_source,
                         source_name='Kafka Source')
source = record:
                    record.get_id() + ',' +
                    record.get_name() + ',' +
source ='To Output String').uid('to-output-string')

sink = FileSink.for_row_format(

source.sink_to(sink).name('FS Sink').uid('fs-sink')
Choosing the sources and sinks depends on the purpo se of the application. As Flink can be implemented in any kind of an environment, various connectors are available. In most cases, Kafka is used as a connector as it has streaming capabilities and can be easily integrated with other services.
Sources are where your program reads its input from. You can attach a source to your program by using StreamExecutionEnvironment.addSource(sourceFunction). Flink comes with a number of pre-implemented source functions. For the list of sources, see the Apache Flink documentation.
Streaming Analytics in Cloudera supports the following sources:
  • HDFS
  • Kafka
Operators transform one or more DataStreams into a new DataStream. When choosing the operator, you need to decide what type of transformation you need on your data. The following are some basic transformation:
  • Map
    Takes one element and produces one element. MapFunction<Integer, Integer>() {
        public Integer map(Integer value) throws Exception {
            return 2 * value;
    }); value: 2 * value, output_type=Types.INT())
  • FlatMap
    Takes one element and produces zero, one, or more elements.
    dataStream.flatMap(new FlatMapFunction<String, String>() {
        public void flatMap(String value, Collector<String> out)
            throws Exception {
            for (String word: value.split(" ")) {
    data_stream.flat_map(lambda value: value.split(' '), output_type=Types.STRING())
  • Filter
    Evaluates a boolean function for each element and retains those for which the function returns true.
    dataStream.filter(new FilterFunction<Integer>() {
        public boolean filter(Integer value) throws Exception {
            return value != 0;
    data_stream.filter(lambda value: value != 0)
  • KeyBy
    Logically partitions a stream into disjoint partitions. All records with the same key are assigned to the same partition. This transformation returns a KeyedStream
    dataStream.keyBy(value -> value.getSomeKey());
    dataStream.keyBy(value -> value.f0);
    data_stream.key_by(lambda value: value.get_some_key())
    data_stream.key_by(lambda value: value[0], key_type=Types.STRING())
  • Window
    Windows can be defined on already partitioned KeyedStreams. Windows group the data in each key according to some characteristic (for example, the data that arrived within the last 5 seconds).
        .keyBy(value -> value.f0)
    data_stream.key_by(lambda value: value[0], key_type=Types.STRING()) \

For the full list of operators, see the Apache Flink documentation.

Data sinks consume DataStreams and forward them to files, sockets, external systems, or print them. Flink comes with a variety of built-in output formats that are encapsulated behind operations on the DataStreams. For the list of sources, see the Apache Flink documentation.
Streaming Analytics in Cloudera supports the following sinks:
  • Kafka
  • HBase
  • Kudu
  • HDFS