Dynamodb 简明教程

DynamoDB - Create Table

创建表通常包括生成表、为其命名、建立其主键属性以及设置属性数据类型。

Creating a table generally consists of spawning the table, naming it, establishing its primary key attributes, and setting attribute data types.

利用 GUI 控制台、Java 或其他选项执行这些任务。

Utilize the GUI Console, Java, or another option to perform these tasks.

Create Table using the GUI Console

通过访问 https://console.aws.amazon.com/dynamodb 中的控制台创建表。然后选择“创建表”选项。

Create a table by accessing the console at https://console.aws.amazon.com/dynamodb. Then choose the “Create Table” option.

gui console

我们的示例生成一个包含产品信息的表,其中具有唯一属性的产品由 ID 号(数字属性)标识。在 Create Table 屏幕中,在表名字段输入表名;在分区键字段中输入主键 (ID);输入数据类型的“数字”。

Our example generates a table populated with product information, with products of unique attributes identified by an ID number (numeric attribute). In the Create Table screen, enter the table name within the table name field; enter the primary key (ID) within the partition key field; and enter “Number” for the data type.

create table

输入所有信息后,选择 Create

After entering all information, select Create.

Create Table using Java

使用 Java 创建相同的表。其主键包含以下两个属性 -

Use Java to create the same table. Its primary key consists of the following two attributes −

  1. ID − Use a partition key, and the ScalarAttributeType N, meaning number.

  2. Nomenclature − Use a sort key, and the ScalarAttributeType S, meaning string.

Java 使用 createTable method 生成一个表;并在调用中,指定表名、主键属性和属性数据类型。

Java uses the createTable method to generate a table; and within the call, table name, primary key attributes, and attribute data types are specified.

您可以查看以下示例 -

You can review the following example −

import java.util.Arrays;

import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
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 ProductsCreateTable {
   public static void main(String[] args) throws Exception {
      AmazonDynamoDBClient client = new AmazonDynamoDBClient()
         .withEndpoint("http://localhost:8000");

      DynamoDB dynamoDB = new DynamoDB(client);
      String tableName = "Products";
      try {
         System.out.println("Creating the table, wait...");
         Table table = dynamoDB.createTable (tableName,
            Arrays.asList (
               new KeySchemaElement("ID", KeyType.HASH), // the partition key
                                                         // the sort key
               new KeySchemaElement("Nomenclature", KeyType.RANGE)
            ),
            Arrays.asList (
               new AttributeDefinition("ID", ScalarAttributeType.N),
               new AttributeDefinition("Nomenclature", ScalarAttributeType.S)
            ),
            new ProvisionedThroughput(10L, 10L)
         );
         table.waitForActive();
         System.out.println("Table created successfully.  Status: " +
            table.getDescription().getTableStatus());

      } catch (Exception e) {
         System.err.println("Cannot create the table: ");
         System.err.println(e.getMessage());
      }
   }
}

在上述示例中,请注意端点: .withEndpoint

In the above example, note the endpoint: .withEndpoint.

它指示使用 localhost 使用本地安装。另外,请注意所需的 ProvisionedThroughput parameter ,本地安装将其忽略。

It indicates the use of a local install by using the localhost. Also, note the required ProvisionedThroughput parameter, which the local install ignores.