Python API Tutorial

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

Note Python 3.5 or higher is required to run PyFlink. Run the following command to confirm that the command “python” in current environment points to Python 3.5+:

  1. $ python --version
  2. # the version printed here must be 3.5+

Setting up a Python Project

You can begin by creating a Python project and installing the PyFlink package.PyFlink is available via PyPi and can be easily installed using pip.

  1. $ python -m pip install apache-flink

You can also build PyFlink from source by following the development guide.

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

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

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 "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 referJob Submission Examplesfor 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 referFlink Python Table API Docs for more details.