Search in sources :

Example 1 with DynamoDbException

use of software.amazon.awssdk.services.dynamodb.model.DynamoDbException in project aws-doc-sdk-examples by awsdocs.

the class DynamoDBAsyncGetItem method getItem.

// snippet-start:[dynamoasyc.java2.get_item.main]
public static void getItem(DynamoDbAsyncClient client, String tableName, String key, String keyVal) {
    HashMap<String, AttributeValue> keyToGet = new HashMap<String, AttributeValue>();
    keyToGet.put(key, AttributeValue.builder().s(keyVal).build());
    try {
        // Create a GetItemRequest instance
        GetItemRequest request = GetItemRequest.builder().key(keyToGet).tableName(tableName).build();
        // Invoke the DynamoDbAsyncClient object's getItem
        java.util.Collection<AttributeValue> returnedItem = client.getItem(request).join().item().values();
        // Convert Set to Map
        Map<String, AttributeValue> map = returnedItem.stream().collect(Collectors.toMap(AttributeValue::s, s -> s));
        Set<String> keys = map.keySet();
        for (String sinKey : keys) {
            System.out.format("%s: %s\n", sinKey, map.get(sinKey).toString());
        }
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
// snippet-end:[dynamoasyc.java2.get_item.main]
}
Also used : DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) GetItemRequest(software.amazon.awssdk.services.dynamodb.model.GetItemRequest) Map(java.util.Map) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) DynamoDbAsyncClient(software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient) Set(java.util.Set) HashMap(java.util.HashMap) Region(software.amazon.awssdk.regions.Region) Collectors(java.util.stream.Collectors) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) HashMap(java.util.HashMap) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) GetItemRequest(software.amazon.awssdk.services.dynamodb.model.GetItemRequest)

Example 2 with DynamoDbException

use of software.amazon.awssdk.services.dynamodb.model.DynamoDbException in project aws-doc-sdk-examples by awsdocs.

the class EnhancedQueryRecordsWithSortKey method queryTableSort.

// snippet-start:[dynamodb.java2.mapping.querykey.main]
public static void queryTableSort(DynamoDbEnhancedClient enhancedClient) {
    try {
        // Create a DynamoDbTable object based on Issues.
        DynamoDbTable<Issues> table = enhancedClient.table("Issues", TableSchema.fromBean(Issues.class));
        String dateVal = "2013-11-19";
        DynamoDbIndex<Issues> secIndex = enhancedClient.table("Issues", TableSchema.fromBean(Issues.class)).index("dueDateIndex");
        AttributeValue attVal = AttributeValue.builder().s(dateVal).build();
        // Create a QueryConditional object that's used in the query operation.
        QueryConditional queryConditional = QueryConditional.keyEqualTo(Key.builder().partitionValue(attVal).build());
        // Get items in the Issues table.
        SdkIterable<Page<Issues>> results = secIndex.query(QueryEnhancedRequest.builder().queryConditional(queryConditional).build());
        AtomicInteger atomicInteger = new AtomicInteger();
        atomicInteger.set(0);
        results.forEach(page -> {
            Issues issue = (Issues) page.items().get(atomicInteger.get());
            System.out.println("The issue title is " + issue.getTitle());
            atomicInteger.incrementAndGet();
        });
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    System.out.println("Done");
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) QueryConditional(software.amazon.awssdk.enhanced.dynamodb.model.QueryConditional) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) Page(software.amazon.awssdk.enhanced.dynamodb.model.Page)

Example 3 with DynamoDbException

use of software.amazon.awssdk.services.dynamodb.model.DynamoDbException in project aws-doc-sdk-examples by awsdocs.

the class CreateTableCompositeKey method createTableComKey.

// snippet-start:[dynamodb.java2.create_table_composite_key.main]
public static String createTableComKey(DynamoDbClient ddb, String tableName) {
    CreateTableRequest request = CreateTableRequest.builder().attributeDefinitions(AttributeDefinition.builder().attributeName("Language").attributeType(ScalarAttributeType.S).build(), AttributeDefinition.builder().attributeName("Greeting").attributeType(ScalarAttributeType.S).build()).keySchema(KeySchemaElement.builder().attributeName("Language").keyType(KeyType.HASH).build(), KeySchemaElement.builder().attributeName("Greeting").keyType(KeyType.RANGE).build()).provisionedThroughput(ProvisionedThroughput.builder().readCapacityUnits(new Long(10)).writeCapacityUnits(new Long(10)).build()).tableName(tableName).build();
    String tableId = "";
    try {
        CreateTableResponse result = ddb.createTable(request);
        tableId = result.tableDescription().tableId();
        return tableId;
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    return "";
}
Also used : DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) CreateTableResponse(software.amazon.awssdk.services.dynamodb.model.CreateTableResponse) CreateTableRequest(software.amazon.awssdk.services.dynamodb.model.CreateTableRequest)

Example 4 with DynamoDbException

use of software.amazon.awssdk.services.dynamodb.model.DynamoDbException in project aws-doc-sdk-examples by awsdocs.

the class DescribeTable method describeDymamoDBTable.

// snippet-start:[dynamodb.java2.describe_table.main]
public static void describeDymamoDBTable(DynamoDbClient ddb, String tableName) {
    DescribeTableRequest request = DescribeTableRequest.builder().tableName(tableName).build();
    try {
        TableDescription tableInfo = ddb.describeTable(request).table();
        if (tableInfo != null) {
            System.out.format("Table name  : %s\n", tableInfo.tableName());
            System.out.format("Table ARN   : %s\n", tableInfo.tableArn());
            System.out.format("Status      : %s\n", tableInfo.tableStatus());
            System.out.format("Item count  : %d\n", tableInfo.itemCount().longValue());
            System.out.format("Size (bytes): %d\n", tableInfo.tableSizeBytes().longValue());
            ProvisionedThroughputDescription throughputInfo = tableInfo.provisionedThroughput();
            System.out.println("Throughput");
            System.out.format("  Read Capacity : %d\n", throughputInfo.readCapacityUnits().longValue());
            System.out.format("  Write Capacity: %d\n", throughputInfo.writeCapacityUnits().longValue());
            List<AttributeDefinition> attributes = tableInfo.attributeDefinitions();
            System.out.println("Attributes");
            for (AttributeDefinition a : attributes) {
                System.out.format("  %s (%s)\n", a.attributeName(), a.attributeType());
            }
        }
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    System.out.println("\nDone!");
}
Also used : DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) AttributeDefinition(software.amazon.awssdk.services.dynamodb.model.AttributeDefinition) DescribeTableRequest(software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest) ProvisionedThroughputDescription(software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughputDescription) TableDescription(software.amazon.awssdk.services.dynamodb.model.TableDescription)

Example 5 with DynamoDbException

use of software.amazon.awssdk.services.dynamodb.model.DynamoDbException in project aws-doc-sdk-examples by awsdocs.

the class EnhancedBatchWriteItems method putBatchRecords.

// snippet-start:[dynamodb.java2.mapping.batchitems.main]
public static void putBatchRecords(DynamoDbEnhancedClient enhancedClient) {
    try {
        DynamoDbTable<Customer> mappedTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class));
        LocalDate localDate = LocalDate.parse("2020-04-07");
        LocalDateTime localDateTime = localDate.atStartOfDay();
        Instant instant = localDateTime.toInstant(ZoneOffset.UTC);
        Customer record2 = new Customer();
        record2.setCustName("Fred Pink");
        record2.setId("id110");
        record2.setEmail("fredp@noserver.com");
        record2.setRegistrationDate(instant);
        Customer record3 = new Customer();
        record3.setCustName("Susan Pink");
        record3.setId("id120");
        record3.setEmail("spink@noserver.com");
        record3.setRegistrationDate(instant);
        // Create a BatchWriteItemEnhancedRequest object
        BatchWriteItemEnhancedRequest batchWriteItemEnhancedRequest = BatchWriteItemEnhancedRequest.builder().writeBatches(WriteBatch.builder(Customer.class).mappedTableResource(mappedTable).addPutItem(r -> r.item(record2)).addPutItem(r -> r.item(record3)).build()).build();
        // Add these two items to the table
        enhancedClient.batchWriteItem(batchWriteItemEnhancedRequest);
        System.out.println("done");
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) DynamoDbEnhancedClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) DynamoDbTable(software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable) BatchWriteItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.BatchWriteItemEnhancedRequest) LocalDateTime(java.time.LocalDateTime) TableSchema(software.amazon.awssdk.enhanced.dynamodb.TableSchema) DynamoDbPartitionKey(software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey) Instant(java.time.Instant) DynamoDbBean(software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) DynamoDbSortKey(software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSortKey) WriteBatch(software.amazon.awssdk.enhanced.dynamodb.model.WriteBatch) LocalDate(java.time.LocalDate) ZoneOffset(java.time.ZoneOffset) Region(software.amazon.awssdk.regions.Region) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) Instant(java.time.Instant) BatchWriteItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.BatchWriteItemEnhancedRequest) LocalDate(java.time.LocalDate)

Aggregations

DynamoDbException (software.amazon.awssdk.services.dynamodb.model.DynamoDbException)33 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)13 HashMap (java.util.HashMap)10 DynamoDbEnhancedClient (software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)8 DynamoDbClient (software.amazon.awssdk.services.dynamodb.DynamoDbClient)5 Instant (java.time.Instant)4 LocalDateTime (java.time.LocalDateTime)4 QueryConditional (software.amazon.awssdk.enhanced.dynamodb.model.QueryConditional)4 Region (software.amazon.awssdk.regions.Region)4 ResourceNotFoundException (software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException)4 LocalDate (java.time.LocalDate)3 DynamoDbPartitionKey (software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey)3 DynamoDbSortKey (software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSortKey)3 CreateTableRequest (software.amazon.awssdk.services.dynamodb.model.CreateTableRequest)3 CreateTableResponse (software.amazon.awssdk.services.dynamodb.model.CreateTableResponse)3 DescribeTableRequest (software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest)3 GetItemRequest (software.amazon.awssdk.services.dynamodb.model.GetItemRequest)3 ArrayList (java.util.ArrayList)2 Expression (software.amazon.awssdk.enhanced.dynamodb.Expression)2 Key (software.amazon.awssdk.enhanced.dynamodb.Key)2