Python API Tutorial

This walkthrough will quickly get you started building a pure Python Flink project.

Please refer to the Python Table API installation guide on how to set up the Python execution environments.

Setting up a Python Project

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.

  1. exec_env = ExecutionEnvironment.get_execution_environment()
  2. exec_env.set_parallelism(1)
  3. t_config = TableConfig()
  4. t_env = BatchTableEnvironment.create(exec_env, t_config)

The the table environment created, you can declare source and sink tables.

  1. t_env.connect(FileSystem().path('/tmp/input')) \
  2. .with_format(OldCsv()
  3. .field('word', DataTypes.STRING())) \
  4. .with_schema(Schema()
  5. .field('word', DataTypes.STRING())) \
  6. .create_temporary_table('mySource')
  7. t_env.connect(FileSystem().path('/tmp/output')) \
  8. .with_format(OldCsv()
  9. .field_delimiter('\t')
  10. .field('word', DataTypes.STRING())
  11. .field('count', DataTypes.BIGINT())) \
  12. .with_schema(Schema()
  13. .field('word', DataTypes.STRING())
  14. .field('count', DataTypes.BIGINT())) \
  15. .create_temporary_table('mySink')

You can also use the TableEnvironment.sql_update() method to register a source/sink table defined in DDL:

  1. my_source_ddl = """
  2. create table mySource (
  3. word VARCHAR
  4. ) with (
  5. 'connector.type' = 'filesystem',
  6. 'format.type' = 'csv',
  7. 'connector.path' = '/tmp/input'
  8. )
  9. """
  10. my_sink_ddl = """
  11. create table mySink (
  12. word VARCHAR,
  13. `count` BIGINT
  14. ) with (
  15. 'connector.type' = 'filesystem',
  16. 'format.type' = 'csv',
  17. 'connector.path' = '/tmp/output'
  18. )
  19. """
  20. t_env.sql_update(my_source_ddl)
  21. t_env.sql_update(my_sink_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.

  1. t_env.from_path('mySource') \
  2. .group_by('word') \
  3. .select('word, count(1)') \
  4. .insert_into('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.

  1. t_env.execute("tutorial_job")

The complete code so far:

  1. from pyflink.dataset import ExecutionEnvironment
  2. from pyflink.table import TableConfig, DataTypes, BatchTableEnvironment
  3. from pyflink.table.descriptors import Schema, OldCsv, FileSystem
  4. exec_env = ExecutionEnvironment.get_execution_environment()
  5. exec_env.set_parallelism(1)
  6. t_config = TableConfig()
  7. t_env = BatchTableEnvironment.create(exec_env, t_config)
  8. t_env.connect(FileSystem().path('/tmp/input')) \
  9. .with_format(OldCsv()
  10. .field('word', DataTypes.STRING())) \
  11. .with_schema(Schema()
  12. .field('word', DataTypes.STRING())) \
  13. .create_temporary_table('mySource')
  14. t_env.connect(FileSystem().path('/tmp/output')) \
  15. .with_format(OldCsv()
  16. .field_delimiter('\t')
  17. .field('word', DataTypes.STRING())
  18. .field('count', DataTypes.BIGINT())) \
  19. .with_schema(Schema()
  20. .field('word', DataTypes.STRING())
  21. .field('count', DataTypes.BIGINT())) \
  22. .create_temporary_table('mySink')
  23. t_env.from_path('mySource') \
  24. .group_by('word') \
  25. .select('word, count(1)') \
  26. .insert_into('mySink')
  27. t_env.execute("tutorial_job")

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:

  1. $ echo -e "flink\npyflink\nflink" > /tmp/input

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):

  1. $ python WordCount.py

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:

  1. $ cat /tmp/output
  2. flink 2
  3. pyflink 1

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.