DynamoDB教程

DynamoDB 创建表

创建表通常包括生成表、命名表、建立其主键属性和设置属性数据类型。
利用 GUI 控制台、Java 或其他选项来执行这些任务。

使用 GUI 控制台创建表

通过访问位于 https://console.aws 的控制台创建表。 amazon.com/dynamodb。然后选择"创建表"选项。
GUI 控制台
我们的示例生成一个填充有产品信息的表格,其中产品具有由 ID 号(数字属性)标识的唯一属性。在 创建表屏幕中,在表名字段中输入表名;在分区键字段中输入主键 (ID);并为数据类型输入"数字"。
创建表格
输入所有信息后,选择 创建

使用 Java 创建表

使用Java创建相同的表。它的主键由以下两个属性组成-
ID-使用分区键和 ScalarAttributeType N,表示数字。 命名法-使用排序键和 ScalarAttributeType S,表示字符串。
Java 使用 createTable 方法 生成表;并在调用中指定了表名、主键属性和属性数据类型。
您可以查看以下示例-
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.
它表示使用本地主机进行本地安装。另外,请注意所需的 ProvisionedThroughput 参数,本地安装会忽略该参数。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4