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

步骤 1:创建表

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

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

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

  1. 将以下程序复制并粘贴到名为 MoviesCreateTable.rb 的文件中。

    # # Copyright 2010-2019 Amazon.com, 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 # # http://aws.amazon.com/apache2.0/ # # 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. # require "aws-sdk" Aws.config.update({ region: "us-west-2", endpoint: "http://localhost:8000" }) dynamodb = Aws::DynamoDB::Client.new params = { table_name: "Movies", key_schema: [ { attribute_name: "year", key_type: "HASH" #Partition key }, { attribute_name: "title", key_type: "RANGE" #Sort key } ], attribute_definitions: [ { attribute_name: "year", attribute_type: "N" }, { attribute_name: "title", attribute_type: "S" }, ], provisioned_throughput: { read_capacity_units: 10, write_capacity_units: 10 } } begin result = dynamodb.create_table(params) puts "Created table. Status: " + result.table_description.table_status; rescue Aws::DynamoDB::Errors::ServiceError => error puts "Unable to create table:" puts "#{error.message}" end

    注意

    • 您需要设置终端节点,以表示您要在计算机上的可下载版 Amazon DynamoDB 中创建表。

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

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

  2. 要运行该程序,请输入以下命令。

    ruby MoviesCreateTable.rb

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