Writing to Kinesis Data Firehose Using the Amazon SDK
You can use the Amazon Kinesis Data Firehose API to send data to a
Kinesis Data Firehose delivery stream using the Amazon
SDK for Java
These examples do not represent production-ready code, in that they do not check for all possible exceptions, or account for all possible security or performance considerations.
The Kinesis Data Firehose API offers two operations for sending data to your delivery stream:
PutRecord and PutRecordBatch. PutRecord()
sends one
data record within one call and PutRecordBatch()
can send multiple data records
within one call.
Single Write Operations Using PutRecord
Putting data requires only the Kinesis Data Firehose delivery stream name and a byte buffer (<=1000 KB). Because Kinesis Data Firehose batches multiple records before loading the file into Amazon S3, you may want to add a record separator. To put data one record at a time into a delivery stream, use the following code:
PutRecordRequest putRecordRequest = new PutRecordRequest(); putRecordRequest.setDeliveryStreamName(deliveryStreamName); String data = line + "\n"; Record record = new Record().withData(ByteBuffer.wrap(data.getBytes())); putRecordRequest.setRecord(record); // Put record into the DeliveryStream firehoseClient.putRecord(putRecordRequest);
For more code context, see the sample code included in the Amazon SDK. For information about request and response syntax, see the relevant topic in Amazon Kinesis Data Firehose API Operations.
Batch Write Operations Using PutRecordBatch
Putting data requires only the Kinesis Data Firehose delivery stream name and a list of records. Because Kinesis Data Firehose batches multiple records before loading the file into Amazon S3, you may want to add a record separator. To put data records in batches into a delivery stream, use the following code:
PutRecordBatchRequest putRecordBatchRequest = new PutRecordBatchRequest(); putRecordBatchRequest.setDeliveryStreamName(deliveryStreamName); putRecordBatchRequest.setRecords(recordList); // Put Record Batch records. Max No.Of Records we can put in a // single put record batch request is 500 firehoseClient.putRecordBatch(putRecordBatchRequest); recordList.clear();
For more code context, see the sample code included in the Amazon SDK. For information about request and response syntax, see the relevant topic in Amazon Kinesis Data Firehose API Operations.