Apache Flink streaming examples demonstrating various stream processing patterns and use cases
npx @tessl/cli install tessl/maven-org-apache-flink--flink-examples-streaming-2-10@1.3.0Apache Flink streaming examples demonstrating various stream processing patterns and use cases. This collection provides comprehensive reference implementations showcasing Flink's DataStream API capabilities, real-time stream processing patterns, and integration with external systems.
org.apache.flink:flink-examples-streaming_2.10:1.3.3<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-examples-streaming_2.10</artifactId>
<version>1.3.3</version>
</dependency>These examples are standalone executable programs that demonstrate various streaming patterns:
# Run examples using Flink CLI
$FLINK_HOME/bin/flink run WordCount.jar --input input.txt --output output.txt
# Or execute directly with Java
java -cp flink-examples-streaming_2.10-1.3.3.jar \
org.apache.flink.streaming.examples.wordcount.WordCount \
--input input.txt --output output.txtThe examples are organized into functional categories, each demonstrating specific aspects of Flink streaming:
Each example is self-contained with configurable parameters and includes both Java and Scala implementations where applicable.
Basic streaming word count implementations demonstrating fundamental DataStream operations and tuple-based processing.
// Main executable classes
public class WordCount {
public static void main(String[] args) throws Exception;
public static final class Tokenizer
implements FlatMapFunction<String, Tuple2<String, Integer>> {
public void flatMap(String value, Collector<Tuple2<String, Integer>> out)
throws Exception;
}
}
public class PojoExample {
public static void main(String[] args) throws Exception;
}Real-time data processing from socket connections with windowing and aggregation operations.
public class SocketWindowWordCount {
public static void main(String[] args) throws Exception;
public static class WordWithCount {
public String word;
public long count;
public WordWithCount();
public WordWithCount(String word, long count);
public String toString();
}
}Advanced windowing patterns including time windows, session windows, and custom triggers with event-time processing.
public class TopSpeedWindowing {
public static void main(String[] args) throws Exception;
}
public class SessionWindowing {
public static void main(String[] args) throws Exception;
}
public class WindowWordCount {
public static void main(String[] args) throws Exception;
}Advanced stream processing with conditional routing using ProcessFunction and OutputTag for stream splitting.
public class SideOutputExample {
static final OutputTag<String> rejectedWordsTag = new OutputTag<String>("rejected") {};
public static void main(String[] args) throws Exception;
public static final class Tokenizer extends ProcessFunction<String, Tuple2<String, Integer>> {
public void processElement(String value, Context ctx, Collector<Tuple2<String, Integer>> out)
throws Exception;
}
}Non-blocking external system integration with configurable parallelism and error handling.
public class AsyncIOExample {
public static void main(String[] args) throws Exception;
}Streaming iterations with feedback loops and convergence criteria for iterative algorithms.
public class IterateExample {
public static void main(String[] args) throws Exception;
public static class InputMap
implements MapFunction<Tuple2<Integer, Integer>,
Tuple5<Integer, Integer, Integer, Integer, Integer>>;
public static class Step
implements MapFunction<Tuple5<Integer, Integer, Integer, Integer, Integer>,
Tuple5<Integer, Integer, Integer, Integer, Integer>>;
public static class MySelector
implements OutputSelector<Tuple5<Integer, Integer, Integer, Integer, Integer>>;
}Time-based stream joins with coordinated watermarks and window-based join operations.
public class WindowJoin {
public static void main(String[] args) throws Exception;
}Integration examples for Kafka, Twitter, and other external data sources with fault-tolerant connectors.
public class ReadFromKafka {
public static void main(String[] args) throws Exception;
}
public class WriteIntoKafka {
public static void main(String[] args) throws Exception;
}
public class TwitterExample {
public static void main(String[] args) throws Exception;
}Incremental learning patterns and online algorithm implementations for streaming ML workflows.
public class IncrementalLearningSkeleton {
public static void main(String[] args) throws Exception;
}Shared utility classes and data generators used across multiple examples.
public class ThrottledIterator<T> implements Iterator<T>, Serializable {
public ThrottledIterator(Iterator<T> source, long elementsPerSecond);
public boolean hasNext();
public T next();
public void remove(); // throws UnsupportedOperationException
}// Flink core types used throughout examples
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.utils.ParameterTool;
import org.apache.flink.util.Collector;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.OutputTag;
// Common data structures
class Tuple2<T0, T1> {
public T0 f0;
public T1 f1;
public Tuple2();
public Tuple2(T0 f0, T1 f1);
}
class Tuple4<T0, T1, T2, T3> {
public T0 f0;
public T1 f1;
public T2 f2;
public T3 f3;
public Tuple4();
public Tuple4(T0 f0, T1 f1, T2 f2, T3 f3);
}
class Tuple5<T0, T1, T2, T3, T4> {
public T0 f0;
public T1 f1;
public T2 f2;
public T3 f3;
public T4 f4;
public Tuple5();
public Tuple5(T0 f0, T1 f1, T2 f2, T3 f3, T4 f4);
}
// Advanced stream processing types
abstract class ProcessFunction<I, O> extends AbstractRichFunction {
public abstract void processElement(I value, Context ctx, Collector<O> out) throws Exception;
public void onTimer(long timestamp, OnTimerContext ctx, Collector<O> out) throws Exception;
public abstract class Context {
public abstract <X> void output(OutputTag<X> outputTag, X value);
public abstract long timestamp();
public abstract long currentWatermark();
}
}
class OutputTag<T> {
public OutputTag(String id);
}