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

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

使用中的表格 DynamoDB

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

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

  • 您的账户和地区唯一的表

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

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

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

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

    注意

    Amazon DynamoDB 定价基于您在表上设置的预配置吞吐量值,因此请仅根据您认为的表所需的容量预留容量。

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

创建表

使用DynamoDbClient’screateTable方法创建新 DynamoDB 表。您需要构造表属性和表架构,二者用于标识表的主键。您还必须提供初始预置吞吐量值和表名。

注意

如果使用您选择的名称的表已经存在,DynamoDbException则会抛出。

创建具有简单主键的表

此代码创建了一个表,该表的属性为表的简单主键。该示例使用了AttributeDefinitionKeySchemaElement对象。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;

代码

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 ""; }

请参阅上的完整示例 GitHub。

创建具有复合主键的表

以下示例创建带有两个属性的表。这两个属性均用于复合主键。

导入

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;

代码

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 ""; }

请参阅上的完整示例 GitHub。

列出表

您可以通过调用DynamoDbClient’slistTables方法列出特定区域中的表。

注意

如果您的账户和区域的命名表不存在,ResourceNotFoundException则会抛出 a。

导入

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;

代码

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 个表,在返回的ListTablesResponse对象lastEvaluatedTableName上使用来获取最后一个被评估的表。可使用此值在上一列出的最后一个返回值后开始列出。

请参阅上的完整示例 GitHub。

描述表(获取相关信息)

使用 DynamoDbClient’s describeTable 方法获取有关表的信息。

注意

如果您的账户和区域的命名表不存在,ResourceNotFoundException则会抛出 a。

导入

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;

代码

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!"); }

请参阅上的完整示例 GitHub。

修改(更新)表

您可以通过调用 DynamoDbClient’s updateTable 方法随时修改表的预置吞吐量值。

注意

如果您的账户和区域的命名表不存在,ResourceNotFoundException则会抛出 a。

导入

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;

代码

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!"); }

请参阅上的完整示例 GitHub。

删除表

要删除表,请调用 DynamoDbClient’s deleteTable 方法并提供表名称。

注意

如果您的账户和区域的命名表不存在,ResourceNotFoundException则会抛出 a。

导入

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;

代码

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!"); }

请参阅上的完整示例 GitHub。

更多信息