本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
步骤 1:在 DynamoDB 中使用 AWS SDK for JavaScript 创建表
在此步骤中,您将创建一个名为 Movies
的表。 表的主键由以下属性组成:
-
year
– 分区键。对于数字,AttributeType
为N
。 -
title
– 排序键。对于字符串,AttributeType
为S
。
-
将以下程序复制并粘贴到名为
MoviesCreateTable.js
的文件中。/** * 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. */ var AWS = require("aws-sdk"); AWS.config.update({ region: "us-west-2", endpoint: "http://localhost:8000" }); var dynamodb = new AWS.DynamoDB(); var params = { TableName : "Movies", KeySchema: [ { AttributeName: "year", KeyType: "HASH"}, //Partition key { AttributeName: "title", KeyType: "RANGE" } //Sort key ], AttributeDefinitions: [ { AttributeName: "year", AttributeType: "N" }, { AttributeName: "title", AttributeType: "S" } ], ProvisionedThroughput: { ReadCapacityUnits: 10, WriteCapacityUnits: 10 } }; dynamodb.createTable(params, function(err, data) { if (err) { console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2)); } else { console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2)); } });
注意 -
设置终端节点以指示您正在计算机上的 Amazon DynamoDB 中创建表。
-
在
createTable
调用中,您需要指定表名称、主键属性及其数据类型。 -
ProvisionedThroughput
参数是必填项;但 DynamoDB 的可下载版本将忽略此参数。(预置的吞吐量不在此教程的讨论范围内。)
-
-
要运行该程序,请输入以下命令。
node MoviesCreateTable.js
要了解有关管理表的更多信息,请参阅使用 DynamoDB 中的表和数据。