Flow 102
NB: this post was updated 8th Jan 2018 to account for changed image names and invocation being “hot” by default.
If you haven’t read Flow 101 yet, I encourage you to start there and get grounded in what Flow is, what it’s for and how it works.
In this post I’ll go through how to build a more complex Flow with parallelism and asynchronous chaining. I’ll assume you have set up the services as described in the Flow 101.
The demo Flow
An app which:
- reads some text
- greps for a given keyword
- counts the matching lines
- prints the count
- prints the file header
In your shell, it might look something like:
Installing helper functions
One of the cool things about Fn is that because it’s based on Docker, functions can be written in any language — even Bash!
Clone this repo of simple Bash functions and deploy them all:
You can test all of these individually, for example:
Creating our Flow function
In a new directory called word-flow
:
And, make HelloFunction.java
look like this:
It’s worth reading this code carefully, remembering that anything returning a FlowFuture
object is an asynchronous call, which can be chained with thenApply
, thenCompose
and the other Flow API methods.
We’ll want some test data:
Deploy the function, and remember to configure the app with the location of the Flow Server (aka “completer”):
And… send in the Shakespeare:
Visualising the Flow
Check the UI on http://localhost:3002 and you should see something like this:
As you could see from the code above, the head
and grep
are executed in parallel, the linecount
has to wait for the grep
, and the main
has to wait till everything else is finished.
Further reading
For a more thorough treatment of the different operations you can use to create Flows, see the Fn Flow User Guide. If you’re at the top of the class, you can have a look at the Flow — Advanced Topics page. And a real example can be found in the Asynchronous Thumbnails project.
Finally, there is an explanation of testing Fn Java Functions and Flows
Any questions or comments? There is #fn-flow on the FnProject slack, and our github. Or hit me up on Twitter as @MaximumGilliard.
Originally published at mjg123.github.io.