Amazon DynamoDB
开发人员指南 (API Version 2012-08-10)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。点 击 Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

第 2 步:加载示例数据

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

此方案将使用一个示例数据文件,其中包含与 Internet Movie Database (IMDb) 中的数千部电影相关的信息。

电影数据以 JSON 格式编码。对于每部电影,JSON 将定义一个 year 名称-值对、一个 title 名称-值对和一个复杂的 info 对象,如以下示例中所示:

Copy
{ "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 文件复制并粘贴到您的 DynamoDB_intro Visual Studio 项目的 bin/Debug 文件夹中。

步骤 2.2:将示例数据加载到 Movies 表

构建一个程序以将电影数据加载到您在步骤 1 中创建的表。

  1. 此程序使用 MIT 许可证 (MIT) 所许可的开源 Newtonsoft Json.NET 库对 JSON 数据反序列化 (请参阅 https://github.com/JamesNK/Newtonsoft.Json/blob/master/LICENSE.md)。

    要将 Json.NET 库加载到您的项目中,请在 Visual Studio 中,从 Tools 菜单打开 NuGet Package Manager Console。然后在 PM> 提示符处,键入以下命令:

    Copy
    PM> Install-Package Newtonsoft.Json
  2. 将以下程序复制并粘贴到 Program.cs 文件中,替换文件的当前内容:

    Copy
    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using Amazon; using Amazon.DynamoDBv2; using Amazon.DynamoDBv2.Model; using Amazon.DynamoDBv2.DocumentModel; using Newtonsoft; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace DynamoDB_intro { class Program { public static Table GetTableObject(string tableName) { // First, set up a DynamoDB client for DynamoDB Local AmazonDynamoDBConfig ddbConfig = new AmazonDynamoDBConfig(); ddbConfig.ServiceURL = "http://localhost:8000"; AmazonDynamoDBClient client; try { client = new AmazonDynamoDBClient(ddbConfig); } catch (Exception ex) { Console.WriteLine("\n Error: failed to create a DynamoDB client; " + ex.Message); return (null); } // Now, create a Table object for the specified table Table table; try { table = Table.LoadTable(client, tableName); } catch (Exception ex) { Console.WriteLine("\n Error: failed to load the 'Movies' table; " + ex.Message); return (null); } return (table); } public static void Main(string[] args) { // First, read in the JSON data from the moviedate.json file StreamReader sr = null; JsonTextReader jtr = null; JArray movieArray = null; try { sr = new StreamReader("moviedata.json"); jtr = new JsonTextReader(sr); movieArray = (JArray)JToken.ReadFrom(jtr); } catch (Exception ex) { Console.WriteLine("\n Error: could not read from the 'moviedata.json' file, because: " + ex.Message); PauseForDebugWindow(); return; } finally { if (jtr != null) jtr.Close(); if (sr != null) sr.Close(); } // Get a Table object for the table that you created in Step 1 Table table = GetTableObject("Movies"); if (table == null) { PauseForDebugWindow(); return; } // Load the movie data into the table (this could take some time) Console.Write("\n Now writing {0:#,##0} movie records from moviedata.json (might take 15 minutes)...\n ...completed: ", movieArray.Count); for (int i = 0, j = 99; i < movieArray.Count; i++) { try { string itemJson = movieArray[i].ToString(); Document doc = Document.FromJson(itemJson); table.PutItem(doc); } catch (Exception ex) { Console.WriteLine("\nError: Could not write the movie record #{0:#,##0}, because {1}", i, ex.Message); PauseForDebugWindow(); return; } if (i >= j) { j++; Console.Write("{0,5:#,##0}, ", j); if (j % 1000 == 0) Console.Write("\n "); j += 99; } } Console.WriteLine("\n Finished writing all movie records to DynamoDB!"); PauseForDebugWindow(); } public static void PauseForDebugWindow() { // Keep the console open if in Debug mode... Console.Write("\n\n ...Press any key to continue"); Console.ReadKey(); Console.WriteLine(); } } }
  3. 现在,编译项目,保持其处于调试模式并运行它。