Search in sources :

Example 1 with SdkAutoConstructMap

use of software.amazon.awssdk.core.util.SdkAutoConstructMap in project aws-sdk-java-v2 by aws.

the class EnhancedAttributeValue method toAttributeValueMap.

/**
 * Convert this {@link EnhancedAttributeValue} into a generated {@code Map<String, AttributeValue>}.
 *
 * <p>
 * This call will fail with a {@link RuntimeException} if {@link #isMap()} is false.
 */
public Map<String, AttributeValue> toAttributeValueMap() {
    Validate.validState(isMap(), "Cannot convert an attribute value of type %s to a generated item. Must be %s.", type(), AttributeValueType.M);
    AttributeValue generatedAttributeValue = toAttributeValue();
    Validate.validState(generatedAttributeValue.m() != null && !(generatedAttributeValue.m() instanceof SdkAutoConstructMap), "Map EnhancedAttributeValue was not converted into a Map AttributeValue.");
    return generatedAttributeValue.m();
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) SdkAutoConstructMap(software.amazon.awssdk.core.util.SdkAutoConstructMap)

Example 2 with SdkAutoConstructMap

use of software.amazon.awssdk.core.util.SdkAutoConstructMap in project aws-sdk-java-v2 by aws.

the class DynamoServiceIntegrationTest method testServiceOperations.

/**
 * Tests that we can call each service operation to create and describe
 * tables, put, update and delete data, and query.
 */
@Test
public void testServiceOperations() throws Exception {
    // Describe all tables
    ListTablesResponse describeTablesResult = dynamo.listTables(ListTablesRequest.builder().build());
    // Describe our new table
    DescribeTableRequest describeTablesRequest = DescribeTableRequest.builder().tableName(tableName).build();
    TableDescription tableDescription = dynamo.describeTable(describeTablesRequest).table();
    assertEquals(tableName, tableDescription.tableName());
    assertNotNull(tableDescription.tableStatus());
    assertEquals(HASH_KEY_NAME, tableDescription.keySchema().get(0).attributeName());
    assertEquals(KeyType.HASH, tableDescription.keySchema().get(0).keyType());
    assertNotNull(tableDescription.provisionedThroughput().numberOfDecreasesToday());
    assertEquals(READ_CAPACITY, tableDescription.provisionedThroughput().readCapacityUnits());
    assertEquals(WRITE_CAPACITY, tableDescription.provisionedThroughput().writeCapacityUnits());
    // Add some data
    int contentLength = 1 * 1024;
    Set<SdkBytes> byteBufferSet = new HashSet<SdkBytes>();
    byteBufferSet.add(SdkBytes.fromByteArray(generateByteArray(contentLength)));
    byteBufferSet.add(SdkBytes.fromByteArray(generateByteArray(contentLength + 1)));
    Map<String, AttributeValue> item = new HashMap<String, AttributeValue>();
    item.put(HASH_KEY_NAME, AttributeValue.builder().s("bar").build());
    item.put("age", AttributeValue.builder().n("30").build());
    item.put("bar", AttributeValue.builder().s("" + System.currentTimeMillis()).build());
    item.put("foos", AttributeValue.builder().ss("bleh", "blah").build());
    item.put("S", AttributeValue.builder().ss("ONE", "TWO").build());
    item.put("blob", AttributeValue.builder().b(SdkBytes.fromByteArray(generateByteArray(contentLength))).build());
    item.put("blobs", AttributeValue.builder().bs(SdkBytes.fromByteArray(generateByteArray(contentLength)), SdkBytes.fromByteArray(generateByteArray(contentLength + 1))).build());
    item.put("BS", AttributeValue.builder().bs(byteBufferSet).build());
    PutItemRequest putItemRequest = PutItemRequest.builder().tableName(tableName).item(item).returnValues(ReturnValue.ALL_OLD.toString()).build();
    PutItemResponse putItemResult = dynamo.putItem(putItemRequest);
    // Get our new item
    GetItemResponse itemResult = dynamo.getItem(GetItemRequest.builder().tableName(tableName).key(mapKey(HASH_KEY_NAME, AttributeValue.builder().s("bar").build())).consistentRead(true).build());
    assertNotNull(itemResult.item().get("S").ss());
    assertEquals(2, itemResult.item().get("S").ss().size());
    assertTrue(itemResult.item().get("S").ss().contains("ONE"));
    assertTrue(itemResult.item().get("S").ss().contains("TWO"));
    assertEquals("30", itemResult.item().get("age").n());
    assertNotNull(itemResult.item().get("bar").s());
    assertNotNull(itemResult.item().get("blob").b());
    assertTrue(itemResult.item().get("blob").b().equals(SdkBytes.fromByteArray(generateByteArray(contentLength))));
    assertNotNull(itemResult.item().get("blobs").bs());
    assertEquals(2, itemResult.item().get("blobs").bs().size());
    assertTrue(itemResult.item().get("blobs").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength))));
    assertTrue(itemResult.item().get("blobs").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength + 1))));
    assertNotNull(itemResult.item().get("BS").bs());
    assertEquals(2, itemResult.item().get("BS").bs().size());
    assertTrue(itemResult.item().get("BS").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength))));
    assertTrue(itemResult.item().get("BS").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength + 1))));
    // Pause to try and deal with ProvisionedThroughputExceededExceptions
    Thread.sleep(1000 * 5);
    // Add some data into the table with binary hash key
    ByteBuffer byteBuffer = ByteBuffer.allocate(contentLength * 2);
    byteBuffer.put(generateByteArray(contentLength));
    byteBuffer.flip();
    item = new HashMap<String, AttributeValue>();
    item.put(HASH_KEY_NAME, AttributeValue.builder().b(SdkBytes.fromByteBuffer(byteBuffer)).build());
    // Reuse the byteBuffer
    item.put("blob", AttributeValue.builder().b(SdkBytes.fromByteBuffer(byteBuffer)).build());
    item.put("blobs", AttributeValue.builder().bs(SdkBytes.fromByteArray(generateByteArray(contentLength)), SdkBytes.fromByteArray(generateByteArray(contentLength + 1))).build());
    // Reuse the byteBufferSet
    item.put("BS", AttributeValue.builder().bs(byteBufferSet).build());
    putItemRequest = PutItemRequest.builder().tableName(binaryKeyTableName).item(item).returnValues(ReturnValue.ALL_OLD.toString()).build();
    dynamo.putItem(putItemRequest);
    // Get our new item
    itemResult = dynamo.getItem(GetItemRequest.builder().tableName(binaryKeyTableName).key(mapKey(HASH_KEY_NAME, AttributeValue.builder().b(SdkBytes.fromByteBuffer(byteBuffer)).build())).consistentRead(true).build());
    assertNotNull(itemResult.item().get("blob").b());
    assertEquals(itemResult.item().get("blob").b(), SdkBytes.fromByteArray(generateByteArray(contentLength)));
    assertNotNull(itemResult.item().get("blobs").bs());
    assertEquals(2, itemResult.item().get("blobs").bs().size());
    assertTrue(itemResult.item().get("blobs").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength))));
    assertTrue(itemResult.item().get("blobs").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength + 1))));
    assertNotNull(itemResult.item().get("BS").bs());
    assertEquals(2, itemResult.item().get("BS").bs().size());
    assertTrue(itemResult.item().get("BS").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength))));
    assertTrue(itemResult.item().get("BS").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength + 1))));
    // Pause to try and deal with ProvisionedThroughputExceededExceptions
    Thread.sleep(1000 * 5);
    // Load some random data
    System.out.println("Loading data...");
    Random random = new Random();
    for (int i = 0; i < 50; i++) {
        item = new HashMap<String, AttributeValue>();
        item.put(HASH_KEY_NAME, AttributeValue.builder().s("bar-" + System.currentTimeMillis()).build());
        item.put("age", AttributeValue.builder().n(Integer.toString(random.nextInt(100) + 30)).build());
        item.put("bar", AttributeValue.builder().s("" + System.currentTimeMillis()).build());
        item.put("foos", AttributeValue.builder().ss("bleh", "blah").build());
        dynamo.putItem(PutItemRequest.builder().tableName(tableName).item(item).returnValues(ReturnValue.ALL_OLD.toString()).build());
    }
    // Update an item
    Map<String, AttributeValueUpdate> itemUpdates = new HashMap<String, AttributeValueUpdate>();
    itemUpdates.put("1", AttributeValueUpdate.builder().value(AttributeValue.builder().s("¢").build()).action(AttributeAction.PUT.toString()).build());
    itemUpdates.put("foos", AttributeValueUpdate.builder().value(AttributeValue.builder().ss("foo").build()).action(AttributeAction.PUT.toString()).build());
    itemUpdates.put("S", AttributeValueUpdate.builder().value(AttributeValue.builder().ss("THREE").build()).action(AttributeAction.ADD.toString()).build());
    itemUpdates.put("age", AttributeValueUpdate.builder().value(AttributeValue.builder().n("10").build()).action(AttributeAction.ADD.toString()).build());
    itemUpdates.put("blob", AttributeValueUpdate.builder().value(AttributeValue.builder().b(SdkBytes.fromByteArray(generateByteArray(contentLength + 1))).build()).action(AttributeAction.PUT.toString()).build());
    itemUpdates.put("blobs", AttributeValueUpdate.builder().value(AttributeValue.builder().bs(SdkBytes.fromByteArray(generateByteArray(contentLength))).build()).action(AttributeAction.PUT.toString()).build());
    UpdateItemRequest updateItemRequest = UpdateItemRequest.builder().tableName(tableName).key(mapKey(HASH_KEY_NAME, AttributeValue.builder().s("bar").build())).attributeUpdates(itemUpdates).returnValues("ALL_NEW").build();
    UpdateItemResponse updateItemResult = dynamo.updateItem(updateItemRequest);
    assertEquals("¢", updateItemResult.attributes().get("1").s());
    assertEquals(1, updateItemResult.attributes().get("foos").ss().size());
    assertTrue(updateItemResult.attributes().get("foos").ss().contains("foo"));
    assertEquals(3, updateItemResult.attributes().get("S").ss().size());
    assertTrue(updateItemResult.attributes().get("S").ss().contains("ONE"));
    assertTrue(updateItemResult.attributes().get("S").ss().contains("TWO"));
    assertTrue(updateItemResult.attributes().get("S").ss().contains("THREE"));
    assertEquals(Integer.toString(30 + 10), updateItemResult.attributes().get("age").n());
    assertEquals(updateItemResult.attributes().get("blob").b(), SdkBytes.fromByteArray(generateByteArray(contentLength + 1)));
    assertEquals(1, updateItemResult.attributes().get("blobs").bs().size());
    assertTrue(updateItemResult.attributes().get("blobs").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength))));
    itemUpdates.clear();
    itemUpdates.put("age", AttributeValueUpdate.builder().value(AttributeValue.builder().n("30").build()).action(AttributeAction.PUT.toString()).build());
    itemUpdates.put("blobs", AttributeValueUpdate.builder().value(AttributeValue.builder().bs(SdkBytes.fromByteArray(generateByteArray(contentLength + 1))).build()).action(AttributeAction.ADD.toString()).build());
    updateItemRequest = UpdateItemRequest.builder().tableName(tableName).key(mapKey(HASH_KEY_NAME, AttributeValue.builder().s("bar").build())).attributeUpdates(itemUpdates).returnValues("ALL_NEW").build();
    updateItemResult = dynamo.updateItem(updateItemRequest);
    assertEquals("30", updateItemResult.attributes().get("age").n());
    assertEquals(2, updateItemResult.attributes().get("blobs").bs().size());
    assertTrue(updateItemResult.attributes().get("blobs").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength))));
    assertTrue(updateItemResult.attributes().get("blobs").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength + 1))));
    // Get an item that doesn't exist.
    GetItemRequest itemsRequest = GetItemRequest.builder().tableName(tableName).key(mapKey(HASH_KEY_NAME, AttributeValue.builder().s("3").build())).consistentRead(true).build();
    GetItemResponse itemsResult = dynamo.getItem(itemsRequest);
    assertTrue(itemsResult.item() instanceof SdkAutoConstructMap);
    // Get an item that doesn't have any attributes,
    itemsRequest = GetItemRequest.builder().tableName(tableName).key(mapKey(HASH_KEY_NAME, AttributeValue.builder().s("bar").build())).consistentRead(true).attributesToGet("non-existent-attribute").build();
    itemsResult = dynamo.getItem(itemsRequest);
    assertEquals(0, itemsResult.item().size());
    // Scan data
    ScanRequest scanRequest = ScanRequest.builder().tableName(tableName).attributesToGet(HASH_KEY_NAME).build();
    ScanResponse scanResult = dynamo.scan(scanRequest);
    assertTrue(scanResult.count() > 0);
    assertTrue(scanResult.scannedCount() > 0);
    // Try a more advanced Scan query and run it a few times for performance metrics
    System.out.println("Testing Scan...");
    for (int i = 0; i < 10; i++) {
        HashMap<String, Condition> scanFilter = new HashMap<String, Condition>();
        scanFilter.put("age", Condition.builder().attributeValueList(AttributeValue.builder().n("40").build()).comparisonOperator(ComparisonOperator.GT.toString()).build());
        scanRequest = ScanRequest.builder().tableName(tableName).scanFilter(scanFilter).build();
        scanResult = dynamo.scan(scanRequest);
    }
    // Batch write
    HashMap<String, List<WriteRequest>> requestItems = new HashMap<String, List<WriteRequest>>();
    List<WriteRequest> writeRequests = new ArrayList<WriteRequest>();
    HashMap<String, AttributeValue> writeAttributes = new HashMap<String, AttributeValue>();
    writeAttributes.put(HASH_KEY_NAME, AttributeValue.builder().s("" + System.currentTimeMillis()).build());
    writeAttributes.put("bar", AttributeValue.builder().s("" + System.currentTimeMillis()).build());
    writeRequests.add(WriteRequest.builder().putRequest(PutRequest.builder().item(writeAttributes).build()).build());
    writeRequests.add(WriteRequest.builder().deleteRequest(DeleteRequest.builder().key(mapKey(HASH_KEY_NAME, AttributeValue.builder().s("toDelete").build())).build()).build());
    requestItems.put(tableName, writeRequests);
    BatchWriteItemResponse batchWriteItem = dynamo.batchWriteItem(BatchWriteItemRequest.builder().requestItems(requestItems).build());
    // assertNotNull(batchWriteItem.itemCollectionMetrics());
    // assertEquals(1, batchWriteItem.itemCollectionMetrics().size());
    // assertEquals(tableName, batchWriteItem.itemCollectionMetrics().entrySet().iterator().next().get);
    // assertNotNull(tableName, batchWriteItem.getResponses().iterator().next().getCapacityUnits());
    assertNotNull(batchWriteItem.unprocessedItems());
    assertTrue(batchWriteItem.unprocessedItems().isEmpty());
    // Delete some data
    DeleteItemRequest deleteItemRequest = DeleteItemRequest.builder().tableName(tableName).key(mapKey(HASH_KEY_NAME, AttributeValue.builder().s("jeep").build())).returnValues(ReturnValue.ALL_OLD.toString()).build();
    DeleteItemResponse deleteItemResult = dynamo.deleteItem(deleteItemRequest);
    // Delete our table
    DeleteTableResponse deleteTable = dynamo.deleteTable(DeleteTableRequest.builder().tableName(tableName).build());
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) HashMap(java.util.HashMap) PutItemResponse(software.amazon.awssdk.services.dynamodb.model.PutItemResponse) ArrayList(java.util.ArrayList) TableDescription(software.amazon.awssdk.services.dynamodb.model.TableDescription) SdkBytes(software.amazon.awssdk.core.SdkBytes) Random(java.util.Random) PutItemRequest(software.amazon.awssdk.services.dynamodb.model.PutItemRequest) DeleteItemResponse(software.amazon.awssdk.services.dynamodb.model.DeleteItemResponse) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Condition(software.amazon.awssdk.services.dynamodb.model.Condition) SdkAutoConstructMap(software.amazon.awssdk.core.util.SdkAutoConstructMap) AttributeValueUpdate(software.amazon.awssdk.services.dynamodb.model.AttributeValueUpdate) WriteRequest(software.amazon.awssdk.services.dynamodb.model.WriteRequest) DeleteItemRequest(software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest) ScanResponse(software.amazon.awssdk.services.dynamodb.model.ScanResponse) DescribeTableRequest(software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest) ByteBuffer(java.nio.ByteBuffer) ScanRequest(software.amazon.awssdk.services.dynamodb.model.ScanRequest) UpdateItemResponse(software.amazon.awssdk.services.dynamodb.model.UpdateItemResponse) ListTablesResponse(software.amazon.awssdk.services.dynamodb.model.ListTablesResponse) GetItemResponse(software.amazon.awssdk.services.dynamodb.model.GetItemResponse) BatchWriteItemResponse(software.amazon.awssdk.services.dynamodb.model.BatchWriteItemResponse) DeleteTableResponse(software.amazon.awssdk.services.dynamodb.model.DeleteTableResponse) BatchGetItemRequest(software.amazon.awssdk.services.dynamodb.model.BatchGetItemRequest) GetItemRequest(software.amazon.awssdk.services.dynamodb.model.GetItemRequest) Test(org.junit.Test)

Aggregations

SdkAutoConstructMap (software.amazon.awssdk.core.util.SdkAutoConstructMap)2 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)2 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Random (java.util.Random)1 Test (org.junit.Test)1 SdkBytes (software.amazon.awssdk.core.SdkBytes)1 AttributeValueUpdate (software.amazon.awssdk.services.dynamodb.model.AttributeValueUpdate)1 BatchGetItemRequest (software.amazon.awssdk.services.dynamodb.model.BatchGetItemRequest)1 BatchWriteItemResponse (software.amazon.awssdk.services.dynamodb.model.BatchWriteItemResponse)1 Condition (software.amazon.awssdk.services.dynamodb.model.Condition)1 DeleteItemRequest (software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest)1 DeleteItemResponse (software.amazon.awssdk.services.dynamodb.model.DeleteItemResponse)1 DeleteTableResponse (software.amazon.awssdk.services.dynamodb.model.DeleteTableResponse)1 DescribeTableRequest (software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest)1 GetItemRequest (software.amazon.awssdk.services.dynamodb.model.GetItemRequest)1 GetItemResponse (software.amazon.awssdk.services.dynamodb.model.GetItemResponse)1