Side Outputs

In addition to the main stream that results from DataStream operations, you can also produce any number of additional side output result streams. The type of data in the result streams does not have to match the type of data in the main stream and the types of the different side outputs can also differ. This operation can be useful when you want to split a stream of data where you would normally have to replicate the stream and then filter out from each stream the data that you don’t want to have.

When using side outputs, you first need to define an OutputTag that will be used to identify a side output stream:

Java

  1. // this needs to be an anonymous inner class, so that we can analyze the type
  2. OutputTag<String> outputTag = new OutputTag<String>("side-output") {};

Scala

  1. val outputTag = OutputTag[String]("side-output")

Python

  1. output_tag = OutputTag("side-output", Types.STRING())

Notice how the OutputTag is typed according to the type of elements that the side output stream contains.

Emitting data to a side output is possible from the following functions:

You can use the Context parameter, which is exposed to users in the above functions, to emit data to a side output identified by an OutputTag. Here is an example of emitting side output data from a ProcessFunction:

Java

  1. DataStream<Integer> input = ...;
  2. final OutputTag<String> outputTag = new OutputTag<String>("side-output"){};
  3. SingleOutputStreamOperator<Integer> mainDataStream = input
  4. .process(new ProcessFunction<Integer, Integer>() {
  5. @Override
  6. public void processElement(
  7. Integer value,
  8. Context ctx,
  9. Collector<Integer> out) throws Exception {
  10. // emit data to regular output
  11. out.collect(value);
  12. // emit data to side output
  13. ctx.output(outputTag, "sideout-" + String.valueOf(value));
  14. }
  15. });

Scala

  1. val input: DataStream[Int] = ...
  2. val outputTag = OutputTag[String]("side-output")
  3. val mainDataStream = input
  4. .process(new ProcessFunction[Int, Int] {
  5. override def processElement(
  6. value: Int,
  7. ctx: ProcessFunction[Int, Int]#Context,
  8. out: Collector[Int]): Unit = {
  9. // emit data to regular output
  10. out.collect(value)
  11. // emit data to side output
  12. ctx.output(outputTag, "sideout-" + String.valueOf(value))
  13. }
  14. })

Python

  1. input = ... # type: DataStream
  2. output_tag = OutputTag("side-output", Types.STRING())
  3. class MyProcessFunction(ProcessFunction):
  4. def process_element(self, value: int, ctx: ProcessFunction.Context):
  5. # emit data to regular output
  6. yield value
  7. # emit data to side output
  8. yield output_tag, "sideout-" + str(value)
  9. main_data_stream = input \
  10. .process(MyProcessFunction(), Types.INT())

For retrieving the side output stream you use getSideOutput(OutputTag) on the result of the DataStream operation. This will give you a DataStream that is typed to the result of the side output stream:

Java

  1. final OutputTag<String> outputTag = new OutputTag<String>("side-output"){};
  2. SingleOutputStreamOperator<Integer> mainDataStream = ...;
  3. DataStream<String> sideOutputStream = mainDataStream.getSideOutput(outputTag);

Scala

  1. val outputTag = OutputTag[String]("side-output")
  2. val mainDataStream = ...
  3. val sideOutputStream: DataStream[String] = mainDataStream.getSideOutput(outputTag)

Python

  1. output_tag = OutputTag("side-output", Types.STRING())
  2. main_data_stream = ... # type: DataStream
  3. side_output_stream = main_data_stream.get_side_output(output_tag) # type: DataStream