Search in sources :

Example 1 with GetShardIteratorRequest

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

the class GetRecords method getStockTrades.

// snippet-start:[kinesis.java2.getrecord.main]
public static void getStockTrades(KinesisClient kinesisClient, String streamName) {
    String shardIterator;
    String lastShardId = null;
    // Retrieve the Shards from a 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(lastShardId).build();
    GetShardIteratorResponse shardIteratorResult = kinesisClient.getShardIterator(itReq);
    shardIterator = shardIteratorResult.shardIterator();
    // Continuously read data records from shard.
    List<Record> records;
    // Create new GetRecordsRequest with existing shardIterator.
    // Set maximum records to return to 1000.
    GetRecordsRequest recordsRequest = GetRecordsRequest.builder().shardIterator(shardIterator).limit(1000).build();
    GetRecordsResponse result = kinesisClient.getRecords(recordsRequest);
    // Put result into record list. Result may be empty.
    records = result.records();
    // Print records
    for (Record record : records) {
        SdkBytes byteBuffer = record.data();
        System.out.println(String.format("Seq No: %s - %s", record.sequenceNumber(), new String(byteBuffer.asByteArray())));
    }
}
Also used : ArrayList(java.util.ArrayList) GetShardIteratorRequest(software.amazon.awssdk.services.kinesis.model.GetShardIteratorRequest) GetRecordsResponse(software.amazon.awssdk.services.kinesis.model.GetRecordsResponse) DescribeStreamResponse(software.amazon.awssdk.services.kinesis.model.DescribeStreamResponse) DescribeStreamRequest(software.amazon.awssdk.services.kinesis.model.DescribeStreamRequest) GetRecordsRequest(software.amazon.awssdk.services.kinesis.model.GetRecordsRequest) SdkBytes(software.amazon.awssdk.core.SdkBytes) GetShardIteratorResponse(software.amazon.awssdk.services.kinesis.model.GetShardIteratorResponse) Record(software.amazon.awssdk.services.kinesis.model.Record) Shard(software.amazon.awssdk.services.kinesis.model.Shard)

Example 2 with GetShardIteratorRequest

use of software.amazon.awssdk.services.kinesis.model.GetShardIteratorRequest in project beam by apache.

the class SimplifiedKinesisClientTest method shouldHandleGetShardIteratorError.

private void shouldHandleGetShardIteratorError(Exception thrownException, Class<? extends Exception> expectedExceptionClass) {
    GetShardIteratorRequest request = GetShardIteratorRequest.builder().streamName(STREAM).shardId(SHARD_1).shardIteratorType(ShardIteratorType.LATEST).build();
    when(kinesis.getShardIterator(request)).thenThrow(thrownException);
    try {
        underTest.getShardIterator(STREAM, SHARD_1, ShardIteratorType.LATEST, null, null);
        failBecauseExceptionWasNotThrown(expectedExceptionClass);
    } catch (Exception e) {
        assertThat(e).isExactlyInstanceOf(expectedExceptionClass);
    } finally {
        reset(kinesis);
    }
}
Also used : GetShardIteratorRequest(software.amazon.awssdk.services.kinesis.model.GetShardIteratorRequest) LimitExceededException(software.amazon.awssdk.services.kinesis.model.LimitExceededException) ProvisionedThroughputExceededException(software.amazon.awssdk.services.kinesis.model.ProvisionedThroughputExceededException) SdkClientException(software.amazon.awssdk.core.exception.SdkClientException) ExpiredIteratorException(software.amazon.awssdk.services.kinesis.model.ExpiredIteratorException) SdkServiceException(software.amazon.awssdk.core.exception.SdkServiceException)

Aggregations

GetShardIteratorRequest (software.amazon.awssdk.services.kinesis.model.GetShardIteratorRequest)2 ArrayList (java.util.ArrayList)1 SdkBytes (software.amazon.awssdk.core.SdkBytes)1 SdkClientException (software.amazon.awssdk.core.exception.SdkClientException)1 SdkServiceException (software.amazon.awssdk.core.exception.SdkServiceException)1 DescribeStreamRequest (software.amazon.awssdk.services.kinesis.model.DescribeStreamRequest)1 DescribeStreamResponse (software.amazon.awssdk.services.kinesis.model.DescribeStreamResponse)1 ExpiredIteratorException (software.amazon.awssdk.services.kinesis.model.ExpiredIteratorException)1 GetRecordsRequest (software.amazon.awssdk.services.kinesis.model.GetRecordsRequest)1 GetRecordsResponse (software.amazon.awssdk.services.kinesis.model.GetRecordsResponse)1 GetShardIteratorResponse (software.amazon.awssdk.services.kinesis.model.GetShardIteratorResponse)1 LimitExceededException (software.amazon.awssdk.services.kinesis.model.LimitExceededException)1 ProvisionedThroughputExceededException (software.amazon.awssdk.services.kinesis.model.ProvisionedThroughputExceededException)1 Record (software.amazon.awssdk.services.kinesis.model.Record)1 Shard (software.amazon.awssdk.services.kinesis.model.Shard)1