处理 DynamoDB 中的表 - AWS SDK for Java 1.x

我们宣布了即将推出 end-of-support 的 AWS SDK for Java (v1)。建议您迁移到 AWS SDK for Java v2。有关日期、其他详细信息以及如何迁移的信息,请参阅链接的公告。

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

处理 DynamoDB 中的表

表是 DynamoDB 数据库中所有项目的容器。您必须先创建表,然后才能在 DynamoDB 中添加或删除数据。

对于每个表,您必须定义:

  • 名称,它对于您的账户和所在区域是唯一的。

  • 一个主键,每个值对于它都必须是唯一的;表中的任意两个项目不能具有相同的主键值。

    主键可以是简单主键(包含单个分区 (HASH) 键)或复合主键(包含一个分区和一个排序 (RANGE) 键)。

    每个键值均有一个由 ScalarAttributeType 类枚举的关联的数据类型。键值可以是二进制 (B)、数字 (N) 或字符串 (S)。有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的命名规则和数据类型

  • 预置吞吐量值,这些值定义为表保留的读取/写入容量单位数。

    注意

    Amazon DynamoDB 定价基于您为表设置的预置吞吐量值,因此您应只为表保留可能需要的容量。

表的预置吞吐量可随时修改,以便您能够在需要更改时调整容量。

创建表

使用 DynamoDB 客户端createTable 方法可创建新的 DynamoDB 表。您需要构造表属性和表架构,二者用于标识表的主键。您还必须提供初始预置吞吐量值和表名。仅在创建 DynamoDB 表时定义键表属性。

注意

如果使用您所选名称的表已存在,则将引发 AmazonServiceException

导入

import com.amazonaws.AmazonServiceException; import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; import com.amazonaws.services.dynamodbv2.model.AttributeDefinition; import com.amazonaws.services.dynamodbv2.model.CreateTableRequest; import com.amazonaws.services.dynamodbv2.model.CreateTableResult; import com.amazonaws.services.dynamodbv2.model.KeySchemaElement; import com.amazonaws.services.dynamodbv2.model.KeyType; import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput; import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType;

创建具有简单主键的表

此代码使用简单主键 (“Name”) 创建表。

代码

CreateTableRequest request = new CreateTableRequest() .withAttributeDefinitions(new AttributeDefinition( "Name", ScalarAttributeType.S)) .withKeySchema(new KeySchemaElement("Name", KeyType.HASH)) .withProvisionedThroughput(new ProvisionedThroughput( new Long(10), new Long(10))) .withTableName(table_name); final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient(); try { CreateTableResult result = ddb.createTable(request); System.out.println(result.getTableDescription().getTableName()); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); }

请参阅 GitHub 上的完整示例

创建具有复合主键的表

添加另一个 AttributeDefinitionKeySchemaElementCreateTableRequest

代码

CreateTableRequest request = new CreateTableRequest() .withAttributeDefinitions( new AttributeDefinition("Language", ScalarAttributeType.S), new AttributeDefinition("Greeting", ScalarAttributeType.S)) .withKeySchema( new KeySchemaElement("Language", KeyType.HASH), new KeySchemaElement("Greeting", KeyType.RANGE)) .withProvisionedThroughput( new ProvisionedThroughput(new Long(10), new Long(10))) .withTableName(table_name);

请参阅 GitHub 上的完整示例

列出表

您可以通过调用 DynamoDB 客户端listTables 方法列出特定区域中的表。

注意

如果您的账户和区域没有该已命名的表,则将引发 ResourceNotFoundException 异常。

导入

import com.amazonaws.AmazonServiceException; import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; import com.amazonaws.services.dynamodbv2.model.ListTablesRequest; import com.amazonaws.services.dynamodbv2.model.ListTablesResult;

代码

final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient(); ListTablesRequest request; boolean more_tables = true; String last_name = null; while(more_tables) { try { if (last_name == null) { request = new ListTablesRequest().withLimit(10); } else { request = new ListTablesRequest() .withLimit(10) .withExclusiveStartTableName(last_name); } ListTablesResult table_list = ddb.listTables(request); List<String> table_names = table_list.getTableNames(); if (table_names.size() > 0) { for (String cur_name : table_names) { System.out.format("* %s\n", cur_name); } } else { System.out.println("No tables found!"); System.exit(0); } last_name = table_list.getLastEvaluatedTableName(); if (last_name == null) { more_tables = false; }

默认情况下,每次调用将返回最多 100 个表 – 对返回的 ListTablesResult 对象使用 getLastEvaluatedTableName 可获得评估的上一个表。可使用此值在上一列出的最后一个返回值后开始列出。

请参阅 GitHub 上的完整示例

描述表(获取相关信息)

调用 DynamoDB 客户端describeTable 方法。

注意

如果您的账户和区域没有该已命名的表,则将引发 ResourceNotFoundException 异常。

导入

import com.amazonaws.AmazonServiceException; import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; import com.amazonaws.services.dynamodbv2.model.AttributeDefinition; import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughputDescription; import com.amazonaws.services.dynamodbv2.model.TableDescription;

代码

final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient(); try { TableDescription table_info = ddb.describeTable(table_name).getTable(); if (table_info != null) { System.out.format("Table name : %s\n", table_info.getTableName()); System.out.format("Table ARN : %s\n", table_info.getTableArn()); System.out.format("Status : %s\n", table_info.getTableStatus()); System.out.format("Item count : %d\n", table_info.getItemCount().longValue()); System.out.format("Size (bytes): %d\n", table_info.getTableSizeBytes().longValue()); ProvisionedThroughputDescription throughput_info = table_info.getProvisionedThroughput(); System.out.println("Throughput"); System.out.format(" Read Capacity : %d\n", throughput_info.getReadCapacityUnits().longValue()); System.out.format(" Write Capacity: %d\n", throughput_info.getWriteCapacityUnits().longValue()); List<AttributeDefinition> attributes = table_info.getAttributeDefinitions(); System.out.println("Attributes"); for (AttributeDefinition a : attributes) { System.out.format(" %s (%s)\n", a.getAttributeName(), a.getAttributeType()); } } } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); }

请参阅 GitHub 上的完整示例

修改(更新)表

您可以通过调用 DynamoDB 客户端updateTable 方法随时修改表的预置吞吐量值。

注意

如果您的账户和区域没有该已命名的表,则将引发 ResourceNotFoundException 异常。

导入

import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput; import com.amazonaws.AmazonServiceException;

代码

ProvisionedThroughput table_throughput = new ProvisionedThroughput( read_capacity, write_capacity); final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient(); try { ddb.updateTable(table_name, table_throughput); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); }

请参阅 GitHub 上的完整示例

删除表

调用 DynamoDB 客户端deleteTable 方法,并向其传递表名称。

注意

如果您的账户和区域没有该已命名的表,则将引发 ResourceNotFoundException 异常。

导入

import com.amazonaws.AmazonServiceException; import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;

代码

final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient(); try { ddb.deleteTable(table_name); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); }

请参阅 GitHub 上的完整示例

更多信息