在 Java 中使用 DynamoDB 表 - Amazon DynamoDB
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

在 Java 中使用 DynamoDB 表

您可以使用 Amazon SDK for Java 创建、更新和删除 Amazon DynamoDB 表,以及列出您账户中所有的表或者获取有关某个特定表的信息。

创建表

要创建表,您必须提供表名称、表的主键以及预置的吞吐量值。下面的代码段会创建一个将数字类型属性 ID 用作主键的示例表。

要使用 Amazon SDK for Java API 创建表
  1. 创建 DynamoDB 类的实例。

  2. 实例化 CreateTableRequest 以提供请求信息。

    您必须提供表名称、属性定义、键架构以及预置吞吐量值。

  3. 以参数形式提供请求对象,运行 createTable 方法。

以下代码示例演示了上述步骤。

AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build(); DynamoDB dynamoDB = new DynamoDB(client); List<AttributeDefinition> attributeDefinitions= new ArrayList<AttributeDefinition>(); attributeDefinitions.add(new AttributeDefinition().withAttributeName("Id").withAttributeType("N")); List<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>(); keySchema.add(new KeySchemaElement().withAttributeName("Id").withKeyType(KeyType.HASH)); CreateTableRequest request = new CreateTableRequest() .withTableName(tableName) .withKeySchema(keySchema) .withAttributeDefinitions(attributeDefinitions) .withProvisionedThroughput(new ProvisionedThroughput() .withReadCapacityUnits(5L) .withWriteCapacityUnits(6L)); Table table = dynamoDB.createTable(request); table.waitForActive();

在 DynamoDB 创建表并且将其状态设置为 ACTIVE 之前,表将无法使用。createTable 请求返回 Table 对象,您可以用它来获取有关表的更多信息。

TableDescription tableDescription = dynamoDB.getTable(tableName).describe(); System.out.printf("%s: %s \t ReadCapacityUnits: %d \t WriteCapacityUnits: %d", tableDescription.getTableStatus(), tableDescription.getTableName(), tableDescription.getProvisionedThroughput().getReadCapacityUnits(), tableDescription.getProvisionedThroughput().getWriteCapacityUnits());

您可以随时调用客户端的 describe 方法来获取表信息。

TableDescription tableDescription = dynamoDB.getTable(tableName).describe();

更新表

您可以仅更新现有表的预置吞吐量值。您可能需要根据应用程序的要求更新这些值。

注意

有关每天的吞吐量增加和减少的更多信息,请参阅 Amazon DynamoDB 中的服务、账户和表限额

使用Amazon SDK for Java API 更新表
  1. 创建 Table 类的实例。

  2. 创建 ProvisionedThroughput 类的实例以提供新吞吐量值。

  3. 提供 ProvisionedThroughput 实例作为参数来执行 updateTable 方法。

以下代码示例演示了上述步骤。

AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build(); DynamoDB dynamoDB = new DynamoDB(client); Table table = dynamoDB.getTable("ProductCatalog"); ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput() .withReadCapacityUnits(15L) .withWriteCapacityUnits(12L); table.updateTable(provisionedThroughput); table.waitForActive();

删除表

使用Amazon SDK for Java API 删除表
  1. 创建 Table 类的实例。

  2. 创建一个 DeleteTableRequest 类的实例,并提供您想要删除的表的名称。

  3. 提供 Table 实例作为参数来执行 deleteTable 方法。

以下代码示例演示了上述步骤。

AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build(); DynamoDB dynamoDB = new DynamoDB(client); Table table = dynamoDB.getTable("ProductCatalog"); table.delete(); table.waitForDelete();

列出表

要列出您账户中的表,请创建 DynamoDB 的实例并执行 listTables 方法。ListTables 操作无需使用参数。

AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build(); DynamoDB dynamoDB = new DynamoDB(client); TableCollection<ListTablesResult> tables = dynamoDB.listTables(); Iterator<Table> iterator = tables.iterator(); while (iterator.hasNext()) { Table table = iterator.next(); System.out.println(table.getTableName()); }