Search in sources :

Example 1 with KeysAndAttributes

use of software.amazon.awssdk.services.dynamodb.model.KeysAndAttributes 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 KeysAndAttributes

use of software.amazon.awssdk.services.dynamodb.model.KeysAndAttributes 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 KeysAndAttributes

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

the class BatchGetItemOperation method addReadRequestsToMap.

private void addReadRequestsToMap(ReadBatch readBatch, Map<String, KeysAndAttributes> readRequestMap) {
    KeysAndAttributes newKeysAndAttributes = readBatch.keysAndAttributes();
    KeysAndAttributes existingKeysAndAttributes = readRequestMap.get(readBatch.tableName());
    if (existingKeysAndAttributes == null) {
        readRequestMap.put(readBatch.tableName(), newKeysAndAttributes);
        return;
    }
    KeysAndAttributes mergedKeysAndAttributes = mergeKeysAndAttributes(existingKeysAndAttributes, newKeysAndAttributes);
    readRequestMap.put(readBatch.tableName(), mergedKeysAndAttributes);
}
Also used : KeysAndAttributes(software.amazon.awssdk.services.dynamodb.model.KeysAndAttributes)

Example 4 with KeysAndAttributes

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

the class DynamoServiceIntegrationTest method testRequestEntityTooLargeErrorHandling.

/**
 * Tests that we properly handle error responses for request entities that
 * are too large.
 */
// DISABLED because DynamoDB apparently upped their max request size; we
// should be hitting this with a unit test that simulates an appropriate
// SdkServiceException.
// @Test
public void testRequestEntityTooLargeErrorHandling() throws Exception {
    Map<String, KeysAndAttributes> items = new HashMap<String, KeysAndAttributes>();
    for (int i = 0; i < 1024; i++) {
        KeysAndAttributes kaa = KeysAndAttributes.builder().build();
        StringBuilder bigString = new StringBuilder();
        for (int j = 0; j < 1024; j++) {
            bigString.append("a");
        }
        bigString.append(i);
        items.put(bigString.toString(), kaa);
    }
    BatchGetItemRequest request = BatchGetItemRequest.builder().requestItems(items).build();
    try {
        dynamo.batchGetItem(request);
    } catch (AwsServiceException exception) {
        assertNotNull(exception.getMessage());
        assertEquals("Request entity too large", exception.awsErrorDetails().errorCode());
        assertEquals(413, exception.statusCode());
    }
}
Also used : BatchGetItemRequest(software.amazon.awssdk.services.dynamodb.model.BatchGetItemRequest) HashMap(java.util.HashMap) AwsServiceException(software.amazon.awssdk.awscore.exception.AwsServiceException) KeysAndAttributes(software.amazon.awssdk.services.dynamodb.model.KeysAndAttributes)

Example 5 with KeysAndAttributes

use of software.amazon.awssdk.services.dynamodb.model.KeysAndAttributes 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)

Aggregations

KeysAndAttributes (software.amazon.awssdk.services.dynamodb.model.KeysAndAttributes)16 HashMap (java.util.HashMap)12 BatchGetItemResponse (software.amazon.awssdk.services.dynamodb.model.BatchGetItemResponse)11 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)10 List (java.util.List)9 Map (java.util.Map)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 Test (org.junit.Test)6 DynamoDbClient (software.amazon.awssdk.services.dynamodb.DynamoDbClient)5 Collection (java.util.Collection)4 Optional (java.util.Optional)4 Key (software.amazon.awssdk.enhanced.dynamodb.Key)4 PutItemRequest (software.amazon.awssdk.services.dynamodb.model.PutItemRequest)4 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)3 SiteIdKeyGenerator.createDatabaseKey (com.formkiq.stacks.dynamodb.SiteIdKeyGenerator.createDatabaseKey)3 QueryRequest (software.amazon.awssdk.services.dynamodb.model.QueryRequest)3 QueryResponse (software.amazon.awssdk.services.dynamodb.model.QueryResponse)3