本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用中的表格 DynamoDB
表是 DynamoDB 數據庫中所有項目的容器。您必須先建立資料表 DynamoDB,才能從中新增或移除資料。
對於每個資料表,您必須定義:
-
對於您的帳戶和區域而言是唯一的表格名稱。
-
每個值的主索引鍵都必須獨一無二,資料表中任兩個項目不能有相同的主索引鍵值。
主索引鍵可以是簡單的,包含單一分割區 (HASH) 索引鍵;也可以是複合的,包含分割區和排序 (RANGE) 索引鍵。
每個鍵值都有一個關聯的數據類型,由
ScalarAttributeType
類枚舉。索引鍵值可以是二進位 (B)、數值 (N)、或字串 (S)。如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的命名規則和資料類型。 -
佈建輸送量值用於定義為資料表預留的讀取/寫入容量單位數。
注意
Amazon DynamoDB 定價
是根據您在表格上設定的佈建輸送量值,因此請只保留您認為表格需要的容量。 資料表的佈建輸送量隨時可以修改,所以您可以在需求變更時調整容量。
建立資料表
使用該DynamoDbClient’s
createTable
方法來創建一個新的 DynamoDB 表。您需要建構資料表屬性和資料表結構描述,這兩項都會用來識別資料表的主索引鍵。您也必須提供初始佈建的輸送量值和資料表名稱。
注意
如果具有您選擇的名稱的資料表已存在,DynamoDbException
就會擲回一個。
使用簡單主索引鍵建立資料表
此程式碼會建立一個資料表,其中一個屬性是資料表的簡單主索引鍵。此範例使用AttributeDefinition
和KeySchemaElement
物件的 CreateTableRequest
匯入
import software.amazon.awssdk.core.waiters.WaiterResponse; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.model.CreateTableRequest; import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition; import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType; import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType; import software.amazon.awssdk.services.dynamodb.model.KeySchemaElement; import software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput; import software.amazon.awssdk.services.dynamodb.model.KeyType; import software.amazon.awssdk.services.dynamodb.model.CreateTableResponse; import software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest; import software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.waiters.DynamoDbWaiter;
Code
public static String createTable(DynamoDbClient ddb, String tableName, String key) { DynamoDbWaiter dbWaiter = ddb.waiter(); CreateTableRequest request = CreateTableRequest.builder() .attributeDefinitions(AttributeDefinition.builder() .attributeName(key) .attributeType(ScalarAttributeType.S) .build()) .keySchema(KeySchemaElement.builder() .attributeName(key) .keyType(KeyType.HASH) .build()) .provisionedThroughput(ProvisionedThroughput.builder() .readCapacityUnits(new Long(10)) .writeCapacityUnits(new Long(10)) .build()) .tableName(tableName) .build(); String newTable =""; try { CreateTableResponse response = ddb.createTable(request); DescribeTableRequest tableRequest = DescribeTableRequest.builder() .tableName(tableName) .build(); // Wait until the Amazon DynamoDB table is created WaiterResponse<DescribeTableResponse> waiterResponse = dbWaiter.waitUntilTableExists(tableRequest); waiterResponse.matched().response().ifPresent(System.out::println); newTable = response.tableDescription().tableName(); return newTable; } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } return ""; }
請參閱(詳見)的完整實例
使用複合主索引鍵建立資料表
下列範例會建立具有兩個屬性的資料表。這兩個屬性都用於複合主鍵。
匯入
import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition; import software.amazon.awssdk.services.dynamodb.model.CreateTableRequest; import software.amazon.awssdk.services.dynamodb.model.CreateTableResponse; import software.amazon.awssdk.services.dynamodb.model.KeySchemaElement; import software.amazon.awssdk.services.dynamodb.model.KeyType; import software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput; import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType; import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
Code
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 ""; }
請參閱(詳見)的完整實例
列出資料表
您可以通過調用該DynamoDbClient’s
listTables
方法列出特定區域中的表。
注意
如果指定的資料表不存在於您的帳戶和區域,ResourceNotFoundException
匯入
import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import software.amazon.awssdk.services.dynamodb.model.ListTablesResponse; import software.amazon.awssdk.services.dynamodb.model.ListTablesRequest; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import java.util.List;
Code
public static void listAllTables(DynamoDbClient ddb){ boolean moreTables = true; String lastName = null; while(moreTables) { try { ListTablesResponse response = null; if (lastName == null) { ListTablesRequest request = ListTablesRequest.builder().build(); response = ddb.listTables(request); } else { ListTablesRequest request = ListTablesRequest.builder() .exclusiveStartTableName(lastName).build(); response = ddb.listTables(request); } List<String> tableNames = response.tableNames(); if (tableNames.size() > 0) { for (String curName : tableNames) { System.out.format("* %s\n", curName); } } else { System.out.println("No tables found!"); System.exit(0); } lastName = response.lastEvaluatedTableName(); if (lastName == null) { moreTables = false; } } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } } System.out.println("\nDone!"); }
根據預設,每次呼叫最多會傳回 100 個表格 — 用lastEvaluatedTableName
於傳回的ListTablesResponse
請參閱(詳見)的完整實例
描述資料表 (取得其相關資訊)
使用該DynamoDbClient’s
describeTable
方法來獲取有關表的信息。
注意
如果指定的資料表不存在於您的帳戶和區域,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.AttributeDefinition; import software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest; import software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughputDescription; import software.amazon.awssdk.services.dynamodb.model.TableDescription; import java.util.List;
Code
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!"); }
請參閱(詳見)的完整實例
修改 (更新) 資料表
您可以隨時呼叫DynamoDbClient’s
updateTable
方法來修改表格的佈建輸送量值。
注意
如果指定的資料表不存在於您的帳戶和區域,ResourceNotFoundException
匯入
import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.UpdateTableRequest; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;
Code
public static void updateDynamoDBTable(DynamoDbClient ddb, String tableName, Long readCapacity, Long writeCapacity) { System.out.format( "Updating %s with new provisioned throughput values\n", tableName); System.out.format("Read capacity : %d\n", readCapacity); System.out.format("Write capacity : %d\n", writeCapacity); ProvisionedThroughput tableThroughput = ProvisionedThroughput.builder() .readCapacityUnits(readCapacity) .writeCapacityUnits(writeCapacity) .build(); UpdateTableRequest request = UpdateTableRequest.builder() .provisionedThroughput(tableThroughput) .tableName(tableName) .build(); try { ddb.updateTable(request); } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } System.out.println("Done!"); }
請參閱(詳見)的完整實例
刪除資料表
要刪除表,請調用DynamoDbClient’s
deleteTable
方法並提供表的名稱。
注意
如果指定的資料表不存在於您的帳戶和區域,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.DeleteTableRequest;
Code
public static void deleteDynamoDBTable(DynamoDbClient ddb, String tableName) { DeleteTableRequest request = DeleteTableRequest.builder() .tableName(tableName) .build(); try { ddb.deleteTable(request); } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } System.out.println(tableName +" was successfully deleted!"); }
請參閱(詳見)的完整實例
其他資訊
-
Amazon DynamoDB 開發人員指南中使用表格的準則