本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用中的項目 DynamoDB
在 DynamoDB 中,項目是屬性的集合,每個屬性都有名稱和值。屬性值可以是純量、集合或文件類型。如需詳細資訊,請參閱Amazon DynamoDB開發人員指南中的命名規則和資料類型。
從資料表擷取 (取得) 項目
呼叫 DynamoDbClient的getItem
方法,並傳遞一個GetItemRequestGetItemRequest
來擷取特定的屬性。
您可以使用傳回GetItemResponse
物件的item()
方法來擷取與項目相關聯之索引鍵 (String AttributeValue
匯入
import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.AttributeValue; import software.amazon.awssdk.services.dynamodb.model.GetItemRequest; import java.util.HashMap; import java.util.Map; import java.util.Set;
Code
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); } }
請參閱(詳見)的完整實例
使用非同步用戶端從資料表擷取 (取得) 項目
叫用的getItem
方法, DynamoDbAsyncClient 並將其傳遞給具有您想要之項目之資料表名稱和主索引鍵值的GetItemRequest
您可以傳回一個集合
匯入
import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.model.GetItemRequest; import software.amazon.awssdk.services.dynamodb.model.AttributeValue; import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;
Code
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); }
請參閱(詳見)的完整實例
將新項目新增至資料表
建立代表項目屬性的索引鍵值組對應
注意
如果您的帳戶和區域不存在具名資料表,ResourceNotFoundException
匯入
import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.AttributeValue; import software.amazon.awssdk.services.dynamodb.model.PutItemRequest; import software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException; import java.util.HashMap;
Code
public static void putItemInTable(DynamoDbClient ddb, String tableName, String key, String keyVal, String albumTitle, String albumTitleValue, String awards, String awardVal, String songTitle, String songTitleVal){ HashMap<String,AttributeValue> itemValues = new HashMap<String,AttributeValue>(); // Add all content to the table itemValues.put(key, AttributeValue.builder().s(keyVal).build()); itemValues.put(songTitle, AttributeValue.builder().s(songTitleVal).build()); itemValues.put(albumTitle, AttributeValue.builder().s(albumTitleValue).build()); itemValues.put(awards, AttributeValue.builder().s(awardVal).build()); PutItemRequest request = PutItemRequest.builder() .tableName(tableName) .item(itemValues) .build(); try { ddb.putItem(request); System.out.println(tableName +" was successfully updated"); } catch (ResourceNotFoundException e) { System.err.format("Error: The Amazon DynamoDB table \"%s\" can't be found.\n", tableName); System.err.println("Be sure that it exists and that you've typed its name correctly!"); System.exit(1); } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } }
請參閱(詳見)的完整實例
更新資料表中的現有項目
您可以使用 DynamoDbClient 的 updateItem
方法,提供資料表名稱、主索引鍵值和要更新的欄位對應,更新資料表中已存在項目的屬性。
注意
如果您的帳戶和區域不存在具名資料表,或者您傳入的主索引鍵所識別的項目不存在,ResourceNotFoundException
匯入
import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import software.amazon.awssdk.services.dynamodb.model.AttributeAction; import software.amazon.awssdk.services.dynamodb.model.AttributeValue; import software.amazon.awssdk.services.dynamodb.model.AttributeValueUpdate; import software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException; import software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import java.util.HashMap;
Code
public static void updateTableItem(DynamoDbClient ddb, String tableName, String key, String keyVal, String name, String updateVal){ HashMap<String,AttributeValue> itemKey = new HashMap<String,AttributeValue>(); itemKey.put(key, AttributeValue.builder().s(keyVal).build()); HashMap<String,AttributeValueUpdate> updatedValues = new HashMap<String,AttributeValueUpdate>(); // Update the column specified by name with updatedVal updatedValues.put(name, AttributeValueUpdate.builder() .value(AttributeValue.builder().s(updateVal).build()) .action(AttributeAction.PUT) .build()); UpdateItemRequest request = UpdateItemRequest.builder() .tableName(tableName) .key(itemKey) .attributeUpdates(updatedValues) .build(); try { ddb.updateItem(request); } catch (ResourceNotFoundException e) { System.err.println(e.getMessage()); System.exit(1); } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } System.out.println("Done!"); }
請參閱(詳見)的完整實例
刪除資料表中的現有項目
您可以使用的deleteItem
方法並提供資料表名稱以及主索引鍵值來刪除資料表中存在的項目。 DynamoDbClient
注意
如果您的帳戶和區域不存在具名資料表,或者您傳入的主索引鍵所識別的項目不存在,ResourceNotFoundException
匯入
import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.AttributeValue; import software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import java.util.HashMap;
Code
public static void deleteDynamoDBItem(DynamoDbClient ddb, String tableName, String key, String keyVal) { HashMap<String,AttributeValue> keyToGet = new HashMap<String,AttributeValue>(); keyToGet.put(key, AttributeValue.builder() .s(keyVal) .build()); DeleteItemRequest deleteReq = DeleteItemRequest.builder() .tableName(tableName) .key(keyToGet) .build(); try { ddb.deleteItem(deleteReq); } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } }
請參閱(詳見)的完整實例
其他資訊
-
Amazon DynamoDB開發人員指南中的項目使用指南