Class FlinkKinesisConsumer<T>
- java.lang.Object
-
- org.apache.flink.api.common.functions.AbstractRichFunction
-
- org.apache.flink.streaming.api.functions.source.RichParallelSourceFunction<T>
-
- org.apache.flink.streaming.connectors.kinesis.FlinkKinesisConsumer<T>
-
- Type Parameters:
T- the type of data emitted
- All Implemented Interfaces:
Serializable,org.apache.flink.api.common.functions.Function,org.apache.flink.api.common.functions.RichFunction,org.apache.flink.api.java.typeutils.ResultTypeQueryable<T>,org.apache.flink.streaming.api.checkpoint.CheckpointedFunction,org.apache.flink.streaming.api.functions.source.ParallelSourceFunction<T>,org.apache.flink.streaming.api.functions.source.SourceFunction<T>
- Direct Known Subclasses:
FlinkDynamoDBStreamsConsumer
@PublicEvolving public class FlinkKinesisConsumer<T> extends org.apache.flink.streaming.api.functions.source.RichParallelSourceFunction<T> implements org.apache.flink.api.java.typeutils.ResultTypeQueryable<T>, org.apache.flink.streaming.api.checkpoint.CheckpointedFunctionThe Flink Kinesis Consumer is an exactly-once parallel streaming data source that subscribes to multiple AWS Kinesis streams within the same AWS service region, and can handle resharding of streams. Each subtask of the consumer is responsible for fetching data records from multiple Kinesis shards. The number of shards fetched by each subtask will change as shards are closed and created by Kinesis.To leverage Flink's checkpointing mechanics for exactly-once streaming processing guarantees, the Flink Kinesis consumer is implemented with the AWS Java SDK, instead of the officially recommended AWS Kinesis Client Library, for low-level control on the management of stream state. The Flink Kinesis Connector also supports setting the initial starting points of Kinesis streams, namely TRIM_HORIZON and LATEST.
Kinesis and the Flink consumer support dynamic re-sharding and shard IDs, while sequential, cannot be assumed to be consecutive. There is no perfect generic default assignment function. Default shard to subtask assignment, which is based on hash code, may result in skew, with some subtasks having many shards assigned and others none.
It is recommended to monitor the shard distribution and adjust assignment appropriately. A custom assigner implementation can be set via
setShardAssigner(KinesisShardAssigner)to optimize the hash function or use static overrides to limit skew.In order for the consumer to emit watermarks, a timestamp assigner needs to be set via
setPeriodicWatermarkAssigner(AssignerWithPeriodicWatermarks)and the auto watermark emit interval configured viaExecutionConfig.setAutoWatermarkInterval(long).Watermarks can only advance when all shards of a subtask continuously deliver records. To avoid an inactive or closed shard to block the watermark progress, the idle timeout should be configured via configuration property
ConsumerConfigConstants.SHARD_IDLE_INTERVAL_MILLIS. By default, shards won't be considered idle and watermark calculation will wait for newer records to arrive from all shards.Note that re-sharding of the Kinesis stream while an application (that relies on the Kinesis records for watermarking) is running can lead to incorrect late events. This depends on how shards are assigned to subtasks and applies regardless of whether watermarks are generated in the source or a downstream operator.
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.apache.flink.streaming.api.functions.source.SourceFunction
org.apache.flink.streaming.api.functions.source.SourceFunction.SourceContext<T extends Object>
-
-
Constructor Summary
Constructors Constructor Description FlinkKinesisConsumer(String stream, org.apache.flink.api.common.serialization.DeserializationSchema<T> deserializer, Properties configProps)Creates a new Flink Kinesis Consumer.FlinkKinesisConsumer(String stream, KinesisDeserializationSchema<T> deserializer, Properties configProps)Creates a new Flink Kinesis Consumer.FlinkKinesisConsumer(List<String> streams, KinesisDeserializationSchema<T> deserializer, Properties configProps)Creates a new Flink Kinesis Consumer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcancel()voidclose()protected KinesisDataFetcher<T>createFetcher(List<String> streams, org.apache.flink.streaming.api.functions.source.SourceFunction.SourceContext<T> sourceContext, org.apache.flink.api.common.functions.RuntimeContext runtimeContext, Properties configProps, KinesisDeserializationSchema<T> deserializationSchema)This method is exposed for tests that need to mock the KinesisDataFetcher in the consumer.org.apache.flink.streaming.api.functions.AssignerWithPeriodicWatermarks<T>getPeriodicWatermarkAssigner()org.apache.flink.api.common.typeinfo.TypeInformation<T>getProducedType()KinesisShardAssignergetShardAssigner()WatermarkTrackergetWatermarkTracker()voidinitializeState(org.apache.flink.runtime.state.FunctionInitializationContext context)voidrun(org.apache.flink.streaming.api.functions.source.SourceFunction.SourceContext<T> sourceContext)voidsetPeriodicWatermarkAssigner(org.apache.flink.streaming.api.functions.AssignerWithPeriodicWatermarks<T> periodicWatermarkAssigner)Set the assigner that will extract the timestamp fromFlinkKinesisConsumerand calculate the watermark.voidsetShardAssigner(KinesisShardAssigner shardAssigner)Provide a custom assigner to influence how shards are distributed over subtasks.voidsetWatermarkTracker(WatermarkTracker watermarkTracker)Set the global watermark tracker.voidsnapshotState(org.apache.flink.runtime.state.FunctionSnapshotContext context)
-
-
-
Constructor Detail
-
FlinkKinesisConsumer
public FlinkKinesisConsumer(String stream, org.apache.flink.api.common.serialization.DeserializationSchema<T> deserializer, Properties configProps)
Creates a new Flink Kinesis Consumer.The AWS credentials to be used, AWS region of the Kinesis streams, initial position to start streaming from are configured with a
Propertiesinstance.- Parameters:
stream- The single AWS Kinesis stream to read from.deserializer- The deserializer used to convert raw bytes of Kinesis records to Java objects (without key).configProps- The properties used to configure AWS credentials, AWS region, and initial starting position.
-
FlinkKinesisConsumer
public FlinkKinesisConsumer(String stream, KinesisDeserializationSchema<T> deserializer, Properties configProps)
Creates a new Flink Kinesis Consumer.The AWS credentials to be used, AWS region of the Kinesis streams, initial position to start streaming from are configured with a
Propertiesinstance.- Parameters:
stream- The single AWS Kinesis stream to read from.deserializer- The keyed deserializer used to convert raw bytes of Kinesis records to Java objects.configProps- The properties used to configure AWS credentials, AWS region, and initial starting position.
-
FlinkKinesisConsumer
public FlinkKinesisConsumer(List<String> streams, KinesisDeserializationSchema<T> deserializer, Properties configProps)
Creates a new Flink Kinesis Consumer.The AWS credentials to be used, AWS region of the Kinesis streams, initial position to start streaming from are configured with a
Propertiesinstance.- Parameters:
streams- The AWS Kinesis streams to read from.deserializer- The keyed deserializer used to convert raw bytes of Kinesis records to Java objects.configProps- The properties used to configure AWS credentials, AWS region, and initial starting position.
-
-
Method Detail
-
getShardAssigner
public KinesisShardAssigner getShardAssigner()
-
setShardAssigner
public void setShardAssigner(KinesisShardAssigner shardAssigner)
Provide a custom assigner to influence how shards are distributed over subtasks.- Parameters:
shardAssigner- shard assigner
-
getPeriodicWatermarkAssigner
public org.apache.flink.streaming.api.functions.AssignerWithPeriodicWatermarks<T> getPeriodicWatermarkAssigner()
-
setPeriodicWatermarkAssigner
public void setPeriodicWatermarkAssigner(org.apache.flink.streaming.api.functions.AssignerWithPeriodicWatermarks<T> periodicWatermarkAssigner)
Set the assigner that will extract the timestamp fromFlinkKinesisConsumerand calculate the watermark.- Parameters:
periodicWatermarkAssigner- periodic watermark assigner
-
getWatermarkTracker
public WatermarkTracker getWatermarkTracker()
-
setWatermarkTracker
public void setWatermarkTracker(WatermarkTracker watermarkTracker)
Set the global watermark tracker. When set, it will be used by the fetcher to align the shard consumers by event time.- Parameters:
watermarkTracker-
-
run
public void run(org.apache.flink.streaming.api.functions.source.SourceFunction.SourceContext<T> sourceContext) throws Exception
-
cancel
public void cancel()
- Specified by:
cancelin interfaceorg.apache.flink.streaming.api.functions.source.SourceFunction<T>
-
close
public void close() throws Exception- Specified by:
closein interfaceorg.apache.flink.api.common.functions.RichFunction- Overrides:
closein classorg.apache.flink.api.common.functions.AbstractRichFunction- Throws:
Exception
-
getProducedType
public org.apache.flink.api.common.typeinfo.TypeInformation<T> getProducedType()
- Specified by:
getProducedTypein interfaceorg.apache.flink.api.java.typeutils.ResultTypeQueryable<T>
-
initializeState
public void initializeState(org.apache.flink.runtime.state.FunctionInitializationContext context) throws Exception- Specified by:
initializeStatein interfaceorg.apache.flink.streaming.api.checkpoint.CheckpointedFunction- Throws:
Exception
-
snapshotState
public void snapshotState(org.apache.flink.runtime.state.FunctionSnapshotContext context) throws Exception- Specified by:
snapshotStatein interfaceorg.apache.flink.streaming.api.checkpoint.CheckpointedFunction- Throws:
Exception
-
createFetcher
protected KinesisDataFetcher<T> createFetcher(List<String> streams, org.apache.flink.streaming.api.functions.source.SourceFunction.SourceContext<T> sourceContext, org.apache.flink.api.common.functions.RuntimeContext runtimeContext, Properties configProps, KinesisDeserializationSchema<T> deserializationSchema)
This method is exposed for tests that need to mock the KinesisDataFetcher in the consumer.
-
-