First iteration of Java Stream API.
Java Stream API is following the popular functional programming paradigm to construct an Apex Application.
The goal for this API is:
- Easy to construct a dag
- Easy to migrate other streaming application to Apex
- Fully compatible with existing DAG API
- Provide useful build-in transformations with abstracted pluggable components in one place
To achieve the goal and split the work, we categorize all different kind of transformations into 2 different types:
- 1 input, 1+ output (map, filter, flatmap);
- Multiple input, 1 output (Aggregations, Joins, Unions)
This first iteration is only about the first category, which is, 1 in, 1+ out. For transformations like this, it is just like distributed function call. So we abstract out some function types instead of operators. Internally, there are some pre-build function operators which wrap the function and connect together.
The core interface is the ApexStream. The ApexStream is designed in a method chain fashion, which all transformation method returns a new ApexStream object with new output type.
Here are some examples, if you want to do a filter then a map, you can do
You can also mix this with existing operator API. For example, if you want to add a operator after map, you can do this
.addOperator(opt, opt.input, opt.output)
If you want to set the locality or attributes for operator/ports/dag, you can use *with* clause, for example you want filter and map to be container local and you want to set checkpoint window count for the new operator you just added, you can do something like this
Like the dag API, you can run the stream in a distributed mode or local mode, For example,
The stream is implemented in a lazy build mode, which means until you call populateDag or runEmbedded, all the transformations and the order of them will be kept in memory in a graph data structure (DagMeta). This will allow us to solve some technical difficulties such as logical plan optimization etc.
Also the stream is flexible to extend to fit you needs in your organization. For example if you want to provide a filter and map transformation in one operator. Instead of repeating the work of connect filter and map operator together in a thread_local mode. You can add first-order function to ApexStream interface, by simply extending the default implementation ApexStreamImpl
public class MyStream<T> extends ApexStreamImpl<T>
public MyStream(ApexStream<T> apexStream)
<O> MyStream<O> myFilterAndMap(Function.MapFunction<T, O> map, Function.FilterFunction<T> filterFunction)
Then you can use your new Stream like this
new MyStream(stream).<..,MyStream>.flatMap(...) .*myFilterAndMap(...)* .print()