software.amazon.awssdk.services.dynamodb.model.ScanResponse Java Examples

The following examples show how to use software.amazon.awssdk.services.dynamodb.model.ScanResponse. 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: DynamoDBScanItems.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void scanItems( DynamoDbClient ddb,String tableName ) {

        try {
            ScanRequest scanRequest = ScanRequest.builder()
                    .tableName(tableName)
                    .build();

            ScanResponse response = ddb.scan(scanRequest);
            for (Map<String, AttributeValue> item : response.items()) {
                Set<String> keys = item.keySet();
                for (String key : keys) {

                    System.out.println ("The key name is "+key +"\n" );
                    System.out.println("The value is "+item.get(key).s());
                }
            }

        } catch (DynamoDbException e) {
            e.printStackTrace();
        }
        // snippet-end:[dynamodb.java2.dynamoDB_scan.main]
    }
 
Example #2
Source File: ScanOperationTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void transformResults_multipleItems_setsLastEvaluatedKey() {
    List<FakeItem> scanResultItems = generateFakeItemList();
    FakeItem lastEvaluatedKey = createUniqueFakeItem();
    List<Map<String, AttributeValue>> scanResultMaps =
        scanResultItems.stream().map(ScanOperationTest::getAttributeValueMap).collect(toList());

    ScanResponse scanResponse = generateFakeScanResults(scanResultMaps, getAttributeValueMap(lastEvaluatedKey));

    Page<FakeItem> scanResultPage = scanOperation.transformResponse(scanResponse,
                                                                    FakeItem.getTableSchema(),
                                                                    PRIMARY_CONTEXT,
                                                                    null);

    assertThat(scanResultPage.lastEvaluatedKey(), is(getAttributeValueMap(lastEvaluatedKey)));
}
 
Example #3
Source File: ScanOperation.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public Page<T> transformResponse(ScanResponse response,
                                 TableSchema<T> tableSchema,
                                 OperationContext context,
                                 DynamoDbEnhancedClientExtension dynamoDbEnhancedClientExtension) {

    return EnhancedClientUtils.readAndTransformPaginatedItems(response,
                                                              tableSchema,
                                                              context,
                                                              dynamoDbEnhancedClientExtension,
                                                              ScanResponse::items,
                                                              ScanResponse::lastEvaluatedKey);
}
 
Example #4
Source File: DynamoDBLeaseRefresherTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteAllLeasesTimesOut() throws Exception {
    TimeoutException te = setRuleForDependencyTimeout();
    when(dynamoDbClient.scan(any(ScanRequest.class))).thenReturn(mockScanFuture);
    when(mockScanFuture.get(anyLong(), any())).thenReturn(ScanResponse.builder().items(Collections.emptyMap()).build());
    when(leaseSerializer.fromDynamoRecord(any())).thenReturn(lease);
    when(leaseSerializer.getDynamoHashKey(any(Lease.class))).thenReturn(Collections.emptyMap());

    when(dynamoDbClient.deleteItem(any(DeleteItemRequest.class))).thenReturn(mockDeleteFuture);
    when(mockDeleteFuture.get(anyLong(), any())).thenThrow(te);

    verifyCancel(mockDeleteFuture, () -> leaseRefresher.deleteAll());
}
 
Example #5
Source File: AWSDynamoUtils.java    From para with Apache License 2.0 5 votes vote down vote up
/**
 * Reads a page from a standard DynamoDB table.
 * @param <P> type of object
 * @param appid the app identifier (name)
 * @param p a {@link Pager}
 * @return the last row key of the page, or null.
 */
public static <P extends ParaObject> List<P> readPageFromTable(String appid, Pager p) {
	Pager pager = (p != null) ? p : new Pager();
	ScanRequest.Builder scanRequest = ScanRequest.builder().
			tableName(getTableNameForAppid(appid)).
			limit(pager.getLimit()).
			returnConsumedCapacity(ReturnConsumedCapacity.TOTAL);

	if (!StringUtils.isBlank(pager.getLastKey())) {
		scanRequest.exclusiveStartKey(Collections.
				singletonMap(Config._KEY, AttributeValue.builder().s(pager.getLastKey()).build()));
	}

	ScanResponse result = getClient().scan(scanRequest.build());
	String lastKey = null;
	LinkedList<P> results = new LinkedList<>();
	for (Map<String, AttributeValue> item : result.items()) {
		P obj = fromRow(item);
		if (obj != null) {
			lastKey = item.get(Config._KEY).s();
			results.add(obj);
		}
	}

	if (result.lastEvaluatedKey() != null && !result.lastEvaluatedKey().isEmpty()) {
		pager.setLastKey(result.lastEvaluatedKey().get(Config._KEY).s());
	} else if (!results.isEmpty()) {
		// set last key to be equal to the last result - end reached.
		pager.setLastKey(lastKey);
	}
	return results;
}
 
Example #6
Source File: DynamoDBIOTestHelper.java    From beam with Apache License 2.0 5 votes vote down vote up
static List<Map<String, AttributeValue>> generateTestData(String tableName, int numOfItems) {
  BatchWriteItemRequest batchWriteItemRequest =
      generateBatchWriteItemRequest(tableName, numOfItems);

  dynamoDBClient.batchWriteItem(batchWriteItemRequest);
  ScanResponse scanResult =
      dynamoDBClient.scan(ScanRequest.builder().tableName(tableName).build());

  List<Map<String, AttributeValue>> items = scanResult.items();
  Assert.assertEquals(numOfItems, items.size());
  return items;
}
 
Example #7
Source File: DynamoDBIO.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public List<Map<String, AttributeValue>> apply(@Nullable ScanResponse scanResponse) {
  if (scanResponse == null) {
    return Collections.emptyList();
  }
  return scanResponse.items();
}
 
Example #8
Source File: DynamoDBIO.java    From beam with Apache License 2.0 5 votes vote down vote up
@ProcessElement
public void processElement(@Element Read<T> spec, OutputReceiver<T> out) {
  DynamoDbClient client = spec.getDynamoDbClientProvider().getDynamoDbClient();

  ScanRequest scanRequest = spec.getScanRequestFn().apply(null);
  ScanRequest scanRequestWithSegment =
      scanRequest.toBuilder().segment(spec.getSegmentId()).build();

  ScanResponse scanResponse = client.scan(scanRequestWithSegment);
  out.output(spec.getScanResponseMapperFn().apply(scanResponse));
}
 
Example #9
Source File: EnhancedClientScanV1MapperComparisonBenchmark.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
TestItem(TableSchema<?> schema,
         ScanResponse v2Response,

         Class<?> v1BeanClass,
         ScanResult v1Response) {
    this.schema = schema;
    this.v2Response = v2Response;

    this.v1BeanClass = v1BeanClass;
    this.v1Response = v1Response;
}
 
Example #10
Source File: ScanOperationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private static ScanResponse generateFakeScanResults(List<Map<String, AttributeValue>> scanItemMapsPage,
                                                    Map<String, AttributeValue> lastEvaluatedKey) {
    return ScanResponse.builder()
                       .items(scanItemMapsPage)
                       .lastEvaluatedKey(lastEvaluatedKey)
                       .build();
}
 
Example #11
Source File: ScanOperationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void scanItem_withExtension_correctlyTransformsItems() {
    List<FakeItem> scanResultItems = generateFakeItemList();
    List<FakeItem> modifiedResultItems = generateFakeItemList();

    List<Map<String, AttributeValue>> scanResultMaps =
        scanResultItems.stream().map(ScanOperationTest::getAttributeValueMap).collect(toList());

    ReadModification[] readModifications =
        modifiedResultItems.stream()
              .map(ScanOperationTest::getAttributeValueMap)
              .map(attributeMap -> ReadModification.builder().transformedItem(attributeMap).build())
              .collect(Collectors.toList())
              .toArray(new ReadModification[]{});
    when(mockDynamoDbEnhancedClientExtension.afterRead(any(DynamoDbExtensionContext.AfterRead.class)))
        .thenReturn(readModifications[0], Arrays.copyOfRange(readModifications, 1, readModifications.length));

    ScanResponse scanResponse = generateFakeScanResults(scanResultMaps);

    Page<FakeItem> scanResultPage = scanOperation.transformResponse(scanResponse,
                                                                    FakeItem.getTableSchema(),
                                                                    PRIMARY_CONTEXT,
                                                                    mockDynamoDbEnhancedClientExtension);

    assertThat(scanResultPage.items(), is(modifiedResultItems));

    InOrder inOrder = Mockito.inOrder(mockDynamoDbEnhancedClientExtension);
    scanResultMaps.forEach(
        attributeMap -> inOrder.verify(mockDynamoDbEnhancedClientExtension).afterRead(
            DefaultDynamoDbExtensionContext.builder()
                                           .tableMetadata(FakeItem.getTableMetadata())
                                           .operationContext(PRIMARY_CONTEXT)
                                           .items(attributeMap).build()));
}
 
Example #12
Source File: ScanOperationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void transformResults_multipleItems_returnsCorrectItems() {
    List<FakeItem> scanResultItems = generateFakeItemList();
    List<Map<String, AttributeValue>> scanResultMaps =
        scanResultItems.stream().map(ScanOperationTest::getAttributeValueMap).collect(toList());

    ScanResponse scanResponse = generateFakeScanResults(scanResultMaps);

    Page<FakeItem> scanResultPage = scanOperation.transformResponse(scanResponse,
                                                                    FakeItem.getTableSchema(),
                                                                    PRIMARY_CONTEXT,
                                                                    null);
    assertThat(scanResultPage.items(), is(scanResultItems));
}
 
Example #13
Source File: ScanOperationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void getAsyncServiceCall_makesTheRightCallAndReturnsResponse() {
    ScanRequest scanRequest = ScanRequest.builder().build();
    ScanPublisher mockScanPublisher = mock(ScanPublisher.class);
    when(mockDynamoDbAsyncClient.scanPaginator(any(ScanRequest.class))).thenReturn(mockScanPublisher);

    SdkPublisher<ScanResponse> response = scanOperation.asyncServiceCall(mockDynamoDbAsyncClient)
                                                       .apply(scanRequest);

    assertThat(response, is(mockScanPublisher));
    verify(mockDynamoDbAsyncClient).scanPaginator(scanRequest);
}
 
Example #14
Source File: ScanOperationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void getServiceCall_makesTheRightCallAndReturnsResponse() {
    ScanRequest scanRequest = ScanRequest.builder().build();
    ScanIterable mockScanIterable = mock(ScanIterable.class);
    when(mockDynamoDbClient.scanPaginator(any(ScanRequest.class))).thenReturn(mockScanIterable);

    SdkIterable<ScanResponse> response = scanOperation.serviceCall(mockDynamoDbClient).apply(scanRequest);

    assertThat(response, is(mockScanIterable));
    verify(mockDynamoDbClient).scanPaginator(scanRequest);
}
 
Example #15
Source File: DynamoDBPositionsStorage.java    From liiklus with MIT License 5 votes vote down vote up
@Override
public Publisher<Positions> findAll() {
    return Flux.from(dynamoDB.scanPaginator(req -> req.tableName(tableName)))
            .flatMapIterable(ScanResponse::items)
            .map(item -> new Positions(
                    item.get("topic").s(),
                    GroupId.ofString(item.get("groupId").s()),
                    toPositions(item)
            ));
}
 
Example #16
Source File: ScanOperationTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
private static ScanResponse generateFakeScanResults(List<Map<String, AttributeValue>> scanItemMapsPage) {
    return ScanResponse.builder().items(scanItemMapsPage).build();
}
 
Example #17
Source File: EnhancedClientScanV1MapperComparisonBenchmark.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
private static DynamoDbClient getV2Client(Blackhole bh, ScanResponse scanResponse) {
    return new V2TestDynamoDbScanClient(bh, scanResponse);
}
 
Example #18
Source File: V2TestDynamoDbScanClient.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
public V2TestDynamoDbScanClient(Blackhole bh, ScanResponse scanResponse) {
    super(bh);
    this.scanResponse = scanResponse;
}
 
Example #19
Source File: V2TestDynamoDbScanClient.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public ScanResponse scan(ScanRequest scanRequest) {
    bh.consume(scanRequest);
    return this.scanResponse;
}
 
Example #20
Source File: DynamoDBIO.java    From beam with Apache License 2.0 4 votes vote down vote up
@Nullable
abstract SerializableFunction<ScanResponse, T> getScanResponseMapperFn();
 
Example #21
Source File: DynamoDBIO.java    From beam with Apache License 2.0 4 votes vote down vote up
abstract Builder<T> setScanResponseMapperFn(
SerializableFunction<ScanResponse, T> scanResponseMapperFn);
 
Example #22
Source File: DynamoDBIO.java    From beam with Apache License 2.0 4 votes vote down vote up
public Read<T> withScanResponseMapperFn(
    SerializableFunction<ScanResponse, T> scanResultMapperFn) {
  checkArgument(scanResultMapperFn != null, "scanResultMapper can not be null");
  return toBuilder().setScanResponseMapperFn(scanResultMapperFn).build();
}
 
Example #23
Source File: ScanOperation.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public Function<ScanRequest, SdkPublisher<ScanResponse>> asyncServiceCall(DynamoDbAsyncClient dynamoDbAsyncClient) {
    return dynamoDbAsyncClient::scanPaginator;
}
 
Example #24
Source File: DynamoDBIOTestHelper.java    From beam with Apache License 2.0 4 votes vote down vote up
static List<Map<String, AttributeValue>> readDataFromTable(String tableName) {
  ScanRequest scanRequest = ScanRequest.builder().tableName(tableName).build();
  ScanResponse scanResponse = dynamoDBClient.scan(scanRequest);
  return scanResponse.items();
}
 
Example #25
Source File: ScanOperation.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public Function<ScanRequest, SdkIterable<ScanResponse>> serviceCall(DynamoDbClient dynamoDbClient) {
    return dynamoDbClient::scanPaginator;
}
 
Example #26
Source File: DynamoDBLeaseRefresherTest.java    From amazon-kinesis-client with Apache License 2.0 4 votes vote down vote up
@Test
public void testListLeasesSucceedsThenFails() throws Exception {
    TimeoutException te = setRuleForDependencyTimeout();

    when(dynamoDbClient.scan(any(ScanRequest.class))).thenReturn(mockScanFuture);

    Map<String, AttributeValue> lastEvaluatedKey = new HashMap<>();
    lastEvaluatedKey.put("Test", AttributeValue.builder().s("test").build());

    when(mockScanFuture.get(anyLong(), any(TimeUnit.class)))
            .thenReturn(ScanResponse.builder().lastEvaluatedKey(lastEvaluatedKey).build())
            .thenThrow(te);

    verifyCancel(mockScanFuture, () -> leaseRefresher.listLeases());

    verify(mockScanFuture, times(2)).get(anyLong(), any(TimeUnit.class));
    verify(dynamoDbClient, times(2)).scan(any(ScanRequest.class));

}