software.amazon.awssdk.services.kinesis.model.GetShardIteratorRequest Java Examples

The following examples show how to use software.amazon.awssdk.services.kinesis.model.GetShardIteratorRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KinesisShardIterator.java    From synapse with Apache License 2.0 6 votes vote down vote up
private GetShardIteratorRequest buildIteratorShardRequest(final ShardPosition shardPosition) {
    final GetShardIteratorRequest.Builder shardRequestBuilder = GetShardIteratorRequest
            .builder()
            .shardId(shardPosition.shardName())
            .streamName(channelName);

    switch (shardPosition.startFrom()) {
        case HORIZON:
            shardRequestBuilder.shardIteratorType(TRIM_HORIZON);
            break;
        case POSITION:
            shardRequestBuilder.shardIteratorType(AFTER_SEQUENCE_NUMBER);
            shardRequestBuilder.startingSequenceNumber(shardPosition.position());
            break;
        case AT_POSITION:
            shardRequestBuilder.shardIteratorType(AT_SEQUENCE_NUMBER);
            shardRequestBuilder.startingSequenceNumber(shardPosition.position());
            break;
        case TIMESTAMP:
            shardRequestBuilder
                    .shardIteratorType(AT_TIMESTAMP)
                    .timestamp(shardPosition.timestamp());
            break;
    }
    return shardRequestBuilder.build();
}
 
Example #2
Source File: KinesisShardReader.java    From synapse with Apache License 2.0 6 votes vote down vote up
private ShardPosition sanitizePositionedShardPosition(ShardPosition shardPosition) {
    try {
        StartFrom startFrom = shardPosition.startFrom();

        if (startFrom == StartFrom.AT_POSITION || startFrom == StartFrom.POSITION) {
            ShardIteratorType type = startFrom == StartFrom.POSITION
                    ? ShardIteratorType.AFTER_SEQUENCE_NUMBER
                    : ShardIteratorType.AT_SEQUENCE_NUMBER;
            kinesisClient.getShardIterator(GetShardIteratorRequest.builder()
                    .shardId(shardName)
                    .streamName(channelName)
                    .shardIteratorType(type)
                    .startingSequenceNumber(shardPosition.position())
                    .build())
                    .get();
        }

        return shardPosition;
    } catch (ExecutionException | InterruptedException | RuntimeException e) {
        LOG.warn(marker, "given shardposition {} / {} not accessible, falling back to horizon", shardPosition.shardName(), shardPosition.position());
    }
    return ShardPosition.fromHorizon(shardPosition.shardName());
}
 
Example #3
Source File: IteratorBuilderTest.java    From amazon-kinesis-client with Apache License 2.0 6 votes vote down vote up
static WrappedRequest<GetShardIteratorRequest> wrapped(GetShardIteratorRequest.Builder builder) {
    GetShardIteratorRequest req = builder.build();
    return new WrappedRequest<GetShardIteratorRequest>() {
        @Override
        public ShardIteratorType shardIteratorType() {
            return req.shardIteratorType();
        }

        @Override
        public String sequenceNumber() {
            return req.startingSequenceNumber();
        }

        @Override
        public Instant timestamp() {
            return req.timestamp();
        }

        @Override
        public GetShardIteratorRequest request() {
            return req;
        }
    };
}
 
Example #4
Source File: PrefetchRecordsPublisherIntegrationTest.java    From amazon-kinesis-client with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() throws Exception {
    records = new ArrayList<>();
    dataFetcher = spy(new KinesisDataFetcherForTest(kinesisClient, streamName, shardId, MAX_RECORDS_PER_CALL));
    getRecordsRetrievalStrategy = Mockito.spy(new SynchronousGetRecordsRetrievalStrategy(dataFetcher));
    executorService = spy(Executors.newFixedThreadPool(1));
    CompletableFuture<GetShardIteratorResponse> future = mock(CompletableFuture.class);

    when(extendedSequenceNumber.sequenceNumber()).thenReturn("LATEST");
    when(future.get(anyLong(), any(TimeUnit.class))).thenReturn(GetShardIteratorResponse.builder().shardIterator("TestIterator").build());
    when(kinesisClient.getShardIterator(any(GetShardIteratorRequest.class))).thenReturn(future);

    getRecordsCache = new PrefetchRecordsPublisher(MAX_SIZE,
            MAX_BYTE_SIZE,
            MAX_RECORDS_COUNT,
            MAX_RECORDS_PER_CALL,
            getRecordsRetrievalStrategy,
            executorService,
            IDLE_MILLIS_BETWEEN_CALLS,
            new NullMetricsFactory(),
            operation,
            "test-shard");
}
 
Example #5
Source File: KinesisDataFetcherTest.java    From amazon-kinesis-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetRecordsThrowsSdkException() throws Exception {
    expectedExceptionRule.expect(SdkException.class);
    expectedExceptionRule.expectMessage("Test Exception");

    CompletableFuture<GetShardIteratorResponse> getShardIteratorFuture = CompletableFuture
            .completedFuture(GetShardIteratorResponse.builder().shardIterator("test").build());

    // Set up proxy mock methods
    when(kinesisClient.getShardIterator(any(GetShardIteratorRequest.class))).thenReturn(getShardIteratorFuture);
    when(kinesisClient.getRecords(any(GetRecordsRequest.class))).thenReturn(getRecordsResponseFuture);
    when(getRecordsResponseFuture.get(anyLong(), any(TimeUnit.class)))
            .thenThrow(new ExecutionException(SdkException.builder().message("Test Exception").build()));

    // Create data fectcher and initialize it with latest type checkpoint
    kinesisDataFetcher.initialize(SentinelCheckpoint.LATEST.toString(), INITIAL_POSITION_LATEST);
    final GetRecordsRetrievalStrategy getRecordsRetrievalStrategy = new SynchronousGetRecordsRetrievalStrategy(
            kinesisDataFetcher);

    // Call records of dataFetcher which will throw an exception
    getRecordsRetrievalStrategy.getRecords(MAX_RECORDS);

}
 
Example #6
Source File: KinesisDataFetcherTest.java    From amazon-kinesis-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testNonNullGetRecords() throws Exception {
    final String nextIterator = "TestIterator";
    final ArgumentCaptor<GetShardIteratorRequest> iteratorCaptor = ArgumentCaptor
            .forClass(GetShardIteratorRequest.class);
    final ArgumentCaptor<GetRecordsRequest> recordsCaptor = ArgumentCaptor.forClass(GetRecordsRequest.class);
    final GetShardIteratorRequest expectedIteratorRequest = makeGetShardIteratorRequest(
            ShardIteratorType.LATEST.name());
    final GetRecordsRequest expectedRecordsRequest = makeGetRecordsRequest(nextIterator);

    final CompletableFuture<GetRecordsResponse> future = mock(CompletableFuture.class);

    when(kinesisClient.getShardIterator(iteratorCaptor.capture()))
            .thenReturn(makeGetShardIteratorResonse(nextIterator));
    when(kinesisClient.getRecords(recordsCaptor.capture())).thenReturn(future);
    when(future.get(anyLong(), any(TimeUnit.class))).thenThrow(
            new ExecutionException(ResourceNotFoundException.builder().message("Test Exception").build()));

    kinesisDataFetcher.initialize(SentinelCheckpoint.LATEST.toString(), INITIAL_POSITION_LATEST);
    DataFetcherResult dataFetcherResult = kinesisDataFetcher.getRecords();

    assertNotNull(dataFetcherResult);
    assertEquals(expectedIteratorRequest.startingSequenceNumber(), iteratorCaptor.getValue().startingSequenceNumber());
    assertEquals(expectedRecordsRequest.shardIterator(), recordsCaptor.getValue().shardIterator());
}
 
Example #7
Source File: KinesisDataFetcherTest.java    From amazon-kinesis-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testTimeoutExceptionIsRetryableForGetRecords() throws Exception {
    expectedExceptionRule.expect(RetryableRetrievalException.class);
    expectedExceptionRule.expectCause(isA(TimeoutException.class));
    expectedExceptionRule.expectMessage("Timeout");

    CompletableFuture<GetShardIteratorResponse> getShardIteratorFuture = CompletableFuture
            .completedFuture(GetShardIteratorResponse.builder().shardIterator("test").build());

    // Set up proxy mock methods
    when(kinesisClient.getShardIterator(any(GetShardIteratorRequest.class))).thenReturn(getShardIteratorFuture);
    when(kinesisClient.getRecords(any(GetRecordsRequest.class))).thenReturn(getRecordsResponseFuture);
    when(getRecordsResponseFuture.get(anyLong(), any(TimeUnit.class))).thenThrow(new TimeoutException("Timeout"));

    // Create data fectcher and initialize it with latest type checkpoint
    kinesisDataFetcher.initialize(SentinelCheckpoint.LATEST.toString(), INITIAL_POSITION_LATEST);
    final GetRecordsRetrievalStrategy getRecordsRetrievalStrategy = new SynchronousGetRecordsRetrievalStrategy(
            kinesisDataFetcher);

    // Call records of dataFetcher which will throw an exception
    getRecordsRetrievalStrategy.getRecords(MAX_RECORDS);
}
 
Example #8
Source File: KinesisIntegrationTests.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetIteratorForBogusStream() {
    try {
        client.getShardIterator(GetShardIteratorRequest.builder()
                                                       .streamName("bogus-stream-name")
                                                       .shardId("bogus-shard-id")
                                                       .shardIteratorType(ShardIteratorType.LATEST)
                                                       .build());
        Assert.fail("Expected ResourceNotFoundException");
    } catch (ResourceNotFoundException exception) {
        // Ignored or expected.
    }
}
 
Example #9
Source File: KinesisDataFetcherTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testadvanceIteratorTo() throws KinesisClientLibException, InterruptedException, ExecutionException {
    final Checkpointer checkpoint = mock(Checkpointer.class);
    final String iteratorA = "foo";
    final String iteratorB = "bar";
    final String seqA = "123";
    final String seqB = "456";

    ArgumentCaptor<GetShardIteratorRequest> shardIteratorRequestCaptor = ArgumentCaptor
            .forClass(GetShardIteratorRequest.class);

    when(kinesisClient.getShardIterator(shardIteratorRequestCaptor.capture()))
            .thenReturn(makeGetShardIteratorResonse(iteratorA)).thenReturn(makeGetShardIteratorResonse(iteratorA))
            .thenReturn(makeGetShardIteratorResonse(iteratorB));
    when(checkpoint.getCheckpoint(SHARD_ID)).thenReturn(new ExtendedSequenceNumber(seqA));

    kinesisDataFetcher.initialize(seqA, null);
    kinesisDataFetcher.advanceIteratorTo(seqA, null);
    kinesisDataFetcher.advanceIteratorTo(seqB, null);

    final List<GetShardIteratorRequest> shardIteratorRequests = shardIteratorRequestCaptor.getAllValues();
    assertEquals(3, shardIteratorRequests.size());

    int count = 0;
    for (GetShardIteratorRequest request : shardIteratorRequests) {
        assertEquals(STREAM_NAME, request.streamName());
        assertEquals(SHARD_ID, request.shardId());
        assertEquals(ShardIteratorType.AT_SEQUENCE_NUMBER.toString(), request.shardIteratorTypeAsString());
        if (count == 2) {
            assertEquals(seqB, request.startingSequenceNumber());
        } else {
            assertEquals(seqA, request.startingSequenceNumber());
        }
        count++;
    }
}
 
Example #10
Source File: KinesisDataFetcherTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
private void testInitializeAndFetch(final String iteratorType, final String seqNo,
        final InitialPositionInStreamExtended initialPositionInStream) throws Exception {
    final ArgumentCaptor<GetShardIteratorRequest> iteratorCaptor = ArgumentCaptor
            .forClass(GetShardIteratorRequest.class);
    final ArgumentCaptor<GetRecordsRequest> recordsCaptor = ArgumentCaptor.forClass(GetRecordsRequest.class);
    final String iterator = "foo";
    final List<Record> expectedRecords = Collections.emptyList();
    GetShardIteratorRequest expectedIteratorRequest = makeGetShardIteratorRequest(iteratorType);
    if (iteratorType.equals(ShardIteratorType.AT_TIMESTAMP.toString())) {
        expectedIteratorRequest = expectedIteratorRequest.toBuilder()
                .timestamp(initialPositionInStream.getTimestamp().toInstant()).build();
    } else if (iteratorType.equals(ShardIteratorType.AT_SEQUENCE_NUMBER.toString())) {
        expectedIteratorRequest = expectedIteratorRequest.toBuilder().startingSequenceNumber(seqNo).build();
    }
    final GetRecordsRequest expectedRecordsRequest = makeGetRecordsRequest(iterator);

    when(kinesisClient.getShardIterator(iteratorCaptor.capture()))
            .thenReturn(makeGetShardIteratorResonse(iterator));

    when(kinesisClient.getRecords(recordsCaptor.capture()))
            .thenReturn(makeGetRecordsResponse(null, expectedRecords));

    Checkpointer checkpoint = mock(Checkpointer.class);
    when(checkpoint.getCheckpoint(SHARD_ID)).thenReturn(new ExtendedSequenceNumber(seqNo));

    final GetRecordsRetrievalStrategy getRecordsRetrievalStrategy = new SynchronousGetRecordsRetrievalStrategy(
            kinesisDataFetcher);
    kinesisDataFetcher.initialize(seqNo, initialPositionInStream);

    assertEquals(expectedRecords, getRecordsRetrievalStrategy.getRecords(MAX_RECORDS).records());
    verify(kinesisClient, times(1)).getShardIterator(any(GetShardIteratorRequest.class));
    verify(kinesisClient, times(1)).getRecords(any(GetRecordsRequest.class));
}
 
Example #11
Source File: KinesisDataFetcherTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testadvanceIteratorToTrimHorizonLatestAndAtTimestamp() throws InterruptedException, ExecutionException {
    final ArgumentCaptor<GetShardIteratorRequest> requestCaptor = ArgumentCaptor
            .forClass(GetShardIteratorRequest.class);
    final String iteratorHorizon = "TRIM_HORIZON";
    final String iteratorLatest = "LATEST";
    final String iteratorAtTimestamp = "AT_TIMESTAMP";
    final Map<ShardIteratorType, GetShardIteratorRequest> requestsMap = Arrays
            .stream(new String[] { iteratorHorizon, iteratorLatest, iteratorAtTimestamp })
            .map(this::makeGetShardIteratorRequest)
            .collect(Collectors.toMap(r -> ShardIteratorType.valueOf(r.shardIteratorTypeAsString()), r -> r));
    GetShardIteratorRequest tsReq = requestsMap.get(ShardIteratorType.AT_TIMESTAMP);
    requestsMap.put(ShardIteratorType.AT_TIMESTAMP,
            tsReq.toBuilder().timestamp(INITIAL_POSITION_AT_TIMESTAMP.getTimestamp().toInstant()).build());

    when(kinesisClient.getShardIterator(requestCaptor.capture()))
            .thenReturn(makeGetShardIteratorResonse(iteratorHorizon))
            .thenReturn(makeGetShardIteratorResonse(iteratorLatest))
            .thenReturn(makeGetShardIteratorResonse(iteratorAtTimestamp));

    kinesisDataFetcher.advanceIteratorTo(ShardIteratorType.TRIM_HORIZON.toString(), INITIAL_POSITION_TRIM_HORIZON);
    assertEquals(iteratorHorizon, kinesisDataFetcher.getNextIterator());

    kinesisDataFetcher.advanceIteratorTo(ShardIteratorType.LATEST.toString(), INITIAL_POSITION_LATEST);
    assertEquals(iteratorLatest, kinesisDataFetcher.getNextIterator());

    kinesisDataFetcher.advanceIteratorTo(ShardIteratorType.AT_TIMESTAMP.toString(), INITIAL_POSITION_AT_TIMESTAMP);
    assertEquals(iteratorAtTimestamp, kinesisDataFetcher.getNextIterator());

    final List<GetShardIteratorRequest> requests = requestCaptor.getAllValues();
    assertEquals(3, requests.size());
    requests.forEach(request -> {
        final ShardIteratorType type = ShardIteratorType.fromValue(request.shardIteratorTypeAsString());
        assertEquals(requestsMap.get(type).startingSequenceNumber(), request.startingSequenceNumber());
        requestsMap.remove(type);
    });
    assertEquals(0, requestsMap.size());
}
 
Example #12
Source File: KinesisDataFetcherTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetRecordsWithResourceNotFoundException() throws Exception {
    final ArgumentCaptor<GetShardIteratorRequest> iteratorCaptor = ArgumentCaptor
            .forClass(GetShardIteratorRequest.class);
    final ArgumentCaptor<GetRecordsRequest> recordsCaptor = ArgumentCaptor.forClass(GetRecordsRequest.class);
    // Set up arguments used by proxy
    final String nextIterator = "TestShardIterator";

    final GetShardIteratorRequest expectedIteratorRequest = makeGetShardIteratorRequest(
            ShardIteratorType.LATEST.name());
    final GetRecordsRequest expectedRecordsRequest = makeGetRecordsRequest(nextIterator);

    final CompletableFuture<GetRecordsResponse> future = mock(CompletableFuture.class);

    // Set up proxy mock methods
    when(kinesisClient.getShardIterator(iteratorCaptor.capture()))
            .thenReturn(makeGetShardIteratorResonse(nextIterator));
    when(kinesisClient.getRecords(recordsCaptor.capture())).thenReturn(future);
    when(future.get(anyLong(), any(TimeUnit.class))).thenThrow(
            new ExecutionException(ResourceNotFoundException.builder().message("Test Exception").build()));

    // Create data fectcher and initialize it with latest type checkpoint
    kinesisDataFetcher.initialize(SentinelCheckpoint.LATEST.toString(), INITIAL_POSITION_LATEST);
    final GetRecordsRetrievalStrategy getRecordsRetrievalStrategy = new SynchronousGetRecordsRetrievalStrategy(
            kinesisDataFetcher);
    try {
        // Call records of dataFetcher which will throw an exception
        getRecordsRetrievalStrategy.getRecords(MAX_RECORDS);
    } finally {
        // Test shard has reached the end
        assertTrue("Shard should reach the end", kinesisDataFetcher.isShardEndReached());
        assertEquals(expectedIteratorRequest.startingSequenceNumber(), iteratorCaptor.getValue().startingSequenceNumber());
        assertEquals(expectedRecordsRequest.shardIterator(), recordsCaptor.getValue().shardIterator());
    }
}
 
Example #13
Source File: KinesisDataFetcherTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testTimeoutExceptionIsRetryableForGetShardIterator() throws Exception {
    expectedExceptionRule.expect(RetryableRetrievalException.class);
    expectedExceptionRule.expectCause(isA(TimeoutException.class));
    expectedExceptionRule.expectMessage("Timeout");

    // Set up proxy mock methods
    when(kinesisClient.getShardIterator(any(GetShardIteratorRequest.class)))
            .thenReturn(getShardIteratorResponseFuture);
    when(getShardIteratorResponseFuture.get(anyLong(), any(TimeUnit.class)))
            .thenThrow(new TimeoutException("Timeout"));

    // Create data fectcher and initialize it with latest type checkpoint
    kinesisDataFetcher.initialize(SentinelCheckpoint.LATEST.toString(), INITIAL_POSITION_LATEST);
}
 
Example #14
Source File: IteratorBuilderTest.java    From amazon-kinesis-client with Apache License 2.0 4 votes vote down vote up
private GetShardIteratorRequest.Builder gsiBase() {
    return GetShardIteratorRequest.builder().shardId(SHARD_ID).streamName(STREAM_NAME);
}
 
Example #15
Source File: IteratorBuilderTest.java    From amazon-kinesis-client with Apache License 2.0 4 votes vote down vote up
private void verifyGsiBase(GetShardIteratorRequest req) {
    assertThat(req.streamName(), equalTo(STREAM_NAME));
    assertThat(req.shardId(), equalTo(SHARD_ID));
}
 
Example #16
Source File: KinesisDataFetcherTest.java    From amazon-kinesis-client with Apache License 2.0 4 votes vote down vote up
@Test
public void testFetcherDoesNotAdvanceWithoutAccept() throws InterruptedException, ExecutionException {
    final ArgumentCaptor<GetShardIteratorRequest> iteratorCaptor = ArgumentCaptor
            .forClass(GetShardIteratorRequest.class);
    final ArgumentCaptor<GetRecordsRequest> recordsCaptor = ArgumentCaptor.forClass(GetRecordsRequest.class);
    final String initialIterator = "InitialIterator";
    final String nextIterator1 = "NextIteratorOne";
    final String nextIterator2 = "NextIteratorTwo";
    final CompletableFuture<GetRecordsResponse> nonAdvancingResult1 = makeGetRecordsResponse(initialIterator, null);
    final CompletableFuture<GetRecordsResponse> nonAdvancingResult2 = makeGetRecordsResponse(nextIterator1, null);
    final CompletableFuture<GetRecordsResponse> finalNonAdvancingResult = makeGetRecordsResponse(nextIterator2,
            null);
    final CompletableFuture<GetRecordsResponse> advancingResult1 = makeGetRecordsResponse(nextIterator1, null);
    final CompletableFuture<GetRecordsResponse> advancingResult2 = makeGetRecordsResponse(nextIterator2, null);
    final CompletableFuture<GetRecordsResponse> finalAdvancingResult = makeGetRecordsResponse(null, null);

    when(kinesisClient.getShardIterator(iteratorCaptor.capture()))
            .thenReturn(makeGetShardIteratorResonse(initialIterator));
    when(kinesisClient.getRecords(recordsCaptor.capture())).thenReturn(nonAdvancingResult1, advancingResult1,
            nonAdvancingResult2, advancingResult2, finalNonAdvancingResult, finalAdvancingResult);

    kinesisDataFetcher.initialize("TRIM_HORIZON",
            InitialPositionInStreamExtended.newInitialPosition(InitialPositionInStream.TRIM_HORIZON));

    assertNoAdvance(nonAdvancingResult1.get(), initialIterator);
    assertAdvanced(advancingResult1.get(), initialIterator, nextIterator1);
    verify(kinesisClient, times(2)).getRecords(any(GetRecordsRequest.class));

    assertNoAdvance(nonAdvancingResult2.get(), nextIterator1);
    assertAdvanced(advancingResult2.get(), nextIterator1, nextIterator2);
    verify(kinesisClient, times(4)).getRecords(any(GetRecordsRequest.class));

    assertNoAdvance(finalNonAdvancingResult.get(), nextIterator2);
    assertAdvanced(finalAdvancingResult.get(), nextIterator2, null);
    verify(kinesisClient, times(6)).getRecords(any(GetRecordsRequest.class));



    reset(kinesisClient);

    DataFetcherResult terminal = kinesisDataFetcher.getRecords();
    assertTrue(terminal.isShardEnd());
    assertNotNull(terminal.getResult());

    final GetRecordsResponse terminalResult = terminal.getResult();
    assertNotNull(terminalResult.records());
    assertEquals(0, terminalResult.records().size());
    assertNull(terminalResult.nextShardIterator());
    assertEquals(kinesisDataFetcher.TERMINAL_RESULT, terminal);

    verify(kinesisClient, never()).getRecords(any(GetRecordsRequest.class));
}
 
Example #17
Source File: KinesisDataFetcherTest.java    From amazon-kinesis-client with Apache License 2.0 4 votes vote down vote up
private GetShardIteratorRequest makeGetShardIteratorRequest(String shardIteratorType) {
    return GetShardIteratorRequest.builder().shardIteratorType(shardIteratorType).streamName(STREAM_NAME)
            .shardId(SHARD_ID).build();
}
 
Example #18
Source File: KinesisRequestsBuilder.java    From amazon-kinesis-client with Apache License 2.0 4 votes vote down vote up
public static GetShardIteratorRequest.Builder getShardIteratorRequestBuilder() {
    return appendUserAgent(GetShardIteratorRequest.builder());
}
 
Example #19
Source File: IteratorBuilder.java    From amazon-kinesis-client with Apache License 2.0 4 votes vote down vote up
public static GetShardIteratorRequest.Builder request(GetShardIteratorRequest.Builder builder,
        String sequenceNumber, InitialPositionInStreamExtended initialPosition) {
    return apply(builder, GetShardIteratorRequest.Builder::shardIteratorType, GetShardIteratorRequest.Builder::timestamp,
            GetShardIteratorRequest.Builder::startingSequenceNumber, initialPosition, sequenceNumber,
            ShardIteratorType.AT_SEQUENCE_NUMBER);
}
 
Example #20
Source File: GetRecords.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
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 = record.data();
                System.out.println(String.format("Seq No: %s - %s", record.sequenceNumber(),
                 new String(byteBuffer.asByteArray())));
             }

             }
 
Example #21
Source File: KinesisIntegrationTests.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
private void testGets(final String streamName, final Shard shard) {
    // Get an iterator for the first shard.
    GetShardIteratorResponse iteratorResult = client.getShardIterator(
            GetShardIteratorRequest.builder()
                                   .streamName(streamName)
                                   .shardId(shard.shardId())
                                   .shardIteratorType(ShardIteratorType.AT_SEQUENCE_NUMBER)
                                   .startingSequenceNumber(shard.sequenceNumberRange().startingSequenceNumber())
                                   .build());
    Assert.assertNotNull(iteratorResult);

    String iterator = iteratorResult.shardIterator();
    Assert.assertNotNull(iterator);

    int tries = 0;
    GetRecordsResponse result;
    List<Record> records;

    // Read the first record from the first shard (looping until it's
    // available).
    while (true) {
        tries += 1;
        if (tries > 100) {
            Assert.fail("Failed to read any records after 100 seconds");
        }

        result = client.getRecords(GetRecordsRequest.builder()
                                                    .shardIterator(iterator)
                                                    .limit(1)
                                                    .build());
        Assert.assertNotNull(result);
        Assert.assertNotNull(result.records());
        Assert.assertNotNull(result.nextShardIterator());

        records = result.records();
        if (records.size() > 0) {
            long arrivalTime = records.get(0).approximateArrivalTimestamp().toEpochMilli();
            Long delta = Math.abs(Instant.now().minusMillis(arrivalTime).toEpochMilli());
            // Assert that the arrival date is within 5 minutes of the current date to make sure it unmarshalled correctly.
            assertThat(delta, Matchers.lessThan(60 * 5000L));
            break;
        }

        try {
            Thread.sleep(1000);
        } catch (InterruptedException exception) {
            throw new RuntimeException(exception);
        }

        iterator = result.nextShardIterator();
    }

    System.out.println("  [Succeeded after " + tries + " tries]");
    Assert.assertEquals(1, records.size());
    validateRecord(records.get(0), "See No Evil");

    // Read the second record from the first shard.
    result = client.getRecords(GetRecordsRequest.builder()
                                                .shardIterator(result.nextShardIterator())
                                                .build());
    Assert.assertNotNull(result);
    Assert.assertNotNull(result.records());
    Assert.assertNotNull(result.nextShardIterator());

    records = result.records();
    Assert.assertEquals(1, records.size());
    validateRecord(records.get(0), "See No Evil");

    // Try to read some more, get EOF.
    result = client.getRecords(GetRecordsRequest.builder()
                                                .shardIterator(result.nextShardIterator())
                                                .build());
    Assert.assertNotNull(result);
    Assert.assertNotNull(result.records());
    Assert.assertTrue(result.records().isEmpty());
    Assert.assertNull(result.nextShardIterator());
}