Intro to the DataStream API

The focus of this training is to broadly cover the DataStream API well enough that you will be able to get started writing streaming applications.

What can be Streamed?

Flink’s DataStream APIs will let you stream anything they can serialize. Flink’s own serializer is used for

  • basic types, i.e., String, Long, Integer, Boolean, Array
  • composite types: Tuples, POJOs, and Scala case classes

and Flink falls back to Kryo for other types. It is also possible to use other serializers with Flink. Avro, in particular, is well supported.

Java tuples and POJOs

Flink’s native serializer can operate efficiently on tuples and POJOs.

Tuples

For Java, Flink defines its own Tuple0 thru Tuple25 types.

  1. Tuple2<String, Integer> person = Tuple2.of("Fred", 35);
  2. // zero based index!
  3. String name = person.f0;
  4. Integer age = person.f1;

POJOs

Flink recognizes a data type as a POJO type (and allows “by-name” field referencing) if the following conditions are fulfilled:

  • The class is public and standalone (no non-static inner class)
  • The class has a public no-argument constructor
  • All non-static, non-transient fields in the class (and all superclasses) are either public (and non-final) or have public getter- and setter- methods that follow the Java beans naming conventions for getters and setters.

Example:

  1. public class Person {
  2. public String name;
  3. public Integer age;
  4. public Person() {}
  5. public Person(String name, Integer age) {
  6. . . .
  7. }
  8. }
  9. Person person = new Person("Fred Flintstone", 35);

Flink’s serializer supports schema evolution for POJO types.

Scala tuples and case classes

These work just as you’d expect.

All Flink Scala APIs are deprecated and will be removed in a future Flink version. You can still build your application in Scala, but you should move to the Java version of either the DataStream and/or Table API.

See FLIP-265 Deprecate and remove Scala API support

A Complete Example

This example takes a stream of records about people as input, and filters it to only include the adults.

  1. import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
  2. import org.apache.flink.streaming.api.datastream.DataStream;
  3. import org.apache.flink.api.common.functions.FilterFunction;
  4. public class Example {
  5. public static void main(String[] args) throws Exception {
  6. final StreamExecutionEnvironment env =
  7. StreamExecutionEnvironment.getExecutionEnvironment();
  8. DataStream<Person> flintstones = env.fromElements(
  9. new Person("Fred", 35),
  10. new Person("Wilma", 35),
  11. new Person("Pebbles", 2));
  12. DataStream<Person> adults = flintstones.filter(new FilterFunction<Person>() {
  13. @Override
  14. public boolean filter(Person person) throws Exception {
  15. return person.age >= 18;
  16. }
  17. });
  18. adults.print();
  19. env.execute();
  20. }
  21. public static class Person {
  22. public String name;
  23. public Integer age;
  24. public Person() {}
  25. public Person(String name, Integer age) {
  26. this.name = name;
  27. this.age = age;
  28. }
  29. public String toString() {
  30. return this.name.toString() + ": age " + this.age.toString();
  31. }
  32. }
  33. }

Stream execution environment

Every Flink application needs an execution environment, env in this example. Streaming applications need to use a StreamExecutionEnvironment.

The DataStream API calls made in your application build a job graph that is attached to the StreamExecutionEnvironment. When env.execute() is called this graph is packaged up and sent to the JobManager, which parallelizes the job and distributes slices of it to the Task Managers for execution. Each parallel slice of your job will be executed in a task slot.

Note that if you don’t call execute(), your application won’t be run.

Flink runtime: client, job manager, task managers

This distributed runtime depends on your application being serializable. It also requires that all dependencies are available to each node in the cluster.

Basic stream sources

The example above constructs a DataStream<Person> using env.fromElements(...). This is a convenient way to throw together a simple stream for use in a prototype or test. There is also a fromCollection(Collection) method on StreamExecutionEnvironment. So instead, you could do this:

  1. List<Person> people = new ArrayList<Person>();
  2. people.add(new Person("Fred", 35));
  3. people.add(new Person("Wilma", 35));
  4. people.add(new Person("Pebbles", 2));
  5. DataStream<Person> flintstones = env.fromCollection(people);

Another convenient way to get some data into a stream while prototyping is to use a socket

  1. DataStream<String> lines = env.socketTextStream("localhost", 9999);

or a file

  1. DataStream<String> lines = env.readTextFile("file:///path");

In real applications the most commonly used data sources are those that support low-latency, high throughput parallel reads in combination with rewind and replay – the prerequisites for high performance and fault tolerance – such as Apache Kafka, Kinesis, and various filesystems. REST APIs and databases are also frequently used for stream enrichment.

Basic stream sinks

The example above uses adults.print() to print its results to the task manager logs (which will appear in your IDE’s console, when running in an IDE). This will call toString() on each element of the stream.

The output looks something like this

  1. 1> Fred: age 35
  2. 2> Wilma: age 35

where 1> and 2> indicate which sub-task (i.e., thread) produced the output.

In production, commonly used sinks include the FileSink, various databases, and several pub-sub systems.

Debugging

In production, your application will run in a remote cluster or set of containers. And if it fails, it will fail remotely. The JobManager and TaskManager logs can be very helpful in debugging such failures, but it is much easier to do local debugging inside an IDE, which is something that Flink supports. You can set breakpoints, examine local variables, and step through your code. You can also step into Flink’s code, which can be a great way to learn more about its internals if you are curious to see how Flink works.

Hands-on

At this point you know enough to get started coding and running a simple DataStream application. Clone the flink-training-repo , and after following the instructions in the README, do the first exercise: Filtering a Stream (Ride Cleansing) .

Further Reading