Platform Engineering Team/Event Platform Value Stream/Build simple stateless service using PyFlink

This page summarizes the learnings of https://phabricator.wikimedia.org/T318859

User Story
As a platform engineer, I need to try and build a simple stateless service that takes an input stream, transforms, enriches and produces an output using PyFlink

The service should:


 * Listen to  or another existing Kafka topic
 * Make a call to MW Action API
 * Produce some output that combines the data

Is this good abstraction for event driven data producers to create similar services easily?

TL;DR

 * PyFlink by itself is in some areas more burdensome to use than regular Flink
 * Because PyFlink is just a thin wrapper for Flink, if we wanted to use our existing codebase it's basically writing Java without type hints and with the added overhead of needing to convert Java types into Python types
 * However, if we make wrappers for our existing codebase it becomes much more bearable... for the users. *If* we make wrappers
 * The one major advantage is its ability to easily implement UDFs that can be used in both PyFlink and Flink SQL

Pros

 * Python is more familiar to developers
 * Easier to get started; just install pyflink
 * Development is easier. No need to rebuild jars. No need to submit jobs to a cluster just to test it out
 * Can interop with our existing Java codebase
 * Supports Pandas

Cons

 * Need to know Flink
 * There is not a Python equivalent for every Java function, and it's unclear if the missing items are intentional or if it's still in development
 * Immediately gets more complicated the second you want to interop with Java
 * No type hints
 * Need to convert between Java/Python types

If we want the easiest developer experience, it might involve making a library of custom UDFs and Flink SQL connectors so people don't have to touch Flink at all.

Datastream API
Developers can define UDFs by extending one of PyFlink's  classes. Developers can also use third-party Python libraries within their UDFs, but must specify the dependencies when executing the jobs on a remote cluster.

Here's an example of a map function in PyFlink that takes a  and returns the images on that page:

Sample Output:

Full Example

Table API
The table api allows you to create UDFs which can mimic the datastream UDFs. However, the return type has to be one of  since it integrates with SQL.

Here's an example of a UDF that does the equivalent of the datastream example:

Sample Output:

Full Example

Flink SQL + Python UDF
You can pull the UDFs created for the table api and load it into Flink SQL. More experimenting is needed to find its limitations.

Here's an example that uses the UDF from the above example:

WIP Example