- AWS Code Sample demonstrates how to read multiple data records from an Amazon Kinesis data stream.

//snippet-sourceauthor:scmacdon - AWS] /* * Copyright, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ package com.example.kinesis; import; import; import; import; import; import; import; import; import; import; import; import java.util.ArrayList; import java.util.List; /** * Demonstrates how to read data from a Kinesis data stream. Before running this Java code example, populate a data stream * by running the StockTradesWriter example. Then you can use that populated data stream for this example. */ public class GetRecords { public static void main(String[] args) { final String USAGE = "\n" + "Usage:\n" + " GetRecords <streamName>\n\n" + "Where:\n" + " streamName - The Kinesis data stream to read from (i.e., StockTradeStream)\n\n" + "Example:\n" + " GetRecords streamName\n"; if (args.length < 1) { System.out.println(USAGE); System.exit(1); } String streamName = args[0]; Region region = Region.US_EAST_1; KinesisClient kinesisClient = KinesisClient.builder() .region(region) .build(); getStockTrades(kinesisClient,streamName); } public static void getStockTrades(KinesisClient kinesisClient, String streamName) { String shardIterator; String lastShardId = null; // Retrieve the shards from a data stream DescribeStreamRequest describeStreamRequest = DescribeStreamRequest.builder() .streamName(streamName) .build(); List<Shard> shards = new ArrayList<>(); DescribeStreamResponse streamRes; do { streamRes = kinesisClient.describeStream(describeStreamRequest); shards.addAll(streamRes.streamDescription().shards()); if (shards.size() > 0) { lastShardId = shards.get(shards.size() - 1).shardId(); } } while (streamRes.streamDescription().hasMoreShards()); GetShardIteratorRequest itReq = GetShardIteratorRequest.builder() .streamName(streamName) .shardIteratorType("TRIM_HORIZON") .shardId(shards.get(0).shardId()) .build(); GetShardIteratorResponse shardIteratorResult = kinesisClient.getShardIterator(itReq); shardIterator = shardIteratorResult.shardIterator(); // Continuously read data records from a shard List<Record> records; // Create a GetRecordsRequest with the existing shardIterator, // and set maximum records to return to 1000 GetRecordsRequest recordsRequest = GetRecordsRequest.builder() .shardIterator(shardIterator) .limit(1000) .build(); GetRecordsResponse result = kinesisClient.getRecords(recordsRequest); // Put result into a record list, result might be empty records = result.records(); // Print records for (Record record : records) { SdkBytes byteBuffer =; System.out.println(String.format("Seq No: %s - %s", record.sequenceNumber(), new String(byteBuffer.asByteArray()))); } } }

Sample Details

Service: kinesis

Last tested: 3/26/2020

Type: full-example