Amazon DynamoDB
开发人员指南 (API 版本 2012-08-10)
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门

步骤 1:创建表

在此步骤中,您将创建一个名为 Movies 的表。表的主键由以下属性组成:

  • year – 分区键。ScalarAttributeTypeN,表示数字。

  • title – 排序键。ScalarAttributeTypeS,表示字符串。

  1. 将以下程序复制并粘贴到您的 Java 开发环境中。

    /** * 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. */ package com.amazonaws.codesamples.gsg; import java.util.Arrays; import com.amazonaws.client.builder.AwsClientBuilder; import; import; import; import; import; import; import; import; import; public class MoviesCreateTable { public static void main(String[] args) throws Exception { AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard() .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2")) .build(); DynamoDB dynamoDB = new DynamoDB(client); String tableName = "Movies"; try { System.out.println("Attempting to create table; please wait..."); Table table = dynamoDB.createTable(tableName, Arrays.asList(new KeySchemaElement("year", KeyType.HASH), // Partition // key new KeySchemaElement("title", KeyType.RANGE)), // Sort key Arrays.asList(new AttributeDefinition("year", ScalarAttributeType.N), new AttributeDefinition("title", ScalarAttributeType.S)), new ProvisionedThroughput(10L, 10L)); table.waitForActive(); System.out.println("Success. Table status: " + table.getDescription().getTableStatus()); } catch (Exception e) { System.err.println("Unable to create table: "); System.err.println(e.getMessage()); } } }


    • 设置终端节点以指示您正在计算机上的 DynamoDB 中创建表。

    • createTable 调用中,您需要指定表名称、主键属性及其数据类型。

    • ProvisionedThroughput 参数是必填项;但 DynamoDB 的可下载版本将忽略此参数。(预置的吞吐量不在此练习的范围内。)

  2. 编译并运行程序。

要了解有关管理表的更多信息,请参阅在 DynamoDB 中使用表