gigl.common.beam.BetterWriteToTFRecord#
- class gigl.common.beam.better_tfrecordio.BetterWriteToTFRecord(file_path_prefix: str, transformed_metadata: DatasetMetadata | AsSingleton | None = None, file_name_suffix: str | None = '.tfrecord', compression_type: str | None = 'auto', max_bytes_per_shard: int | None = 200000000, num_shards: int | None = 0)#
Bases:
PTransform
Transform for writing to TFRecord sinks.
Methods
Initialize BetterWriteToTFRecord transform.
annotations
default_label
default_type_hints
Returns the display data associated to a pipeline component.
expand
from_runner_api
get_resource_hints
Gets and/or initializes type hints for this object.
Returns the window function to be associated with transform's output.
infer_output_type
register_urn
runner_api_requires_keyed_input
to_runner_api
to_runner_api_parameter
to_runner_api_pickled
type_check_inputs
type_check_inputs_or_outputs
type_check_outputs
Annotates the input type of a
PTransform
with a type-hint.Annotates the output type of a
PTransform
with a type-hint.Adds resource hints to the
PTransform
.- __init__(file_path_prefix: str, transformed_metadata: DatasetMetadata | AsSingleton | None = None, file_name_suffix: str | None = '.tfrecord', compression_type: str | None = 'auto', max_bytes_per_shard: int | None = 200000000, num_shards: int | None = 0)#
Initialize BetterWriteToTFRecord transform.
We improve the default WriteToTFRecord implementation by first simplifying needed params, adding functionality to cap the max bytes per shard. And, adding support for both easily serializing generic protobuff messages and tf.train.Example messages with capacity to specify deferred (computed at runtime) tft.tf_metadata.dataset_metadata.DatasetMetadata.
- Args:
- file_path_prefix (str): The file path to write to. The files written will begin
with this prefix, followed by a shard identifier, and end in a common extension, if given by file_name_suffix.
- transformed_metadata (Optional[Union[dataset_metadata.DatasetMetadata, apache_beam.pvalue.AsSingleton]]):
Useful for encoding tf.train.Example, when reading a TFTransform fn (dataset_metadata.DatasetMetadata) or when building it for the first time (apache_beam.pvalue.AsSingleton[dataset_metadata.DatasetMetadata]). Defaults to None, meaning a generic protobuf message is assumed which will be encoded using SerializeToString().
file_name_suffix (str, optional): Suffix for the files written. Defaults to “.tfrecord”. compression_type (str, optional): Used to handle compressed output files. Typical value
is CompressionTypes.AUTO, in which case the file_path’s extension will be used to detect the compression.
- max_bytes_per_shard (int, optional): The data is sharded into separate files to promote
faster/distributed writes. This parameter controls the max size of these shards. Defaults to int(2e8), or ~200 Mb.
- num_shards (int, optional): The number of files (shards) used for output. If not set,
the service will decide on the optimal number of shards based off of max_bytes_per_shard. WARNING: Constraining the number of shards is likely to reduce the performance of a pipeline - only use if you know what you are doing.
- classmethod __init_subclass__(*args, **kwargs)#
This method is called when a class is subclassed.
The default implementation does nothing. It may be overridden to extend subclasses.
- __or__(right)#
Used to compose PTransforms, e.g., ptransform1 | ptransform2.
- __repr__()#
Return repr(self).
- __ror__(left, label=None)#
Used to apply this PTransform to non-PValues, e.g., a tuple.
- __str__()#
Return str(self).
- __weakref__#
list of weak references to the object (if defined)
- display_data() dict #
Returns the display data associated to a pipeline component.
It should be reimplemented in pipeline components that wish to have static display data.
- Returns:
Dict[str, Any]: A dictionary containing
key:value
pairs. The value might be an integer, float or string value; aDisplayDataItem
for values that have more data (e.g. short value, label, url); or aHasDisplayData
instance that has more display data that should be picked up. For example:{ 'key1': 'string_value', 'key2': 1234, 'key3': 3.14159265, 'key4': DisplayDataItem('apache.org', url='http://apache.org'), 'key5': subComponent }
- get_type_hints()#
Gets and/or initializes type hints for this object.
If type hints have not been set, attempts to initialize type hints in this order: - Using self.default_type_hints(). - Using self.__class__ type hints.
- get_windowing(inputs: Any) Windowing #
Returns the window function to be associated with transform’s output.
By default most transforms just return the windowing function associated with the input PCollection (or the first input if several).
- with_input_types(input_type_hint)#
Annotates the input type of a
PTransform
with a type-hint.- Args:
- input_type_hint (type): An instance of an allowed built-in type, a custom
class, or an instance of a
TypeConstraint
.
- Raises:
- TypeError: If input_type_hint is not a valid type-hint.
See
apache_beam.typehints.typehints.validate_composite_type_param()
for further details.
- Returns:
PTransform: A reference to the instance of this particular
PTransform
object. This allows chaining type-hinting related methods.
- with_output_types(type_hint)#
Annotates the output type of a
PTransform
with a type-hint.- Args:
- type_hint (type): An instance of an allowed built-in type, a custom class,
or a
TypeConstraint
.
- Raises:
- TypeError: If type_hint is not a valid type-hint. See
validate_composite_type_param()
for further details.
- Returns:
PTransform: A reference to the instance of this particular
PTransform
object. This allows chaining type-hinting related methods.
- with_resource_hints(**kwargs) PTransform #
Adds resource hints to the
PTransform
.Resource hints allow users to express constraints on the environment where the transform should be executed. Interpretation of the resource hints is defined by Beam Runners. Runners may ignore the unsupported hints.
- Args:
**kwargs: key-value pairs describing hints and their values.
- Raises:
- ValueError: if provided hints are unknown to the SDK. See
apache_beam.transforms.resources
for a list of known hints.
- Returns:
PTransform: A reference to the instance of this particular
PTransform
object.