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]
}
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);
}
}
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);
}
}
Aggregations