@Experimental(value=SOURCE_SINK) public final class KinesisIO extends java.lang.Object
PTransform
s for reading from Kinesis
streams.
Note that KinesisIO.Write is based on the Kinesis Producer Library which does not yet have an
update to be compatible with AWS SDK for Java version 2 so for now the version in org.apache.beam.sdk.io.kinesis
should be used for writing to Kinesis.
Example usage:
p.apply(KinesisIO.read()
.withStreamName("streamName")
.withInitialPositionInStream(InitialPositionInStream.LATEST)
.withAWSClientsProvider("AWS_KEY", _"AWS_SECRET", STREAM_REGION)
.apply( ... ) // other transformations
As you can see you need to provide 3 things:
InitialPositionInStream.LATEST
- reading will begin from end of the stream
InitialPositionInStream.TRIM_HORIZON
- reading will begin at the very
beginning of the stream
KinesisClient
and CloudWatchClient
clients:
In case when you want to set up KinesisClient
or CloudWatchClient
client by
your own (for example if you're using more sophisticated authorization methods like Amazon STS,
etc.) you can do it by implementing AWSClientsProvider
class:
public class MyCustomKinesisClientProvider implements AWSClientsProvider {
public KinesisClient getKinesisClient() {
// set up your client here
}
public CloudWatchClient getCloudWatchClient() {
// set up your client here
}
}
Usage is pretty straightforward:
p.apply(KinesisIO.read()
.withStreamName("streamName")
.withInitialPositionInStream(InitialPositionInStream.LATEST)
.withAWSClientsProvider(new MyCustomKinesisClientProvider())
.apply( ... ) // other transformations
There’s also possibility to start reading using arbitrary point in time - in this case you
need to provide Instant
object:
p.apply(KinesisIO.read()
.withStreamName("streamName")
.withInitialTimestampInStream(instant)
.withAWSClientsProvider(new MyCustomKinesisClientProvider())
.apply( ... ) // other transformations
Kinesis IO uses ArrivalTimeWatermarkPolicy by default. To use Processing time as event time:
p.apply(KinesisIO.read()
.withStreamName("streamName")
.withInitialPositionInStream(InitialPositionInStream.LATEST)
.withProcessingTimeWatermarkPolicy())
It is also possible to specify a custom watermark policy to control watermark computation. Below is an example
// custom policy
class MyCustomPolicy implements WatermarkPolicy {
private WatermarkPolicyFactory.CustomWatermarkPolicy customWatermarkPolicy;
MyCustomPolicy() {
this.customWatermarkPolicy = new WatermarkPolicyFactory.CustomWatermarkPolicy(WatermarkParameters.create());
}
public Instant getWatermark() {
return customWatermarkPolicy.getWatermark();
}
public void update(KinesisRecord record) {
customWatermarkPolicy.update(record);
}
}
// custom factory
class MyCustomPolicyFactory implements WatermarkPolicyFactory {
public WatermarkPolicy createWatermarkPolicy() {
return new MyCustomPolicy();
}
}
p.apply(KinesisIO.read()
.withStreamName("streamName")
.withInitialPositionInStream(InitialPositionInStream.LATEST)
.withCustomWatermarkPolicy(new MyCustomPolicyFactory())
By default Kinesis IO will poll the Kinesis getRecords() API as fast as possible which may lead to excessive read throttling. To limit the rate of getRecords() calls you can set a rate limit policy. For example, the default fixed delay policy will limit the rate to one API call per second per shard:
p.apply(KinesisIO.read()
.withStreamName("streamName")
.withInitialPositionInStream(InitialPositionInStream.LATEST)
.withFixedDelayRateLimitPolicy())
You can also use a fixed delay policy with a specified delay interval, for example:
p.apply(KinesisIO.read()
.withStreamName("streamName")
.withInitialPositionInStream(InitialPositionInStream.LATEST)
.withFixedDelayRateLimitPolicy(Duration.millis(500))
If you need to change the polling interval of a Kinesis pipeline at runtime, for example to compensate for adding and removing additional consumers to the stream, then you can supply the delay interval as a function so that you can obtain the current delay interval from some external source:
p.apply(KinesisIO.read()
.withStreamName("streamName")
.withInitialPositionInStream(InitialPositionInStream.LATEST)
.withDynamicDelayRateLimitPolicy(() -> Duration.millis(<some delay interval>))
Finally, you can create a custom rate limit policy that responds to successful read calls and/or read throttling exceptions with your own rate-limiting logic:
// custom policy
public class MyCustomPolicy implements RateLimitPolicy {
public void onSuccess(List<KinesisRecord> records) throws InterruptedException {
// handle successful getRecords() call
}
public void onThrottle(KinesisClientThrottledException e) throws InterruptedException {
// handle Kinesis read throttling exception
}
}
// custom factory
class MyCustomPolicyFactory implements RateLimitPolicyFactory {
public RateLimitPolicy getRateLimitPolicy() {
return new MyCustomPolicy();
}
}
p.apply(KinesisIO.read()
.withStreamName("streamName")
.withInitialPositionInStream(InitialPositionInStream.LATEST)
.withCustomRateLimitPolicy(new MyCustomPolicyFactory())
Modifier and Type | Class and Description |
---|---|
static class |
KinesisIO.Read
Implementation of
read() . |
Constructor and Description |
---|
KinesisIO() |
Modifier and Type | Method and Description |
---|---|
static KinesisIO.Read |
read()
Returns a new
KinesisIO.Read transform for reading from Kinesis. |
public static KinesisIO.Read read()
KinesisIO.Read
transform for reading from Kinesis.