pyflink.dataframe.DataFrame.flat_map#
- DataFrame.flat_map(func: Union[Callable[[Dict[str, Any]], Any], DataFrameUDTFWrapper], *, return_dtype: Optional[DataFrameDataTypeLike] = None, concurrency: Optional[int] = None, num_gpus: Optional[float] = None, gpu_type: Optional[str] = None) DataFrame[source]#
Apply a row-based UDTF to each row, producing zero or more rows.
Raw Python functions receive a
dict[str, Any]keyed by DataFrame column name. The result contains only the emitted UDTF columns. When type hints are present, raw Python functions should annotate the row parameter as dict-like, whileDataFrameUDTFWrapperfunctions should annotate it asRowor leave it unannotated. Output column names are inferred from namedTypedDictor struct return types. Scalar outputs and unnamed single-field struct outputs usef0. Unnamed multi-field outputs are rejected; use aTypedDictreturn type or named structreturn_dtypeinstead. Whenfuncis aDataFrameUDTFWrapper,return_dtypemust not be specified because the wrapper already carries its return type. Resource arguments supplied toflat_mapoverride resources defined on the wrapper for this operation.- Example::
>>> from typing import Any, Dict, Iterator, TypedDict >>> from pyflink.dataframe import DataType >>> >>> class Output(TypedDict): ... value: int >>> >>> def expand(row: Dict[str, Any]) -> Iterator[Output]: ... yield {"value": row["x"]} ... yield {"value": row["x"] + 1} >>> >>> df.flat_map(expand) >>> >>> def expand_with_dtype(row): ... yield {"value": row["x"]} ... yield {"value": row["x"] + 1} >>> >>> df.flat_map( ... expand_with_dtype, ... return_dtype=DataType.struct({"value": DataType.int64()}), ... )