Apache Flink offers a Table API as a unified, relational API for batch and stream processing, i.e., queries are executed with the same semantics on unbounded, real-time streams or bounded, batch data sets and produce the same results. The Table API in Flink is commonly used to ease the definition of data analytics, data pipelining, and ETL applications.
In this tutorial, you will learn how to build a pure Python Flink Table API project. The pipeline will read data from an input csv file and write the results to an output csv file.
This walkthrough assumes that you have some familiarity with Python, but you should be able to follow along even if you come from a different programming language.
It also assumes that you are familiar with basic relational concepts such as
GROUP BY clauses.
If you get stuck, check out the community support resources. In particular, Apache Flink’s user mailing list consistently ranks as one of the most active of any Apache project and a great way to get help quickly.
If you want to follow along, you will require a computer with:
Using Python Table API requires installing PyFlink, which is available on PyPI and can be easily installed using
Once PyFlink is installed, you can move on to write a Python Table API job.
Table API applications begin by declaring a table environment; either a
BatchTableEvironment for batch applications or
StreamTableEnvironment for streaming applications.
This serves as the main entry point for interacting with the Flink runtime.
It can be used for setting execution parameters such as restart strategy, default parallelism, etc.
The table config allows setting Table API specific configurations.
The the table environment created, you can declare source and sink tables.
You can also use the TableEnvironment.sql_update() method to register a source/sink table defined in DDL:
This registers a table named
mySource and a table named
mySink in the execution environment.
mySource has only one column, word, and it consumes strings read from file
mySink has two columns, word and count, and writes data to the file
\t as the field delimiter.
You can now create a job which reads input from table
mySource, preforms some transformations, and writes the results to table
Finally you must execute the actual Flink Python Table API job.
All operations, such as creating sources, transformations and sinks are lazy.
t_env.execute(job_name) is called will the job be run.
The complete code so far:
Firstly, you need to prepare input data in the “/tmp/input” file. You can choose the following command line to prepare the input data:
Next, you can run this example on the command line (Note: if the result file “/tmp/output” has already existed, you need to remove the file before running the example):
The command builds and runs the Python Table API program in a local mini cluster. You can also submit the Python Table API program to a remote cluster, you can refer Job Submission Examples for more details.
Finally, you can see the execution result on the command line:
This should get you started with writing your own Flink Python Table API programs. To learn more about the Python Table API, you can refer Flink Python API Docs for more details.