apache_beam.io.kinesis module¶
PTransforms for supporting Kinesis streaming in Python pipelines.
These transforms are currently supported by Beam Flink and Spark portable runners.
Setup
Transforms provided in this module are cross-language transforms implemented in the Beam Java SDK. During the pipeline construction, Python SDK will connect to a Java expansion service to expand these transforms. To facilitate this, a small amount of setup is needed before using these transforms in a Beam Python pipeline.
There are several ways to setup cross-language Kinesis transforms.
- Option 1: use the default expansion service
- Option 2: specify a custom expansion service
See below for details regarding each of these options.
Option 1: Use the default expansion service
This is the recommended and easiest setup option for using Python Kinesis transforms. This option is only available for Beam 2.25.0 and later.
This option requires following pre-requisites before running the Beam pipeline.
- Install Java runtime in the computer from where the pipeline is constructed and make sure that ‘java’ command is available.
In this option, Python SDK will either download (for released Beam version) or build (when running from a Beam Git clone) a expansion service jar and use that to expand transforms. Currently Kinesis transforms use the ‘beam-sdks-java-io-kinesis-expansion-service’ jar for this purpose.
Option 2: specify a custom expansion service
In this option, you startup your own expansion service and provide that as a parameter when using the transforms provided in this module.
This option requires following pre-requisites before running the Beam pipeline.
- Startup your own expansion service.
- Update your pipeline to provide the expansion service address when initiating Kinesis transforms provided in this module.
Flink Users can use the built-in Expansion Service of the Flink Runner’s Job Server. If you start Flink’s Job Server, the expansion service will be started on port 8097. For a different address, please set the expansion_service parameter.
More information
For more information regarding cross-language transforms see: - https://beam.apache.org/roadmap/portability/
For more information specific to Flink runner see: - https://beam.apache.org/documentation/runners/flink/
-
class
apache_beam.io.kinesis.
WriteToKinesis
(stream_name, aws_access_key, aws_secret_key, region, partition_key, service_endpoint=None, verify_certificate=None, producer_properties=None, expansion_service=None)[source]¶ Bases:
apache_beam.transforms.external.ExternalTransform
An external PTransform which writes byte array stream to Amazon Kinesis.
Experimental; no backwards compatibility guarantees.
Initializes a write operation to Kinesis.
Parameters: - stream_name – Kinesis stream name.
- aws_access_key – Kinesis access key.
- aws_secret_key – Kinesis access key secret.
- region – AWS region. Example: ‘us-east-1’.
- service_endpoint – Kinesis service endpoint
- verify_certificate – Enable or disable certificate verification. Never set to False on production. True by default.
- partition_key – Specify default partition key.
- producer_properties – Specify the configuration properties for Kinesis Producer Library (KPL) as dictionary. Example: {‘CollectionMaxCount’: ‘1000’, ‘ConnectTimeout’: ‘10000’}
- expansion_service – The address (host:port) of the ExpansionService.
-
URN
= 'beam:external:java:kinesis:write:v1'¶
-
annotations
() → Dict[str, Union[bytes, str, google.protobuf.message.Message]]¶
-
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]
-
expand
(pvalueish)¶
-
classmethod
from_runner_api
(proto, context)¶
-
classmethod
get_local_namespace
()¶
-
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)¶ 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).
-
infer_output_type
(unused_input_type)¶
-
label
¶
-
classmethod
outer_namespace
(namespace)¶
-
pipeline
= None¶
-
classmethod
register_urn
(urn, parameter_type, constructor=None)¶
-
runner_api_requires_keyed_input
()¶
-
side_inputs
= ()¶
-
to_runner_api
(context, has_parts=False, **extra_kwargs)¶
-
to_runner_api_parameter
(unused_context)¶
-
to_runner_api_pickled
(unused_context)¶
-
to_runner_api_transform
(context, full_label)¶
-
type_check_inputs
(pvalueish)¶
-
type_check_inputs_or_outputs
(pvalueish, input_or_output)¶
-
type_check_outputs
(pvalueish)¶
-
with_input_types
(input_type_hint)¶ Annotates the input type of a
PTransform
with a type-hint.Parameters: 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. Seeapache_beam.typehints.typehints.validate_composite_type_param()
for further details.Returns: A reference to the instance of this particular PTransform
object. This allows chaining type-hinting related methods.Return type: PTransform
-
with_output_types
(type_hint)¶ Annotates the output type of a
PTransform
with a type-hint.Parameters: 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. Seevalidate_composite_type_param()
for further details.Returns: A reference to the instance of this particular PTransform
object. This allows chaining type-hinting related methods.Return type: PTransform
-
class
apache_beam.io.kinesis.
ReadDataFromKinesis
(stream_name, aws_access_key, aws_secret_key, region, service_endpoint=None, verify_certificate=None, max_num_records=None, max_read_time=None, initial_position_in_stream=None, initial_timestamp_in_stream=None, request_records_limit=None, up_to_date_threshold=None, max_capacity_per_shard=None, watermark_policy=None, watermark_idle_duration_threshold=None, rate_limit=None, expansion_service=None)[source]¶ Bases:
apache_beam.transforms.external.ExternalTransform
An external PTransform which reads byte array stream from Amazon Kinesis.
Experimental; no backwards compatibility guarantees.
Initializes a read operation from Kinesis.
Parameters: - stream_name – Kinesis stream name.
- aws_access_key – Kinesis access key.
- aws_secret_key – Kinesis access key secret.
- region – AWS region. Example: ‘us-east-1’.
- service_endpoint – Kinesis service endpoint
- verify_certificate – Enable or disable certificate verification. Never set to False on production. True by default.
- max_num_records – Specifies to read at most a given number of records. Must be greater than 0.
- max_read_time – Specifies to read records during x milliseconds.
- initial_timestamp_in_stream – Specify reading beginning at the given timestamp in milliseconds. Must be in the past.
- initial_position_in_stream – Specify reading from some initial position in stream. Possible values: LATEST - Start after the most recent data record (fetch new data). TRIM_HORIZON - Start from the oldest available data record. AT_TIMESTAMP - Start from the record at or after the specified server-side timestamp.
- request_records_limit – Specifies the maximum number of records in GetRecordsResult returned by GetRecords call which is limited by 10K records. If should be adjusted according to average size of data record to prevent shard overloading. More at: docs.aws.amazon.com/kinesis/latest/APIReference/API_GetRecords.html
- up_to_date_threshold – Specifies how late in milliseconds records consumed by this source can be to still be considered on time. Defaults to zero.
- max_capacity_per_shard – Specifies the maximum number of messages per one shard. Defaults to 10’000.
- watermark_policy – Specifies the watermark policy. Possible values: PROCESSING_TYPE, ARRIVAL_TIME. Defaults to ARRIVAL_TIME.
- watermark_idle_duration_threshold – Use only when watermark policy is ARRIVAL_TIME. Denotes the duration for which the watermark can be idle. Passed in milliseconds.
- rate_limit – Sets fixed rate policy for given milliseconds value. By default there is no rate limit.
- expansion_service – The address (host:port) of the ExpansionService.
-
URN
= 'beam:external:java:kinesis:read_data:v1'¶
-
annotations
() → Dict[str, Union[bytes, str, google.protobuf.message.Message]]¶
-
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]
-
expand
(pvalueish)¶
-
classmethod
from_runner_api
(proto, context)¶
-
classmethod
get_local_namespace
()¶
-
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)¶ 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).
-
infer_output_type
(unused_input_type)¶
-
label
¶
-
classmethod
outer_namespace
(namespace)¶
-
pipeline
= None¶
-
classmethod
register_urn
(urn, parameter_type, constructor=None)¶
-
runner_api_requires_keyed_input
()¶
-
side_inputs
= ()¶
-
to_runner_api
(context, has_parts=False, **extra_kwargs)¶
-
to_runner_api_parameter
(unused_context)¶
-
to_runner_api_pickled
(unused_context)¶
-
to_runner_api_transform
(context, full_label)¶
-
type_check_inputs
(pvalueish)¶
-
type_check_inputs_or_outputs
(pvalueish, input_or_output)¶
-
type_check_outputs
(pvalueish)¶
-
with_input_types
(input_type_hint)¶ Annotates the input type of a
PTransform
with a type-hint.Parameters: 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. Seeapache_beam.typehints.typehints.validate_composite_type_param()
for further details.Returns: A reference to the instance of this particular PTransform
object. This allows chaining type-hinting related methods.Return type: PTransform
-
with_output_types
(type_hint)¶ Annotates the output type of a
PTransform
with a type-hint.Parameters: 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. Seevalidate_composite_type_param()
for further details.Returns: A reference to the instance of this particular PTransform
object. This allows chaining type-hinting related methods.Return type: PTransform