Search in sources :

Example 1 with PutRecordBatchRequest

use of software.amazon.awssdk.services.firehose.model.PutRecordBatchRequest in project flink by apache.

the class KinesisFirehoseSinkWriter method submitRequestEntries.

@Override
protected void submitRequestEntries(List<Record> requestEntries, Consumer<List<Record>> requestResult) {
    PutRecordBatchRequest batchRequest = PutRecordBatchRequest.builder().records(requestEntries).deliveryStreamName(deliveryStreamName).build();
    LOG.trace("Request to submit {} entries to KDF using KDF Sink.", requestEntries.size());
    CompletableFuture<PutRecordBatchResponse> future = firehoseClient.putRecordBatch(batchRequest);
    future.whenComplete((response, err) -> {
        if (err != null) {
            handleFullyFailedRequest(err, requestEntries, requestResult);
        } else if (response.failedPutCount() > 0) {
            handlePartiallyFailedRequest(response, requestEntries, requestResult);
        } else {
            requestResult.accept(Collections.emptyList());
        }
    });
}
Also used : PutRecordBatchResponse(software.amazon.awssdk.services.firehose.model.PutRecordBatchResponse) PutRecordBatchRequest(software.amazon.awssdk.services.firehose.model.PutRecordBatchRequest)

Example 2 with PutRecordBatchRequest

use of software.amazon.awssdk.services.firehose.model.PutRecordBatchRequest in project aws-doc-sdk-examples by awsdocs.

the class PutBatchRecords method addStockTradeData.

// snippet-start:[firehose.java2.put_batch_records.main]
public static void addStockTradeData(FirehoseClient firehoseClient, String streamName) {
    List<Record> recordList = new ArrayList<>();
    try {
        // Repeatedly send stock trades with a 100 milliseconds wait in between
        StockTradeGenerator stockTradeGenerator = new StockTradeGenerator();
        int index = 100;
        // Populate the list with StockTrade data
        for (int x = 0; x < index; x++) {
            StockTrade trade = stockTradeGenerator.getRandomTrade();
            byte[] bytes = trade.toJsonAsBytes();
            Record myRecord = Record.builder().data(SdkBytes.fromByteArray(bytes)).build();
            System.out.println("Adding trade: " + trade.toString());
            recordList.add(myRecord);
            Thread.sleep(100);
        }
        PutRecordBatchRequest recordBatchRequest = PutRecordBatchRequest.builder().deliveryStreamName(streamName).records(recordList).build();
        PutRecordBatchResponse recordResponse = firehoseClient.putRecordBatch(recordBatchRequest);
        System.out.println("The number of records added is: " + recordResponse.requestResponses().size());
        // Check the details of all records in this batch operation.
        String errorMsg = "";
        String errorCode = "";
        List<PutRecordBatchResponseEntry> results = recordResponse.requestResponses();
        for (PutRecordBatchResponseEntry result : results) {
            // Returns null if there is no error.
            errorCode = result.errorCode();
            if (errorCode == null) {
                System.out.println("Record " + result.recordId() + " was successfully added!");
            } else {
                errorMsg = result.errorMessage();
                System.out.println("Error code for record ID : " + result.recordId() + " is " + errorMsg);
            }
        }
    } catch (FirehoseException | InterruptedException e) {
        System.out.println(e.getLocalizedMessage());
        System.exit(1);
    }
}
Also used : ArrayList(java.util.ArrayList) PutRecordBatchResponse(software.amazon.awssdk.services.firehose.model.PutRecordBatchResponse) FirehoseException(software.amazon.awssdk.services.firehose.model.FirehoseException) Record(software.amazon.awssdk.services.firehose.model.Record) PutRecordBatchRequest(software.amazon.awssdk.services.firehose.model.PutRecordBatchRequest) PutRecordBatchResponseEntry(software.amazon.awssdk.services.firehose.model.PutRecordBatchResponseEntry)

Aggregations

PutRecordBatchRequest (software.amazon.awssdk.services.firehose.model.PutRecordBatchRequest)2 PutRecordBatchResponse (software.amazon.awssdk.services.firehose.model.PutRecordBatchResponse)2 ArrayList (java.util.ArrayList)1 FirehoseException (software.amazon.awssdk.services.firehose.model.FirehoseException)1 PutRecordBatchResponseEntry (software.amazon.awssdk.services.firehose.model.PutRecordBatchResponseEntry)1 Record (software.amazon.awssdk.services.firehose.model.Record)1