fiftyone.utils.beam¶
Apache Beam utilities.
Classes:
|
|
|
|
|
Functions:
|
Exports the given sample collection in the specified number shards via Apache Beam. |
|
Imports the given samples into the dataset via Apache Beam. |
|
Merges the given samples into the dataset via Apache Beam. |
-
fiftyone.utils.beam.
beam_import
(dataset, samples, parse_fcn=None, expand_schema=True, validate=True, options=None, verbose=False)¶ Imports the given samples into the dataset via Apache Beam.
This function is a parallelized alternative to
fiftyone.core.dataset.Dataset.add_samples()
.Note
The insertion order of the samples is not guaranteed.
Example:
import fiftyone as fo import fiftyone.utils.beam as foub samples = range(10000) def make_sample(idx): return fo.Sample(filepath="image%d.png" % idx, uuid=idx) # # Option 1: build the samples on the workers # dataset = fo.Dataset() foub.beam_import(dataset, samples, parse_fcn=make_sample) print(dataset) # # Option 2: build the samples in the main thread # # This is generally not preferred but may be necessary if your # ``parse_fcn`` is not serializable # dataset = fo.Dataset() samples = map(make_sample, samples) foub.beam_import(dataset, samples) print(dataset)
- Parameters
dataset – a
fiftyone.core.dataset.Dataset
samples – an iterable of samples. If no
parse_fcn
is provided, these must befiftyone.core.sample.Sample
instances. If aparse_fcn
is provided, these are passed to it for parsingparse_fcn (None) – an optional function that converts elements of
samples
tofiftyone.core.sample.Sample
instancesexpand_schema (True) – whether to dynamically add new sample fields encountered to the dataset schema. If False, an error is raised if a sample’s schema is not a subset of the dataset schema
validate (True) – whether to validate that the fields of each sample are compliant with the dataset schema before adding it
options (None) – a
apache_beam.options.pipeline_options.PipelineOptions
that configures how to run the pipeline. By default, the pipeline will be run via Beam’s direct runner usingmultiprocessing.cpu_count()
threadsverbose (False) – whether to log the Beam pipeline’s messages
-
fiftyone.utils.beam.
beam_merge
(dataset, samples, parse_fcn=None, options=None, verbose=False, **kwargs)¶ Merges the given samples into the dataset via Apache Beam.
This function is a parallelized alternative to
fiftyone.core.dataset.Dataset.merge_samples()
.Note
This function is only useful for merging in-memory samples into a dataset. If you are merging a sample collection, simply call
fiftyone.core.dataset.Dataset.merge_samples()
.Example:
import fiftyone as fo import fiftyone.utils.beam as foub import fiftyone.zoo as foz dataset = foz.load_zoo_dataset("quickstart").clone() samples = iter(dataset.select_fields("predictions")) foub.beam_merge(dataset, samples, fields={"predictions": "predictions2"}) print(dataset.count("predictions.detections")) print(dataset.count("predictions2.detections"))
- Parameters
dataset – a
fiftyone.core.dataset.Dataset
samples – an iterable of samples. If no
parse_fcn
is provided, these must befiftyone.core.sample.Sample
instances. If aparse_fcn
is provided, these are passed to it for parsingparse_fcn (None) – an optional function that converts elements of
samples
tofiftyone.core.sample.Sample
instancesoptions (None) – a
apache_beam.options.pipeline_options.PipelineOptions
that configures how to run the pipeline. By default, the pipeline will be run via Beam’s direct runner usingmultiprocessing.cpu_count()
threadsverbose (False) – whether to log the Beam pipeline’s messages
**kwargs – keyword arguments for
fiftyone.core.dataset.Dataset.merge_samples()
-
fiftyone.utils.beam.
beam_export
(sample_collection, num_shards, options=None, verbose=False, render_kwargs=None, **kwargs)¶ Exports the given sample collection in the specified number shards via Apache Beam.
This function is a parallelized alternative to
fiftyone.core.collections.SampleCollection.export()
that effectively performs the following sharded export in parallel:for idx, (first, last) in enumerate(shards, 1): _kwargs = render_kwargs(kwargs, idx) sample_collection[first:last].export(**_kwargs)
Example:
from apache_beam.options.pipeline_options import PipelineOptions import fiftyone as fo import fiftyone.utils.beam as foub import fiftyone.zoo as foz dataset = foz.load_zoo_dataset("quickstart") # Use multithreading instead of the default multiprocessing options = PipelineOptions( runner="direct", direct_num_workers=10, direct_running_mode="multi_threading", ) foub.beam_export( dataset, num_shards=20, options=options, dataset_type=fo.types.TFObjectDetectionDataset, label_field="ground_truth", tf_records_path="/tmp/beam/tf.records-%05d-of-00020", )
- Parameters
sample_collection – a
fiftyone.core.collections.SampleCollection
num_shards – the number of shards to write
options (None) – a
apache_beam.options.pipeline_options.PipelineOptions
that configures how to run the pipeline. By default, the pipeline will be run via Beam’s direct runner usingmin(num_shards, multiprocessing.cpu_count())
processesverbose (False) – whether to log the Beam pipeline’s messages
render_kwargs (None) – a function that renders
kwargs
for the current shard. The function should have signaturedef render_kwargs(kwargs, idx) -> kwargs
, whereidx
in[1, num_shards]
is the shard index. By default, any string-valued arguments that contain format patterns like%05d
will be rendered viavalue % idx
**kwargs – keyword arguments for
fiftyone.core.collections.SampleCollection.export()
-
class
fiftyone.utils.beam.
ImportBatch
(dataset_name, parse_fcn=None, expand_schema=True, validate=True)¶ Bases:
apache_beam.transforms.core.DoFn
Classes:
alias of
apache_beam.transforms.core._BundleFinalizerParam
alias of
apache_beam.transforms.core._RestrictionDoFnParam
alias of
apache_beam.transforms.core._StateDoFnParam
alias of
apache_beam.transforms.core._TimerDoFnParam
alias of
apache_beam.transforms.core._WatermarkEstimatorParam
Attributes:
Methods:
Returns the display data associated to a pipeline component.
Called after a bundle of elements is processed on a worker.
from_callable
(fn)from_runner_api
(fn_proto, context)Converts from an FunctionSpec to a Fn object.
get_function_arguments
(func)Gets and/or initializes type hints for this object.
infer_output_type
(input_type)process
(sample)Method to use for processing elements.
register_pickle_urn
(pickle_urn)Registers and implements the given urn via pickling.
register_urn
(urn, parameter_type[, fn])Registers a urn with a constructor.
setup
()Called to prepare an instance for processing bundles of elements.
Called before a bundle of elements is processed on a worker.
teardown
()Called to use to clean up this instance before it is discarded.
to_runner_api
(context)Returns an FunctionSpec encoding this Fn.
to_runner_api_parameter
(context)A decorator on process fn specifying that the fn performs an unbounded amount of work per input element.
with_input_types
(*arg_hints, **kwarg_hints)with_output_types
(*arg_hints, **kwarg_hints)-
setup
()¶ Called to prepare an instance for processing bundles of elements.
This is a good place to initialize transient in-memory resources, such as network connections. The resources can then be disposed in
DoFn.teardown
.
-
start_bundle
()¶ Called before a bundle of elements is processed on a worker.
Elements to be processed are split into bundles and distributed to workers. Before a worker calls process() on the first element of its bundle, it calls this method.
-
process
(sample)¶ Method to use for processing elements.
This is invoked by
DoFnRunner
for each element of a inputPCollection
.The following parameters can be used as default values on
process
arguments to indicate that a DoFn accepts the corresponding parameters. For example, a DoFn might accept the element and its timestamp with the following signature:def process(element=DoFn.ElementParam, timestamp=DoFn.TimestampParam): ...
The full set of parameters is:
DoFn.ElementParam
: element to be processed, should not be mutated.DoFn.SideInputParam
: a side input that may be used when processing.DoFn.TimestampParam
: timestamp of the input element.DoFn.WindowParam
:Window
the input element belongs to.DoFn.TimerParam
: auserstate.RuntimeTimer
object defined by the spec of the parameter.DoFn.StateParam
: auserstate.RuntimeState
object defined by the spec of the parameter.DoFn.KeyParam
: key associated with the element.DoFn.RestrictionParam
: aniobase.RestrictionTracker
will be provided here to allow treatment as a SplittableDoFn
. The restriction tracker will be derived from the restriction provider in the parameter.DoFn.WatermarkEstimatorParam
: a function that can be used to track output watermark of SplittableDoFn
implementations.
- Parameters
element – The element to be processed
*args – side inputs
**kwargs – other keyword arguments.
- Returns
An Iterable of output elements or None.
-
finish_bundle
()¶ Called after a bundle of elements is processed on a worker.
-
BundleFinalizerParam
¶ alias of
apache_beam.transforms.core._BundleFinalizerParam
Methods:finalize_bundle
()has_callbacks
()register
(callback)reset
()
-
DoFnProcessParams
= [ElementParam, SideInputParam, TimestampParam, WindowParam, <class 'apache_beam.transforms.core._WatermarkEstimatorParam'>, PaneInfoParam, <class 'apache_beam.transforms.core._BundleFinalizerParam'>, KeyParam, <class 'apache_beam.transforms.core._StateDoFnParam'>, <class 'apache_beam.transforms.core._TimerDoFnParam'>]¶
-
DynamicTimerTagParam
= DynamicTimerTagParam¶
-
ElementParam
= ElementParam¶
-
KeyParam
= KeyParam¶
-
PaneInfoParam
= PaneInfoParam¶
-
RestrictionParam
¶ alias of
apache_beam.transforms.core._RestrictionDoFnParam
-
SideInputParam
= SideInputParam¶
-
StateParam
¶ alias of
apache_beam.transforms.core._StateDoFnParam
-
TimerParam
¶ alias of
apache_beam.transforms.core._TimerDoFnParam
-
TimestampParam
= TimestampParam¶
-
WatermarkEstimatorParam
¶ alias of
apache_beam.transforms.core._WatermarkEstimatorParam
-
WindowParam
= WindowParam¶
-
default_label
()¶
-
default_type_hints
()¶
-
display_data
()¶ Returns the display data associated to a pipeline component.
It should be reimplemented in pipeline components that wish to have static display data.
- Returns
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 }
- Return type
Dict[str, Any]
-
static
from_callable
(fn)¶
-
classmethod
from_runner_api
(fn_proto, context)¶ Converts from an FunctionSpec to a Fn object.
Prefer registering a urn with its parameter type and constructor.
-
get_function_arguments
(func)¶
-
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.
-
infer_output_type
(input_type)¶
-
classmethod
register_pickle_urn
(pickle_urn)¶ Registers and implements the given urn via pickling.
-
classmethod
register_urn
(urn, parameter_type, fn=None)¶ Registers a urn with a constructor.
For example, if ‘beam:fn:foo’ had parameter type FooPayload, one could write RunnerApiFn.register_urn(‘bean:fn:foo’, FooPayload, foo_from_proto) where foo_from_proto took as arguments a FooPayload and a PipelineContext. This function can also be used as a decorator rather than passing the callable in as the final parameter.
A corresponding to_runner_api_parameter method would be expected that returns the tuple (‘beam:fn:foo’, FooPayload)
-
teardown
()¶ Called to use to clean up this instance before it is discarded.
A runner will do its best to call this method on any given instance to prevent leaks of transient resources, however, there may be situations where this is impossible (e.g. process crash, hardware failure, etc.) or unnecessary (e.g. the pipeline is shutting down and the process is about to be killed anyway, so all transient resources will be released automatically by the OS). In these cases, the call may not happen. It will also not be retried, because in such situations the DoFn instance no longer exists, so there’s no instance to retry it on.
Thus, all work that depends on input elements, and all externally important side effects, must be performed in
DoFn.process
orDoFn.finish_bundle
.
-
to_runner_api
(context)¶ Returns an FunctionSpec encoding this Fn.
Prefer overriding self.to_runner_api_parameter.
-
to_runner_api_parameter
(context)¶
-
static
unbounded_per_element
()¶ A decorator on process fn specifying that the fn performs an unbounded amount of work per input element.
-
with_input_types
(*arg_hints, **kwarg_hints)¶
-
with_output_types
(*arg_hints, **kwarg_hints)¶
-
-
class
fiftyone.utils.beam.
MergeBatch
(dataset_name, parse_fcn=None, key_field='filepath', key_fcn=None, expand_schema=True, **kwargs)¶ Bases:
apache_beam.transforms.core.DoFn
Classes:
alias of
apache_beam.transforms.core._BundleFinalizerParam
alias of
apache_beam.transforms.core._RestrictionDoFnParam
alias of
apache_beam.transforms.core._StateDoFnParam
alias of
apache_beam.transforms.core._TimerDoFnParam
alias of
apache_beam.transforms.core._WatermarkEstimatorParam
Attributes:
Methods:
Returns the display data associated to a pipeline component.
Called after a bundle of elements is processed on a worker.
from_callable
(fn)from_runner_api
(fn_proto, context)Converts from an FunctionSpec to a Fn object.
get_function_arguments
(func)Gets and/or initializes type hints for this object.
infer_output_type
(input_type)process
(sample)Method to use for processing elements.
register_pickle_urn
(pickle_urn)Registers and implements the given urn via pickling.
register_urn
(urn, parameter_type[, fn])Registers a urn with a constructor.
setup
()Called to prepare an instance for processing bundles of elements.
Called before a bundle of elements is processed on a worker.
teardown
()Called to use to clean up this instance before it is discarded.
to_runner_api
(context)Returns an FunctionSpec encoding this Fn.
to_runner_api_parameter
(context)A decorator on process fn specifying that the fn performs an unbounded amount of work per input element.
with_input_types
(*arg_hints, **kwarg_hints)with_output_types
(*arg_hints, **kwarg_hints)-
setup
()¶ Called to prepare an instance for processing bundles of elements.
This is a good place to initialize transient in-memory resources, such as network connections. The resources can then be disposed in
DoFn.teardown
.
-
start_bundle
()¶ Called before a bundle of elements is processed on a worker.
Elements to be processed are split into bundles and distributed to workers. Before a worker calls process() on the first element of its bundle, it calls this method.
-
process
(sample)¶ Method to use for processing elements.
This is invoked by
DoFnRunner
for each element of a inputPCollection
.The following parameters can be used as default values on
process
arguments to indicate that a DoFn accepts the corresponding parameters. For example, a DoFn might accept the element and its timestamp with the following signature:def process(element=DoFn.ElementParam, timestamp=DoFn.TimestampParam): ...
The full set of parameters is:
DoFn.ElementParam
: element to be processed, should not be mutated.DoFn.SideInputParam
: a side input that may be used when processing.DoFn.TimestampParam
: timestamp of the input element.DoFn.WindowParam
:Window
the input element belongs to.DoFn.TimerParam
: auserstate.RuntimeTimer
object defined by the spec of the parameter.DoFn.StateParam
: auserstate.RuntimeState
object defined by the spec of the parameter.DoFn.KeyParam
: key associated with the element.DoFn.RestrictionParam
: aniobase.RestrictionTracker
will be provided here to allow treatment as a SplittableDoFn
. The restriction tracker will be derived from the restriction provider in the parameter.DoFn.WatermarkEstimatorParam
: a function that can be used to track output watermark of SplittableDoFn
implementations.
- Parameters
element – The element to be processed
*args – side inputs
**kwargs – other keyword arguments.
- Returns
An Iterable of output elements or None.
-
finish_bundle
()¶ Called after a bundle of elements is processed on a worker.
-
BundleFinalizerParam
¶ alias of
apache_beam.transforms.core._BundleFinalizerParam
Methods:finalize_bundle
()has_callbacks
()register
(callback)reset
()
-
DoFnProcessParams
= [ElementParam, SideInputParam, TimestampParam, WindowParam, <class 'apache_beam.transforms.core._WatermarkEstimatorParam'>, PaneInfoParam, <class 'apache_beam.transforms.core._BundleFinalizerParam'>, KeyParam, <class 'apache_beam.transforms.core._StateDoFnParam'>, <class 'apache_beam.transforms.core._TimerDoFnParam'>]¶
-
DynamicTimerTagParam
= DynamicTimerTagParam¶
-
ElementParam
= ElementParam¶
-
KeyParam
= KeyParam¶
-
PaneInfoParam
= PaneInfoParam¶
-
RestrictionParam
¶ alias of
apache_beam.transforms.core._RestrictionDoFnParam
-
SideInputParam
= SideInputParam¶
-
StateParam
¶ alias of
apache_beam.transforms.core._StateDoFnParam
-
TimerParam
¶ alias of
apache_beam.transforms.core._TimerDoFnParam
-
TimestampParam
= TimestampParam¶
-
WatermarkEstimatorParam
¶ alias of
apache_beam.transforms.core._WatermarkEstimatorParam
-
WindowParam
= WindowParam¶
-
default_label
()¶
-
default_type_hints
()¶
-
display_data
()¶ Returns the display data associated to a pipeline component.
It should be reimplemented in pipeline components that wish to have static display data.
- Returns
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 }
- Return type
Dict[str, Any]
-
static
from_callable
(fn)¶
-
classmethod
from_runner_api
(fn_proto, context)¶ Converts from an FunctionSpec to a Fn object.
Prefer registering a urn with its parameter type and constructor.
-
get_function_arguments
(func)¶
-
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.
-
infer_output_type
(input_type)¶
-
classmethod
register_pickle_urn
(pickle_urn)¶ Registers and implements the given urn via pickling.
-
classmethod
register_urn
(urn, parameter_type, fn=None)¶ Registers a urn with a constructor.
For example, if ‘beam:fn:foo’ had parameter type FooPayload, one could write RunnerApiFn.register_urn(‘bean:fn:foo’, FooPayload, foo_from_proto) where foo_from_proto took as arguments a FooPayload and a PipelineContext. This function can also be used as a decorator rather than passing the callable in as the final parameter.
A corresponding to_runner_api_parameter method would be expected that returns the tuple (‘beam:fn:foo’, FooPayload)
-
teardown
()¶ Called to use to clean up this instance before it is discarded.
A runner will do its best to call this method on any given instance to prevent leaks of transient resources, however, there may be situations where this is impossible (e.g. process crash, hardware failure, etc.) or unnecessary (e.g. the pipeline is shutting down and the process is about to be killed anyway, so all transient resources will be released automatically by the OS). In these cases, the call may not happen. It will also not be retried, because in such situations the DoFn instance no longer exists, so there’s no instance to retry it on.
Thus, all work that depends on input elements, and all externally important side effects, must be performed in
DoFn.process
orDoFn.finish_bundle
.
-
to_runner_api
(context)¶ Returns an FunctionSpec encoding this Fn.
Prefer overriding self.to_runner_api_parameter.
-
to_runner_api_parameter
(context)¶
-
static
unbounded_per_element
()¶ A decorator on process fn specifying that the fn performs an unbounded amount of work per input element.
-
with_input_types
(*arg_hints, **kwarg_hints)¶
-
with_output_types
(*arg_hints, **kwarg_hints)¶
-
-
class
fiftyone.utils.beam.
ExportBatch
(dataset_name, view_stages=None, render_kwargs=None)¶ Bases:
apache_beam.transforms.core.DoFn
Classes:
alias of
apache_beam.transforms.core._BundleFinalizerParam
alias of
apache_beam.transforms.core._RestrictionDoFnParam
alias of
apache_beam.transforms.core._StateDoFnParam
alias of
apache_beam.transforms.core._TimerDoFnParam
alias of
apache_beam.transforms.core._WatermarkEstimatorParam
Attributes:
Methods:
default_render_kwargs
(kwargs, idx)Returns the display data associated to a pipeline component.
Called after a bundle of elements is processed on a worker.
from_callable
(fn)from_runner_api
(fn_proto, context)Converts from an FunctionSpec to a Fn object.
get_function_arguments
(func)Gets and/or initializes type hints for this object.
infer_output_type
(input_type)process
(element, **kwargs)Method to use for processing elements.
register_pickle_urn
(pickle_urn)Registers and implements the given urn via pickling.
register_urn
(urn, parameter_type[, fn])Registers a urn with a constructor.
setup
()Called to prepare an instance for processing bundles of elements.
Called before a bundle of elements is processed on a worker.
teardown
()Called to use to clean up this instance before it is discarded.
to_runner_api
(context)Returns an FunctionSpec encoding this Fn.
to_runner_api_parameter
(context)A decorator on process fn specifying that the fn performs an unbounded amount of work per input element.
with_input_types
(*arg_hints, **kwarg_hints)with_output_types
(*arg_hints, **kwarg_hints)-
static
default_render_kwargs
(kwargs, idx)¶
-
setup
()¶ Called to prepare an instance for processing bundles of elements.
This is a good place to initialize transient in-memory resources, such as network connections. The resources can then be disposed in
DoFn.teardown
.
-
process
(element, **kwargs)¶ Method to use for processing elements.
This is invoked by
DoFnRunner
for each element of a inputPCollection
.The following parameters can be used as default values on
process
arguments to indicate that a DoFn accepts the corresponding parameters. For example, a DoFn might accept the element and its timestamp with the following signature:def process(element=DoFn.ElementParam, timestamp=DoFn.TimestampParam): ...
The full set of parameters is:
DoFn.ElementParam
: element to be processed, should not be mutated.DoFn.SideInputParam
: a side input that may be used when processing.DoFn.TimestampParam
: timestamp of the input element.DoFn.WindowParam
:Window
the input element belongs to.DoFn.TimerParam
: auserstate.RuntimeTimer
object defined by the spec of the parameter.DoFn.StateParam
: auserstate.RuntimeState
object defined by the spec of the parameter.DoFn.KeyParam
: key associated with the element.DoFn.RestrictionParam
: aniobase.RestrictionTracker
will be provided here to allow treatment as a SplittableDoFn
. The restriction tracker will be derived from the restriction provider in the parameter.DoFn.WatermarkEstimatorParam
: a function that can be used to track output watermark of SplittableDoFn
implementations.
- Parameters
element – The element to be processed
*args – side inputs
**kwargs – other keyword arguments.
- Returns
An Iterable of output elements or None.
-
BundleFinalizerParam
¶ alias of
apache_beam.transforms.core._BundleFinalizerParam
Methods:finalize_bundle
()has_callbacks
()register
(callback)reset
()
-
DoFnProcessParams
= [ElementParam, SideInputParam, TimestampParam, WindowParam, <class 'apache_beam.transforms.core._WatermarkEstimatorParam'>, PaneInfoParam, <class 'apache_beam.transforms.core._BundleFinalizerParam'>, KeyParam, <class 'apache_beam.transforms.core._StateDoFnParam'>, <class 'apache_beam.transforms.core._TimerDoFnParam'>]¶
-
DynamicTimerTagParam
= DynamicTimerTagParam¶
-
ElementParam
= ElementParam¶
-
KeyParam
= KeyParam¶
-
PaneInfoParam
= PaneInfoParam¶
-
RestrictionParam
¶ alias of
apache_beam.transforms.core._RestrictionDoFnParam
-
SideInputParam
= SideInputParam¶
-
StateParam
¶ alias of
apache_beam.transforms.core._StateDoFnParam
-
TimerParam
¶ alias of
apache_beam.transforms.core._TimerDoFnParam
-
TimestampParam
= TimestampParam¶
-
WatermarkEstimatorParam
¶ alias of
apache_beam.transforms.core._WatermarkEstimatorParam
-
WindowParam
= WindowParam¶
-
default_label
()¶
-
default_type_hints
()¶
-
display_data
()¶ Returns the display data associated to a pipeline component.
It should be reimplemented in pipeline components that wish to have static display data.
- Returns
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 }
- Return type
Dict[str, Any]
-
finish_bundle
()¶ Called after a bundle of elements is processed on a worker.
-
static
from_callable
(fn)¶
-
classmethod
from_runner_api
(fn_proto, context)¶ Converts from an FunctionSpec to a Fn object.
Prefer registering a urn with its parameter type and constructor.
-
get_function_arguments
(func)¶
-
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.
-
infer_output_type
(input_type)¶
-
classmethod
register_pickle_urn
(pickle_urn)¶ Registers and implements the given urn via pickling.
-
classmethod
register_urn
(urn, parameter_type, fn=None)¶ Registers a urn with a constructor.
For example, if ‘beam:fn:foo’ had parameter type FooPayload, one could write RunnerApiFn.register_urn(‘bean:fn:foo’, FooPayload, foo_from_proto) where foo_from_proto took as arguments a FooPayload and a PipelineContext. This function can also be used as a decorator rather than passing the callable in as the final parameter.
A corresponding to_runner_api_parameter method would be expected that returns the tuple (‘beam:fn:foo’, FooPayload)
-
start_bundle
()¶ Called before a bundle of elements is processed on a worker.
Elements to be processed are split into bundles and distributed to workers. Before a worker calls process() on the first element of its bundle, it calls this method.
-
teardown
()¶ Called to use to clean up this instance before it is discarded.
A runner will do its best to call this method on any given instance to prevent leaks of transient resources, however, there may be situations where this is impossible (e.g. process crash, hardware failure, etc.) or unnecessary (e.g. the pipeline is shutting down and the process is about to be killed anyway, so all transient resources will be released automatically by the OS). In these cases, the call may not happen. It will also not be retried, because in such situations the DoFn instance no longer exists, so there’s no instance to retry it on.
Thus, all work that depends on input elements, and all externally important side effects, must be performed in
DoFn.process
orDoFn.finish_bundle
.
-
to_runner_api
(context)¶ Returns an FunctionSpec encoding this Fn.
Prefer overriding self.to_runner_api_parameter.
-
to_runner_api_parameter
(context)¶
-
static
unbounded_per_element
()¶ A decorator on process fn specifying that the fn performs an unbounded amount of work per input element.
-
with_input_types
(*arg_hints, **kwarg_hints)¶
-
with_output_types
(*arg_hints, **kwarg_hints)¶
-
static