public class KinesisProducer extends Object implements IKinesisProducer
Use a single instance within the application whenever possible:
addUserRecord(java.lang.String, java.lang.String, java.nio.ByteBuffer)
methods are thread safe, and be called
concurrently.Modifier | Constructor and Description |
---|---|
|
KinesisProducer()
Start up a KinesisProducer instance.
|
protected |
KinesisProducer(File inPipe,
File outPipe)
Connect to a running daemon.
|
|
KinesisProducer(KinesisProducerConfiguration config)
Start up a KinesisProducer instance.
|
Modifier and Type | Method and Description |
---|---|
com.google.common.util.concurrent.ListenableFuture<UserRecordResult> |
addUserRecord(String stream,
String partitionKey,
ByteBuffer data)
Put a record asynchronously.
|
com.google.common.util.concurrent.ListenableFuture<UserRecordResult> |
addUserRecord(String stream,
String partitionKey,
String explicitHashKey,
ByteBuffer data)
Put a record asynchronously.
|
com.google.common.util.concurrent.ListenableFuture<UserRecordResult> |
addUserRecord(String stream,
String partitionKey,
String explicitHashKey,
ByteBuffer data,
com.amazonaws.services.schemaregistry.common.Schema schema) |
com.google.common.util.concurrent.ListenableFuture<UserRecordResult> |
addUserRecord(UserRecord userRecord)
Put a record asynchronously.
|
void |
destroy()
Immediately kill the child process.
|
void |
flush()
Instruct the child process to perform a flush, sending some of the
records it has buffered.
|
void |
flush(String stream)
Instruct the child process to perform a flush, sending some of the
records it has buffered for the specified stream.
|
void |
flushSync()
Instructs the child process to flush all records and waits until all
records are complete (either succeeding or failing).
|
List<Metric> |
getMetrics()
Get metrics from the KPL.
|
List<Metric> |
getMetrics(int windowSeconds)
Get metrics from the KPL.
|
List<Metric> |
getMetrics(String metricName)
Get metrics from the KPL.
|
List<Metric> |
getMetrics(String metricName,
int windowSeconds)
Get metrics from the KPL.
|
long |
getOldestRecordTimeInMillis()
Get the time in millis for the oldest record currently waiting in kpl to be processed for sending to kinesis
endpoint.
|
int |
getOutstandingRecordsCount()
Get the number of unfinished records currently being processed.
|
public KinesisProducer(KinesisProducerConfiguration config)
Since this creates a child process, it is fairly expensive. Avoid creating more than one per application, unless putting to multiple regions at the same time. All streams in the same region can share the same instance.
All methods in KinesisProducer are thread-safe.
config
- Configuration for the KinesisProducer. See the docs for that
class for details.KinesisProducerConfiguration
public KinesisProducer()
Since this creates a child process, it is fairly expensive. Avoid creating more than one per application, unless putting to multiple regions at the same time. All streams in the same region can share the same instance.
The KPL will use a set of default configurations. You can set custom
configuration using the constructor that takes a KinesisProducerConfiguration
object.
All methods in KinesisProducer are thread-safe.
public com.google.common.util.concurrent.ListenableFuture<UserRecordResult> addUserRecord(String stream, String partitionKey, ByteBuffer data)
ListenableFuture
is returned that
can be used to retrieve the result, either by polling or by registering a
callback.
The return value can be disregarded if you do not wish to process the result. Under the covers, the KPL will automatically re-attempt puts in case of transient errors (including throttling). A failed result is generally returned only if an irrecoverable error is detected (e.g. trying to put to a stream that doesn't exist), or if the record expires.
Thread safe.
To add a listener to the future:
ListenableFuture<PutRecordResult> f = myKinesisProducer.addUserRecord(...);
com.google.common.util.concurrent.Futures.addCallback(f, callback, executor);
where callback
is an instance of
FutureCallback
and
executor
is an instance of
Executor
.
Important:
If long-running tasks are performed in the callbacks, it is recommended that a custom executor be provided when registering callbacks to ensure that there are enough threads to achieve the desired level of parallelism. By default, the KPL will use an internal thread pool to execute callbacks, but this pool may not have a sufficient number of threads if a large number is desired.
Another option would be to hand the result off to a different component for processing and keep the callback routine fast.
addUserRecord
in interface IKinesisProducer
stream
- Stream to put to.partitionKey
- Partition key. Length must be at least one, and at most 256
(inclusive).data
- Binary data of the record. Maximum size 1MiB.IllegalArgumentException
- if input does not meet stated constraintsDaemonException
- if the child process is deadListenableFuture
,
UserRecordResult
,
KinesisProducerConfiguration.setRecordTtl(long)
,
UserRecordFailedException
public com.google.common.util.concurrent.ListenableFuture<UserRecordResult> addUserRecord(UserRecord userRecord)
ListenableFuture
is returned that
can be used to retrieve the result, either by polling or by registering a
callback.
The return value can be disregarded if you do not wish to process the result. Under the covers, the KPL will automatically re-attempt puts in case of transient errors (including throttling). A failed result is generally returned only if an irrecoverable error is detected (e.g. trying to put to a stream that doesn't exist), or if the record expires.
Thread safe.
To add a listener to the future:
ListenableFuture<PutRecordResult> f = myKinesisProducer.addUserRecord(...);
com.google.common.util.concurrent.Futures.addCallback(f, callback, executor);
where callback
is an instance of
FutureCallback
and
executor
is an instance of
Executor
.
Important:
If long-running tasks are performed in the callbacks, it is recommended that a custom executor be provided when registering callbacks to ensure that there are enough threads to achieve the desired level of parallelism. By default, the KPL will use an internal thread pool to execute callbacks, but this pool may not have a sufficient number of threads if a large number is desired.
Another option would be to hand the result off to a different component for processing and keep the callback routine fast.
addUserRecord
in interface IKinesisProducer
userRecord
- All data necessary to write to the stream.IllegalArgumentException
- if input does not meet stated constraintsDaemonException
- if the child process is deadListenableFuture
,
UserRecordResult
,
KinesisProducerConfiguration.setRecordTtl(long)
,
UserRecordFailedException
public com.google.common.util.concurrent.ListenableFuture<UserRecordResult> addUserRecord(String stream, String partitionKey, String explicitHashKey, ByteBuffer data)
ListenableFuture
is returned that
can be used to retrieve the result, either by polling or by registering a
callback.
The return value can be disregarded if you do not wish to process the result. Under the covers, the KPL will automatically reattempt puts in case of transient errors (including throttling). A failed result is generally returned only if an irrecoverable error is detected (e.g. trying to put to a stream that doesn't exist), or if the record expires.
Thread safe.
To add a listener to the future:
ListenableFuture<PutRecordResult> f = myKinesisProducer.addUserRecord(...);
com.google.common.util.concurrent.Futures.addCallback(f, callback, executor);
where callback
is an instance of
FutureCallback
and
executor
is an instance of
Executor
.
Important:
If long-running tasks are performed in the callbacks, it is recommended that a custom executor be provided when registering callbacks to ensure that there are enough threads to achieve the desired level of parallelism. By default, the KPL will use an internal thread pool to execute callbacks, but this pool may not have a sufficient number of threads if a large number is desired.
Another option would be to hand the result off to a different component for processing and keep the callback routine fast.
addUserRecord
in interface IKinesisProducer
stream
- Stream to put to.partitionKey
- Partition key. Length must be at least one, and at most 256
(inclusive).explicitHashKey
- The hash value used to explicitly determine the shard the data
record is assigned to by overriding the partition key hash.
Must be a valid string representation of a positive integer
with value between 0 and 2^128 - 1 (inclusive).data
- Binary data of the record. Maximum size 1MiB.IllegalArgumentException
- if input does not meet stated constraintsDaemonException
- if the child process is deadListenableFuture
,
UserRecordResult
,
KinesisProducerConfiguration.setRecordTtl(long)
,
UserRecordFailedException
public com.google.common.util.concurrent.ListenableFuture<UserRecordResult> addUserRecord(String stream, String partitionKey, String explicitHashKey, ByteBuffer data, com.amazonaws.services.schemaregistry.common.Schema schema)
addUserRecord
in interface IKinesisProducer
public int getOutstandingRecordsCount()
This is equal to the number of futures returned from addUserRecord(java.lang.String, java.lang.String, java.nio.ByteBuffer)
that have not finished.
getOutstandingRecordsCount
in interface IKinesisProducer
public long getOldestRecordTimeInMillis()
This is time in millis from the oldest future submitted from addUserRecord(java.lang.String, java.lang.String, java.nio.ByteBuffer)
that have not finished. This returns 0 if there are no pending records in processing state.
getOldestRecordTimeInMillis
in interface IKinesisProducer
public List<Metric> getMetrics(String metricName, int windowSeconds) throws InterruptedException, ExecutionException
The KPL computes and buffers useful metrics. Use this method to retrieve them. The same metrics are also uploaded to CloudWatch (unless disabled).
Multiple metrics exist for the same name, each with a different list of dimensions (e.g. stream name). This method will fetch all metrics with the provided name.
See the stand-alone metrics documentation for details about individual metrics.
This method is synchronous and will block while the data is being retrieved.
getMetrics
in interface IKinesisProducer
metricName
- Name of the metrics to fetch.windowSeconds
- Fetch data from the last N seconds. The KPL maintains data at
per second granularity for the past minute. To get total
cumulative data since the start of the program, use the
overloads that do not take this argument.ExecutionException
- If an error occurred while fetching metrics from the child
process.InterruptedException
- If the thread is interrupted while waiting for the response
from the child process.Metric
public List<Metric> getMetrics(String metricName) throws InterruptedException, ExecutionException
The KPL computes and buffers useful metrics. Use this method to retrieve them. The same metrics are also uploaded to CloudWatch (unless disabled).
Multiple metrics exist for the same name, each with a different list of dimensions (e.g. stream name). This method will fetch all metrics with the provided name.
The retrieved data represents cumulative statistics since the start of
the program. To get data from a smaller window, use
getMetrics(String, int)
.
See the stand-alone metrics documentation for details about individual metrics.
This method is synchronous and will block while the data is being retrieved.
getMetrics
in interface IKinesisProducer
metricName
- Name of the metrics to fetch.ExecutionException
- If an error occurred while fetching metrics from the child
process.InterruptedException
- If the thread is interrupted while waiting for the response
from the child process.Metric
public List<Metric> getMetrics() throws InterruptedException, ExecutionException
The KPL computes and buffers useful metrics. Use this method to retrieve them. The same metrics are also uploaded to CloudWatch (unless disabled).
This method fetches all metrics available. To fetch only metrics with a
given name, use getMetrics(String)
.
The retrieved data represents cumulative statistics since the start of
the program. To get data from a smaller window, use
getMetrics(int)
.
See the stand-alone metrics documentation for details about individual metrics.
This method is synchronous and will block while the data is being retrieved.
getMetrics
in interface IKinesisProducer
ExecutionException
- If an error occurred while fetching metrics from the child
process.InterruptedException
- If the thread is interrupted while waiting for the response
from the child process.Metric
public List<Metric> getMetrics(int windowSeconds) throws InterruptedException, ExecutionException
The KPL computes and buffers useful metrics. Use this method to retrieve them. The same metrics are also uploaded to CloudWatch (unless disabled).
This method fetches all metrics available. To fetch only metrics with a
given name, use getMetrics(String, int)
.
See the stand-alone metrics documentation for details about individual metrics.
This method is synchronous and will block while the data is being retrieved.
getMetrics
in interface IKinesisProducer
windowSeconds
- Fetch data from the last N seconds. The KPL maintains data at
per second granularity for the past minute. To get total
cumulative data since the start of the program, use the
overloads that do not take this argument.ExecutionException
- If an error occurred while fetching metrics from the child
process.InterruptedException
- If the thread is interrupted while waiting for the response
from the child process.Metric
public void destroy()
To perform a graceful shutdown instead, there are several options:
flush()
and wait (perhaps with a time limit) for all
futures. If you were sending a very high volume of data you may need to
call flush multiple times to clear all buffers.getOutstandingRecordsCount()
until it returns 0.flushSync()
, which blocks until completion.destroy
in interface IKinesisProducer
public void flush(String stream)
This does not guarantee that all buffered records will be sent, only that
most of them will; to flush all records and wait for completion, use
flushSync()
.
This method returns immediately without blocking.
flush
in interface IKinesisProducer
stream
- Stream to flushDaemonException
- if the child process is deadpublic void flush()
This does not guarantee that all buffered records will be sent, only that
most of them will; to flush all records and wait for completion, use
flushSync()
.
This method returns immediately without blocking.
flush
in interface IKinesisProducer
DaemonException
- if the child process is deadpublic void flushSync()
The wait includes any retries that need to be performed. Depending on your configuration of record TTL and request timeout, this can potentially take a long time if the library is having trouble delivering records to the backend, for example due to network problems.
This is useful if you need to shutdown your application and want to make sure all records are delivered before doing so.
flushSync
in interface IKinesisProducer
DaemonException
- if the child process is deadKinesisProducerConfiguration.setRecordTtl(long)
,
KinesisProducerConfiguration.setRequestTimeout(long)
Copyright © 2021. All rights reserved.