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

步骤 2:加载样本数据

在此步骤中,您将使用样本数据填充 Movies 表。

此方案将使用一个样本数据文件,其中包含与 Internet Movie Database (IMDb) 中的数千部电影相关的信息。如以下示例所示,电影数据是 JSON 格式。每部电影各有一个 yeartitle 和一个名为 info 的 JSON 映射。

[ { "year" : ... , "title" : ... , "info" : { ... } }, { "year" : ..., "title" : ..., "info" : { ... } }, ... ]

在 JSON 数据中,请注意以下事项:

  • yeartitle 用作 Movies 表的主键属性值。

  • 其余的 info 值存储在名为 info 的单一属性中。此程序说明如何在 Amazon DynamoDB 属性中存储 JSON。

以下是一个电影数据的示例。

{ "year" : 2013, "title" : "Turn It Down, Or Else!", "info" : { "directors" : [ "Alice Smith", "Bob Jones" ], "release_date" : "2013-01-18T00:00:00Z", "rating" : 6.2, "genres" : [ "Comedy", "Drama" ], "image_url" : "http://ia.media-imdb.com/images/N/O9ERWAU7FS797AJ7LU8HN09AMUP908RLlo5JF90EWR7LJKQ7@@._V1_SX400_.jpg", "plot" : "A rock band plays their music at high volumes, annoying the neighbors.", "rank" : 11, "running_time_secs" : 5215, "actors" : [ "David Matthewman", "Ann Thomas", "Jonathan G. Neff" ] } }

步骤 2.1:下载样本数据文件

  1. 下载样本数据存档:moviedata.zip

  2. 从存档中提取数据文件 (moviedata.json)。

  3. moviedata.json 文件复制并粘贴到当前目录中。

步骤 2.2:将样本数据加载到 Movies 表

下载样本数据后,您可以运行以下程序来填充 Movies 表。

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

    <!-- <!-- 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. --> <html> <head> <script src="https://sdk.amazonaws.com/js/aws-sdk-2.7.16.min.js"></script> <script type="text/javascript"> AWS.config.update({ region: "us-west-2", endpoint: 'http://localhost:8000', // accessKeyId default can be used while using the downloadable version of DynamoDB. // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead. accessKeyId: "fakeMyKeyId", // secretAccessKey default can be used while using the downloadable version of DynamoDB. // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead. secretAccessKey: "fakeSecretAccessKey" }); var docClient = new AWS.DynamoDB.DocumentClient(); function processFile(evt) { document.getElementById('textarea').innerHTML = ""; document.getElementById('textarea').innerHTML += "Importing movies into DynamoDB. Please wait..." + "\n"; var file = evt.target.files[0]; if (file) { var r = new FileReader(); r.onload = function(e) { var contents = e.target.result; var allMovies = JSON.parse(contents); allMovies.forEach(function (movie) { document.getElementById('textarea').innerHTML += "Processing: " + movie.title + "\n"; var params = { TableName: "Movies", Item: { "year": movie.year, "title": movie.title, "info": movie.info } }; docClient.put(params, function (err, data) { if (err) { document.getElementById('textarea').innerHTML += "Unable to add movie: " + count + movie.title + "\n"; document.getElementById('textarea').innerHTML += "Error JSON: " + JSON.stringify(err) + "\n"; } else { document.getElementById('textarea').innerHTML += "PutItem succeeded: " + movie.title + "\n"; textarea.scrollTop = textarea.scrollHeight; } }); }); }; r.readAsText(file); } else { alert("Could not read movie data file"); } } </script> </head> <body> <input type="file" id="fileinput" accept='application/json'/> <br><br> <textarea readonly id= "textarea" style="width:400px; height:800px"></textarea> <script> document.getElementById('fileinput').addEventListener('change', processFile, false); </script> </body> </html>
  2. 在您的浏览器中打开 MoviesLoadData.html 文件。

  3. 选择 Browse (浏览) 并加载 moviedata.json 文件。