Apache Flink Python API · stateful stream & batch processing · verified against PyFlink 2.x (2026)

PyFlink cheat sheet

PyFlink is the Python API for Apache Flink, the engine for real-time, stateful stream processing (and unified batch). Two layers: the high-level Table API / SQL (relational, declarative — use this for most pipelines) and the lower-level DataStream API (fine-grained control over state, time & timers). Flink's superpowers are event-time processing with watermarks and fault-tolerant state. This sheet targets PyFlink 2.x (Flink 2.x).

setup & two APIs Table API / SQL windows & time DataStream API interop & run gotcha most common

Verified 2026-08-24 against the official docs at nightlies.apache.org/flink (PyFlink 2.x) and pyflink.readthedocs.io. A Java/Flink runtime executes the job; PyFlink builds the dataflow. Needs Java.

Outline

Pick the API: Table/SQL for declarative ETL & analytics, DataStream for custom stateful logic. Both build a lazy dataflow that runs on execute.

Start

  1. 1 · Install & two APIs

Table / SQL

  1. 2 · TableEnvironment & DDL
  2. 3 · Query & sink
  3. 4 · Time, watermarks, windows
  4. 5 · UDFs

DataStream

  1. 6 · Environment & transforms
  2. 7 · Keyed state & process fns

Operate

  1. 8 · Interop & submit
  2. 9 · Gotchas
  3. Worth memorizing

Start

Install and choose your abstraction level.

1Install & two APIs2.x

Table API / SQL

Declarative pipelines over tables — the recommended path.

2TableEnvironment & DDLconnect to sources
3Query & sinktransform & write
4Time, watermarks, windowsthe streaming core
5UDFscustom Python logic

DataStream API

Low-level control when SQL isn't enough.

6Environment & transformsexplicit dataflow
7Keyed state & process fnsstateful streaming

Operate

Bridge the APIs and submit to a cluster.

8Interop & submitrun it
!Common gotchasread before shipping

Worth memorizing

Table/SQL for most, DataStream for controlboth unified stream+batch; both lazy
tables via SQL DDL + connectorWITH ('connector'='kafka'...); WATERMARK for event-time
execute_insert triggers the jobTable; env.execute() for DataStream
windowing TVFsTUMBLE / HOP / CUMULATE / SESSION over an event-time column
pandas UDFs are fastvectorized vs row-at-a-time Python
key_by before keyed opsrequired for windows/reduce/keyed state
keyed state in process functionsValueState/ListState/MapState, per key
enable_checkpointing for exactly-onceor a failure loses state
from/to_data_stream to bridgeSQL for relational work, DataStream for custom logic