Amazon DynamoDB
入门指南 (API Version 2012-08-10)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。点 击 Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

步骤 1:创建表

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

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

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

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

    Copy
    // Copyright 2012-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. // Licensed under the Apache License, Version 2.0. package com.amazonaws.codesamples.gsg; import java.util.Arrays; import com.amazonaws.client.builder.AwsClientBuilder; import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; import com.amazonaws.services.dynamodbv2.document.DynamoDB; import com.amazonaws.services.dynamodbv2.document.Table; import com.amazonaws.services.dynamodbv2.model.AttributeDefinition; 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; 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. 编译并运行程序。

要了解有关管理表的更多信息,请参阅 Amazon DynamoDB 开发人员指南 中的处理表