//snippet-sourcedescription:[PutBatchRecords.java demonstrates how to write multiple data records into a delivery stream.]
//snippet-keyword:[AWS SDK for Java v2]
//snippet-keyword:[Code Sample]
//snippet-service:[Amazon Kinesis Data Firehose]
//snippet-sourcetype:[full-example]
//snippet-sourcedate:[11/04/2020]
//snippet-sourceauthor:[scmacdon - aws]

/*
   Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
   SPDX-License-Identifier: Apache-2.0
*/

package com.example.firehose;

// snippet-start:[firehose.java2.put_batch_records.import]
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.firehose.FirehoseClient;
import software.amazon.awssdk.services.firehose.model.Record;
import software.amazon.awssdk.services.firehose.model.PutRecordBatchRequest;
import software.amazon.awssdk.services.firehose.model.PutRecordBatchResponse;
import software.amazon.awssdk.services.firehose.model.FirehoseException;
import software.amazon.awssdk.core.SdkBytes;
import java.util.ArrayList;
import java.util.List;
// snippet-end:[firehose.java2.put_batch_records.import]


public class PutBatchRecords {

    public static void main(String[] args) {

        final String USAGE = "\n" +
                "Usage:\n" +
                "    PutBatchRecords <streamName> \n\n" +
                "Where:\n" +
                "    streamName - the data stream name \n" ;

        if (args.length != 1) {
            System.out.println(USAGE);
            System.exit(1);
        }

        String streamName = args[0];
        Region region = Region.US_WEST_2;
        FirehoseClient firehoseClient = FirehoseClient.builder()
                .region(region)
                .build();

        addStockTradeData(firehoseClient, streamName);
        firehoseClient.close();
    }

    // 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());

        } catch (FirehoseException | InterruptedException e) {
            System.out.println(e.getLocalizedMessage());
            System.exit(1);
        }
        // snippet-end:[firehose.java2.put_batch_records.main]
    }
}

@@ -113,16 +113,64 @@
 y stream
+ and check each record using the response object
 .%5D%0D%0A//sn
@@ -1020,24 +1020,108 @@
 Exception;%0D%0A
+import software.amazon.awssdk.services.firehose.model.PutRecordBatchResponseEntry;%0D%0A
 import softw
@@ -3401,24 +3401,793 @@
 size());%0D%0A%0D%0A
+            // Check the details of all records in this batch operation.%0D%0A            String errorMsg =%22%22;%0D%0A            String errorCode = %22%22;%0D%0A            List%3CPutRecordBatchResponseEntry%3E results = recordResponse.requestResponses();%0D%0A            for (PutRecordBatchResponseEntry result: results) %7B%0D%0A%0D%0A                // Returns null if there is no error.%0D%0A                errorCode = result.errorCode();%0D%0A                if (errorCode == null) %7B%0D%0A                    System.out.println(%22Record %22+result.recordId() + %22 was successfully added!%22);%0D%0A                %7D else %7B%0D%0A                    errorMsg = result.errorMessage();%0D%0A                    System.out.println(%22Error code for record ID : %22+result.recordId() + %22 is %22+errorMsg);%0D%0A                %7D%0D%0A        %7D%0D%0A%0D%0A
         %7D ca

Update PutBatchRecords.java