You can begin by creating a Python project and installing the PyFlink package following the installation guide.
Writing a Flink Python Table API Program
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.
The table mySource has only one column, word, and it consumes strings read from file /tmp/input.
The table mySink has two columns, word and count, and writes data to the file /tmp/output, with \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 mySink.
Finally you must execute the actual Flink Python Table API job.
All operations, such as creating sources, transformations and sinks are lazy.
Only when t_env.execute(job_name) is called will the job be run.
The complete code so far:
Executing a Flink Python Table API Program
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 Table API Docs for more details.