gfw.common.beam.transforms.WritePartitionedParquet#
- class WritePartitionedParquet(path, schema, window_size=60, allowed_lateness=0, accumulation_mode=1, num_shards=6, codec='snappy', file_suffix='.parquet', partition=None, sink_factory=None)[source]#
Writes elements to hive-partitioned Parquet files.
Output files are organized using Hive-style partitioning. Time partitioning is always present and derived from the window start. Extra dimensions are user-defined and prepended before the time component:
{path}/{prefix}{dim1}={v1}/{prefix}{dim2}={v2}/.../{prefix}date=YYYY-MM-DD/{prefix}hour=HH/
For example, with
HivePartitionConfig(fields={"source": fn}):{path}/event_source=kpler/event_date=2024-01-15/event_hour=08/
## Tuning window_size and num_shards
The primary tuning knob is the combination of
window_sizeandnum_shards, which together determine output file size:rows_per_file = (rows_per_second x window_size) / num_shards
File size directly controls:
Memory consumption per worker: larger files = more memory during flush.
Query performance: files that are too small increase metadata overhead in BigQuery external table scans; files that are too large reduce parallelism.
GCS write latency: files must be written within the window duration, so very large files with short windows can cause the pipeline to stall.
As a rule of thumb, target file sizes of 10-50 MB.
## Row groups
Each output file contains a single Parquet row group, which is optimal for BigQuery external table scans. This is a natural consequence of writing all rows for a shard in a single flush at window close.
- Parameters:
path (str) – Output path where files will be written.
schema (pa.Schema) – PyArrow schema used by the Parquet writer.
window_size (int) – Size of the fixed window in seconds. Controls how frequently files are written. Shorter windows reduce latency and file size but produce more files. Defaults to 60 seconds.
allowed_lateness (int) – Allowed lateness in seconds. Late records arriving within this duration after the window closes will still be included. Defaults to 0.
accumulation_mode (AccumulationMode) – Beam accumulation mode. Defaults to
DISCARDING, which is correct for most streaming use cases.num_shards (int) – Number of output files per partition per window. Must be tuned together with
window_sizeto hit the target file size. Defaults to 6.codec (str) – Compression codec for Parquet. Defaults to
"snappy".file_suffix (str) – File suffix. Defaults to
".parquet".partition (HivePartitionConfig | None) – Hive partition layout. Defaults to hourly time-only partitioning with
"event_"prefix. SeeHivePartitionConfig.sink_factory (Callable[..., ParquetSink] | None) – A callable
(schema, codec) -> FileSinkused to create the sink for each output file. Defaults toParquetSink. InjectFakeParquetSinkto bypass GCS writes in tests while still exercising windowing, partitioning, and file-naming logic.
Methods
annotationsdefault_labeldefault_type_hintsReturns the display data associated to a pipeline component.
Applies windowing, partition keying, and Parquet file writing.
from_runner_apiget_resource_hintsGets and/or initializes type hints for this object.
Returns the window function to be associated with transform's output.
infer_output_typeregister_urnrunner_api_requires_keyed_inputto_runner_apito_runner_api_parameterto_runner_api_pickledtype_check_inputstype_check_inputs_or_outputstype_check_outputsAnnotates the input type of a
PTransformwith a type-hint.Annotates the output type of a
PTransformwith a type-hint.Adds resource hints to the
PTransform.Attributes
labelpipelineside_inputs