Search in sources :

Example 1 with GetItemRequest

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

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

the class GetItem method getDynamoDBItem.

// snippet-start:[dynamodb.java2.get_item.main]
public static void getDynamoDBItem(DynamoDbClient ddb, String tableName, String key, String keyVal) {
    HashMap<String, AttributeValue> keyToGet = new HashMap<String, AttributeValue>();
    keyToGet.put(key, AttributeValue.builder().s(keyVal).build());
    GetItemRequest request = GetItemRequest.builder().key(keyToGet).tableName(tableName).build();
    try {
        Map<String, AttributeValue> returnedItem = ddb.getItem(request).item();
        if (returnedItem != null) {
            Set<String> keys = returnedItem.keySet();
            System.out.println("Amazon DynamoDB table attributes: \n");
            for (String key1 : keys) {
                System.out.format("%s: %s\n", key1, returnedItem.get(key1).toString());
            }
        } else {
            System.out.format("No item found with the key %s!\n", key);
        }
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : 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 3 with GetItemRequest

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

the class Scenario method getItem.

// snippet-start:[dynamodb.java2.scenario.get_item.main]
public static void getItem(DynamoDbClient ddb) {
    HashMap<String, AttributeValue> keyToGet = new HashMap<String, AttributeValue>();
    keyToGet.put("year", AttributeValue.builder().n("1933").build());
    keyToGet.put("title", AttributeValue.builder().s("King Kong").build());
    GetItemRequest request = GetItemRequest.builder().key(keyToGet).tableName("Movies").build();
    try {
        Map<String, AttributeValue> returnedItem = ddb.getItem(request).item();
        if (returnedItem != null) {
            Set<String> keys = returnedItem.keySet();
            System.out.println("Amazon DynamoDB table attributes: \n");
            for (String key1 : keys) {
                System.out.format("%s: %s\n", key1, returnedItem.get(key1).toString());
            }
        } else {
            System.out.format("No item found with the key %s!\n", "year");
        }
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : 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)

Aggregations

HashMap (java.util.HashMap)3 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)3 DynamoDbException (software.amazon.awssdk.services.dynamodb.model.DynamoDbException)3 GetItemRequest (software.amazon.awssdk.services.dynamodb.model.GetItemRequest)3 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Region (software.amazon.awssdk.regions.Region)1 DynamoDbAsyncClient (software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient)1