Search in sources :

Example 1 with BatchGetItemResponse

use of software.amazon.awssdk.services.dynamodb.model.BatchGetItemResponse in project formkiq-core by formkiq.

the class ConfigServiceImpl method get.

@Override
public DynamicObject get(final String siteId) {
    Collection<Map<String, AttributeValue>> keys = new ArrayList<>();
    if (isDefaultSiteId(siteId)) {
        keys.add(keysGeneric(null, PREFIX_CONFIG, DEFAULT_SITE_ID));
    } else {
        keys.add(keysGeneric(null, PREFIX_CONFIG, siteId));
        keys.add(keysGeneric(null, PREFIX_CONFIG, DEFAULT_SITE_ID));
    }
    Map<String, KeysAndAttributes> items = Map.of(this.documentTableName, KeysAndAttributes.builder().keys(keys).build());
    BatchGetItemResponse response = this.dynamoDB.batchGetItem(BatchGetItemRequest.builder().requestItems(items).build());
    AttributeValueToDynamicObject transform = new AttributeValueToDynamicObject();
    Optional<Map<String, AttributeValue>> map = Optional.empty();
    List<Map<String, AttributeValue>> list = response.responses().get(this.documentTableName);
    if (!list.isEmpty()) {
        map = list.stream().filter(s -> s.get(SK).s().equals(siteId)).findFirst();
        if (map.isEmpty()) {
            map = list.stream().filter(s -> s.get(SK).s().equals(DEFAULT_SITE_ID)).findFirst();
        }
    }
    return !map.isEmpty() ? transform.apply(map.get()) : new DynamicObject(Map.of());
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) BatchGetItemResponse(software.amazon.awssdk.services.dynamodb.model.BatchGetItemResponse) ArrayList(java.util.ArrayList) KeysAndAttributes(software.amazon.awssdk.services.dynamodb.model.KeysAndAttributes) Map(java.util.Map)

Example 2 with BatchGetItemResponse

use of software.amazon.awssdk.services.dynamodb.model.BatchGetItemResponse in project formkiq-core by formkiq.

the class WebhooksServiceImpl method findWebhooks.

@Override
public List<DynamicObject> findWebhooks(final String siteId) {
    Map<String, AttributeValue> key = queryKeys(keysGeneric(siteId, PREFIX_WEBHOOKS, null));
    String expr = GSI1_PK + " = :pk";
    QueryRequest q = QueryRequest.builder().tableName(this.documentTableName).indexName(GSI1).keyConditionExpression(expr).expressionAttributeValues(key).build();
    QueryResponse result = this.dynamoDB.query(q);
    Collection<? extends Map<String, AttributeValue>> keys = result.items().stream().map(i -> Map.of(PK, i.get(PK), SK, i.get(SK))).collect(Collectors.toList());
    List<DynamicObject> retlist = Collections.emptyList();
    if (!keys.isEmpty()) {
        Map<String, KeysAndAttributes> items = Map.of(this.documentTableName, KeysAndAttributes.builder().keys(keys).build());
        BatchGetItemResponse batch = this.dynamoDB.batchGetItem(BatchGetItemRequest.builder().requestItems(items).build());
        Map<String, List<Map<String, AttributeValue>>> responses = batch.responses();
        List<Map<String, AttributeValue>> list = responses.get(this.documentTableName);
        AttributeValueToDynamicObject transform = new AttributeValueToDynamicObject();
        retlist = list.stream().map(m -> transform.apply(m)).collect(Collectors.toList());
        retlist.forEach(ob -> updateWebhookTimeToLive(ob));
    }
    return retlist;
}
Also used : BatchGetItemRequest(software.amazon.awssdk.services.dynamodb.model.BatchGetItemRequest) KeysAndAttributes(software.amazon.awssdk.services.dynamodb.model.KeysAndAttributes) TransactWriteItem(software.amazon.awssdk.services.dynamodb.model.TransactWriteItem) Date(java.util.Date) GetItemRequest(software.amazon.awssdk.services.dynamodb.model.GetItemRequest) UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) SiteIdKeyGenerator.createDatabaseKey(com.formkiq.stacks.dynamodb.SiteIdKeyGenerator.createDatabaseKey) Map(java.util.Map) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) TransactWriteItemsRequest(software.amazon.awssdk.services.dynamodb.model.TransactWriteItemsRequest) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) AttributeValueUpdate(software.amazon.awssdk.services.dynamodb.model.AttributeValueUpdate) Collection(java.util.Collection) QueryResponse(software.amazon.awssdk.services.dynamodb.model.QueryResponse) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Put(software.amazon.awssdk.services.dynamodb.model.Put) List(java.util.List) PutItemRequest(software.amazon.awssdk.services.dynamodb.model.PutItemRequest) BatchGetItemResponse(software.amazon.awssdk.services.dynamodb.model.BatchGetItemResponse) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) GetItemResponse(software.amazon.awssdk.services.dynamodb.model.GetItemResponse) DeleteItemRequest(software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest) Collections(java.util.Collections) QueryRequest(software.amazon.awssdk.services.dynamodb.model.QueryRequest) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) QueryRequest(software.amazon.awssdk.services.dynamodb.model.QueryRequest) KeysAndAttributes(software.amazon.awssdk.services.dynamodb.model.KeysAndAttributes) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) QueryResponse(software.amazon.awssdk.services.dynamodb.model.QueryResponse) BatchGetItemResponse(software.amazon.awssdk.services.dynamodb.model.BatchGetItemResponse) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with BatchGetItemResponse

use of software.amazon.awssdk.services.dynamodb.model.BatchGetItemResponse in project aws-sdk-java-v2 by aws.

the class PaginatorIntegrationTest method batchGetItem_allProcessed_shouldNotHaveNextPage.

@Test
public void batchGetItem_allProcessed_shouldNotHaveNextPage() {
    Map<String, KeysAndAttributes> attributes = new HashMap<>();
    Map<String, AttributeValue> keys;
    KeysAndAttributes keysAndAttributes;
    List<Map<String, AttributeValue>> keysList = new ArrayList<>();
    for (int i = 0; i < ITEM_COUNT; i++) {
        keys = new HashMap<>();
        keys.put(HASH_KEY_NAME, AttributeValue.builder().n(String.valueOf(i)).build());
        keysList.add(keys);
    }
    keysAndAttributes = KeysAndAttributes.builder().keys(keysList).build();
    attributes.put(TABLE_NAME, keysAndAttributes);
    Iterator<BatchGetItemResponse> iterator = dynamo.batchGetItemPaginator(b -> b.requestItems(attributes)).iterator();
    assertThat(iterator.next().unprocessedKeys().isEmpty()).isTrue();
    assertThat(iterator.hasNext()).isFalse();
}
Also used : KeysAndAttributes(software.amazon.awssdk.services.dynamodb.model.KeysAndAttributes) BeforeClass(org.junit.BeforeClass) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BiFunction(java.util.function.BiFunction) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) Random(java.util.Random) ArrayList(java.util.ArrayList) TableUtils(utils.test.util.TableUtils) ProvisionedThroughput(software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput) Map(java.util.Map) CreateTableRequest(software.amazon.awssdk.services.dynamodb.model.CreateTableRequest) ScanRequest(software.amazon.awssdk.services.dynamodb.model.ScanRequest) DeleteTableRequest(software.amazon.awssdk.services.dynamodb.model.DeleteTableRequest) DynamoDBTestBase(utils.test.util.DynamoDBTestBase) ScalarAttributeType(software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType) AfterClass(org.junit.AfterClass) Iterator(java.util.Iterator) BasicTempTable(utils.resources.tables.BasicTempTable) ScanResponse(software.amazon.awssdk.services.dynamodb.model.ScanResponse) SdkIterable(software.amazon.awssdk.core.pagination.sync.SdkIterable) KeyType(software.amazon.awssdk.services.dynamodb.model.KeyType) Test(org.junit.Test) SdkPublisher(software.amazon.awssdk.core.async.SdkPublisher) AttributeDefinition(software.amazon.awssdk.services.dynamodb.model.AttributeDefinition) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) ScanPublisher(software.amazon.awssdk.services.dynamodb.paginators.ScanPublisher) List(java.util.List) Stream(java.util.stream.Stream) KeySchemaElement(software.amazon.awssdk.services.dynamodb.model.KeySchemaElement) PutItemRequest(software.amazon.awssdk.services.dynamodb.model.PutItemRequest) BatchGetItemResponse(software.amazon.awssdk.services.dynamodb.model.BatchGetItemResponse) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) ScanIterable(software.amazon.awssdk.services.dynamodb.paginators.ScanIterable) Assert.assertEquals(org.junit.Assert.assertEquals) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) KeysAndAttributes(software.amazon.awssdk.services.dynamodb.model.KeysAndAttributes) BatchGetItemResponse(software.amazon.awssdk.services.dynamodb.model.BatchGetItemResponse) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 4 with BatchGetItemResponse

use of software.amazon.awssdk.services.dynamodb.model.BatchGetItemResponse in project aws-sdk-java-v2 by aws.

the class BatchGetItemOperationTest method transformResponse_multipleTables_multipleItems_no_unprocessedKeys.

@Test
public void transformResponse_multipleTables_multipleItems_no_unprocessedKeys() {
    BatchGetItemResponse fakeResponse = BatchGetItemResponse.builder().build();
    BatchGetItemOperation operation = BatchGetItemOperation.create(emptyRequest());
    BatchGetResultPage resultsPage = operation.transformResponse(fakeResponse, null);
    List<Key> fakeItemResults1 = resultsPage.unprocessedKeysForTable(fakeItemMappedTable);
    List<Key> fakeItemResults2 = resultsPage.unprocessedKeysForTable(fakeItemWithSortMappedTable);
    assertThat(fakeItemResults1, empty());
    assertThat(fakeItemResults2, empty());
}
Also used : BatchGetResultPage(software.amazon.awssdk.enhanced.dynamodb.model.BatchGetResultPage) BatchGetItemResponse(software.amazon.awssdk.services.dynamodb.model.BatchGetItemResponse) Key(software.amazon.awssdk.enhanced.dynamodb.Key) Test(org.junit.Test)

Example 5 with BatchGetItemResponse

use of software.amazon.awssdk.services.dynamodb.model.BatchGetItemResponse in project aws-sdk-java-v2 by aws.

the class BatchGetItemOperationTest method getServiceCall_usingShortcutForm_makesTheRightCallAndReturnsResponse.

@Test
public void getServiceCall_usingShortcutForm_makesTheRightCallAndReturnsResponse() {
    BatchGetItemEnhancedRequest batchGetItemEnhancedRequest = BatchGetItemEnhancedRequest.builder().readBatches(ReadBatch.builder(FakeItem.class).mappedTableResource(fakeItemMappedTable).addGetItem(FAKE_ITEM_KEYS.get(0)).build()).build();
    BatchGetItemOperation operation = BatchGetItemOperation.create(batchGetItemEnhancedRequest);
    BatchGetItemRequest batchGetItemRequest = BatchGetItemRequest.builder().requestItems(singletonMap("test-table", KeysAndAttributes.builder().keys(singletonList(FAKE_ITEM_MAPS.get(0))).build())).build();
    BatchGetItemIterable expectedResponse = mock(BatchGetItemIterable.class);
    when(mockDynamoDbClient.batchGetItemPaginator(any(BatchGetItemRequest.class))).thenReturn(expectedResponse);
    SdkIterable<BatchGetItemResponse> response = operation.serviceCall(mockDynamoDbClient).apply(batchGetItemRequest);
    assertThat(response, sameInstance(expectedResponse));
    verify(mockDynamoDbClient).batchGetItemPaginator(batchGetItemRequest);
}
Also used : BatchGetItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.BatchGetItemEnhancedRequest) BatchGetItemRequest(software.amazon.awssdk.services.dynamodb.model.BatchGetItemRequest) BatchGetItemResponse(software.amazon.awssdk.services.dynamodb.model.BatchGetItemResponse) BatchGetItemIterable(software.amazon.awssdk.services.dynamodb.paginators.BatchGetItemIterable) FakeItem.createUniqueFakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem.createUniqueFakeItem) FakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem) Test(org.junit.Test)

Aggregations

BatchGetItemResponse (software.amazon.awssdk.services.dynamodb.model.BatchGetItemResponse)16 KeysAndAttributes (software.amazon.awssdk.services.dynamodb.model.KeysAndAttributes)11 HashMap (java.util.HashMap)10 List (java.util.List)9 Map (java.util.Map)9 Test (org.junit.Test)9 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)9 Collections (java.util.Collections)7 BatchGetItemRequest (software.amazon.awssdk.services.dynamodb.model.BatchGetItemRequest)7 ArrayList (java.util.ArrayList)6 Collectors (java.util.stream.Collectors)6 BatchGetResultPage (software.amazon.awssdk.enhanced.dynamodb.model.BatchGetResultPage)6 Collection (java.util.Collection)5 DynamoDbClient (software.amazon.awssdk.services.dynamodb.DynamoDbClient)5 Optional (java.util.Optional)4 Key (software.amazon.awssdk.enhanced.dynamodb.Key)4 FakeItem (software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem)4 FakeItem.createUniqueFakeItem (software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem.createUniqueFakeItem)4 PutItemRequest (software.amazon.awssdk.services.dynamodb.model.PutItemRequest)4 QueryRequest (software.amazon.awssdk.services.dynamodb.model.QueryRequest)4