- Amazon DynamoDB
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门 文件包含一些实用程序方法。

getDynamoDBClientgetDaxClient 方法提供 Amazon DynamoDB 和 DynamoDB Accelerator (DAX) 客户端。为了实现控制层面操作(CreateTableDeleteTable)和写入操作,程序使用 DynamoDB 客户端。如果指定 DAX 集群端点,则主程序将创建一个 DAX 客户端,用于执行读取操作(GetItemQueryScan)。

其他 TryDaxHelper 方法(createTablewriteDatadeleteTable)用于设置和停用 DynamoDB 表及其数据。


  • 对表使用不同的预置吞吐量设置。

  • 修改写入的每个项目的大小(参见 writeData 方法的 stringSize 变量)。

  • 修改 GetItemQueryScan 测试的数量及其参数。

  • 注释掉包含 helper.CreateTablehelper.DeleteTable 的行(如果不希望每次运行程序时都创建和删除表)。


要运行此程序,您可以设置 Maven 将 DAX SDK for Java 客户端和 Amazon SDK for Java 作为依赖项。有关更多信息,请参阅 使用客户端作为 Apache Maven 依赖项

或者可以下载 DAX Java 客户端和 Amazon SDK for Java 并加入类路径。有关设置 Java 和 DAX 变量的示例,请参阅 CLASSPATH

/** * Copyright 2010-2019, Inc. or its affiliates. All Rights Reserved. * * This file is licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. A copy of * the License is located at * * * * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ import java.util.Arrays; import; import; import; import; import; import; import; import; import; import; import; import com.amazonaws.util.EC2MetadataUtils; public class TryDaxHelper { private static final String region = EC2MetadataUtils.getEC2InstanceRegion(); DynamoDB getDynamoDBClient() { System.out.println("Creating a DynamoDB client"); AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard() .withRegion(region) .build(); return new DynamoDB(client); } DynamoDB getDaxClient(String daxEndpoint) { System.out.println("Creating a DAX client with cluster endpoint " + daxEndpoint); AmazonDaxClientBuilder daxClientBuilder = AmazonDaxClientBuilder.standard(); daxClientBuilder.withRegion(region).withEndpointConfiguration(daxEndpoint); AmazonDynamoDB client =; return new DynamoDB(client); } void createTable(String tableName, DynamoDB client) { Table table = client.getTable(tableName); try { System.out.println("Attempting to create table; please wait..."); table = client.createTable(tableName, Arrays.asList( new KeySchemaElement("pk", KeyType.HASH), // Partition key new KeySchemaElement("sk", KeyType.RANGE)), // Sort key Arrays.asList( new AttributeDefinition("pk", ScalarAttributeType.N), new AttributeDefinition("sk", ScalarAttributeType.N)), new ProvisionedThroughput(10L, 10L)); table.waitForActive(); System.out.println("Successfully created table. Table status: " + table.getDescription().getTableStatus()); } catch (Exception e) { System.err.println("Unable to create table: "); e.printStackTrace(); } } void writeData(String tableName, DynamoDB client, int pkmax, int skmax) { Table table = client.getTable(tableName); System.out.println("Writing data to the table..."); int stringSize = 1000; StringBuilder sb = new StringBuilder(stringSize); for (int i = 0; i < stringSize; i++) { sb.append('X'); } String someData = sb.toString(); try { for (Integer ipk = 1; ipk <= pkmax; ipk++) { System.out.println(("Writing " + skmax + " items for partition key: " + ipk)); for (Integer isk = 1; isk <= skmax; isk++) { table.putItem(new Item() .withPrimaryKey("pk", ipk, "sk", isk) .withString("someData", someData)); } } } catch (Exception e) { System.err.println("Unable to write item:"); e.printStackTrace(); } } void deleteTable(String tableName, DynamoDB client) { Table table = client.getTable(tableName); try { System.out.println("\nAttempting to delete table; please wait..."); table.delete(); table.waitForDelete(); System.out.println("Successfully deleted table."); } catch (Exception e) { System.err.println("Unable to delete table: "); e.printStackTrace(); } } }