Kinesis Connector
Kinesis I/O: Quickstart
The Samza Kinesis connector allows you to interact with Amazon Kinesis Data Streams,
Amazon’s data streaming service. The hello-samza
project includes an example of processing Kinesis streams using Samza. Here is the complete source code and configs.
You can build and run this example using this tutorial.
Data Format
Like a Kafka topic, a Kinesis stream can have multiple shards with producers and consumers. Each message consumed from the stream is an instance of a Kinesis Record. Samza’s KinesisSystemConsumer wraps the Record into a KinesisIncomingMessageEnvelope.
Consuming from Kinesis
Basic Configuration
Here is the required configuration for consuming messages from Kinesis.
// Define a Kinesis system factory with your identifier. eg: kinesis-system
systems.kinesis-system.samza.factory=org.apache.samza.system.kinesis.KinesisSystemFactory
// Kinesis consumer works with only AllSspToSingleTaskGrouperFactory
job.systemstreampartition.grouper.factory=org.apache.samza.container.grouper.stream.AllSspToSingleTaskGrouperFactory
// Define your streams
task.inputs=kinesis-system.input0
// Define required properties for your streams
systems.kinesis-system.streams.input0.aws.region=YOUR-STREAM-REGION
systems.kinesis-system.streams.input0.aws.accessKey=YOUR-ACCESS_KEY
sensitive.systems.kinesis-system.streams.input0.aws.secretKey=YOUR-SECRET-KEY
Coordination
The Kinesis system consumer does not rely on Samza’s coordination mechanism. Instead, it uses the Kinesis client library (KCL) for coordination and distributing available shards among available instances. Hence, you should
set your grouper
configuration to AllSspToSingleTaskGrouperFactory
.
job.systemstreampartition.grouper.factory=org.apache.samza.container.grouper.stream.AllSspToSingleTaskGrouperFactory
Security
Each Kinesis stream in a given AWS region can be accessed by providing an access key. An Access key consists of two parts: an access key ID (for example, AKIAIOSFODNN7EXAMPLE
) and a secret access key (for example, wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
) which you can use to send programmatic requests to AWS.
systems.kinesis-system.streams.input0.aws.region=YOUR-STREAM-REGION
systems.kinesis-system.streams.input0.aws.accessKey=YOUR-ACCESS_KEY
sensitive.systems.kinesis-system.streams.input0.aws.secretKey=YOUR-SECRET-KEY
Advanced Configuration
Kinesis Client Library Configs
Samza Kinesis Connector uses the Kinesis Client Library (KCL) to access the Kinesis data streams. You can set any KCL Configuration for a stream by configuring it with the systems.system-name.streams.stream-name.aws.kcl.* prefix.
systems.system-name.streams.stream-name.aws.kcl.CONFIG-PARAM=CONFIG-VALUE
As an example, the below configuration is equivalent to invoking kclClient#WithTableName(myTable)
on the KCL instance.
systems.system-name.streams.stream-name.aws.kcl.TableName=myTable
AWS Client configs
Samza allows you to specify any AWS client configs to connect to your Kinesis instance.
You can configure any AWS client configuration with the systems.your-system-name.aws.clientConfig.*
prefix.
systems.system-name.aws.clientConfig.CONFIG-PARAM=CONFIG-VALUE
As an example, to set the proxy host and proxy port to be used by the Kinesis Client:
systems.system-name.aws.clientConfig.ProxyHost=my-proxy-host.com
systems.system-name.aws.clientConfig.ProxyPort=my-proxy-port
Resetting Offsets
Unlike other connectors where Samza stores and manages checkpointed offsets, Kinesis checkpoints are stored in a DynamoDB table. These checkpoints are stored and managed by the KCL library internally. You can reset the checkpoints by configuring a different name for the DynamoDB table.
// change the TableName to a unique name to reset checkpoints.
systems.kinesis-system.streams.input0.aws.kcl.TableName=my-app-table-name
When you reset checkpoints, you can configure your job to start consuming from either the earliest or latest offset in the stream.
// set the starting position to either TRIM_HORIZON (oldest) or LATEST (latest)
systems.kinesis-system.streams.input0.aws.kcl.InitialPositionInStream=LATEST
Alternately, if you want to start from a particular offset in the Kinesis stream, you can login to the AWS console and edit the offsets in your DynamoDB Table. By default, the table-name has the following format: “<job name>-<job id>-<kinesis stream>”.
Known Limitations
The following limitations apply to Samza jobs consuming from Kinesis streams :
- Stateful processing (eg: windows or joins) is not supported on Kinesis streams. However, you can accomplish this by chaining two Samza jobs where the first job reads from Kinesis and sends to Kafka while the second job processes the data from Kafka.
- Kinesis streams cannot be configured as bootstrap or broadcast streams.
- Kinesis streams must be used only with the AllSspToSingleTaskGrouperFactory as the Kinesis consumer does the partition management by itself. No other grouper is currently supported.
- A Samza job that consumes from Kinesis cannot consume from any other input source. However, you can send your results to any destination (eg: Kafka, EventHubs), and have another Samza job consume them.
Producing to Kinesis
The KinesisSystemProducer for Samza is not yet implemented.