将 JSON 文件中的项目加载到 Amazon DynamoDB 表中 - 适用于 Ruby 的 Amazon 开发工具包
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

将 JSON 文件中的项目加载到 Amazon DynamoDB 表中

以下示例将 JSON 文件 movie_data.json 中的项目添加到 us-west-2 区域的 Movies 表中。

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 require 'aws-sdk-dynamodb' require 'json' # Adds an item to a table in Amazon DynamoDB. # # @param dynamodb_client [Aws::DynamoDB::Client] An initialized # Amazon DynamoDB client. # @param table_item [Hash] The properties of the item, in the correct format. # @example # add_item_to_table( # Aws::DynamoDB::Client.new(region, 'us-west-2'), # { # table_name: 'Movies', # item: { # "year": 1985, # "title": "The Big Movie", # "info": { # "directors": ["Mary"], # "release_date": "1985-12-25T00:00:00Z", # "rating": 5.5, # "genres": [ # "Action", # "Drama" # ], # "image_url": "http://docs.aws.amazon.com/assets/images/aws_logo_dark.png", # "plot": "Nothing happens at all.", # "rank": 2, # "running_time_secs": 7380, # "actors": [ # "Larry", # "Moe", # "Curly" # ] # } # } # } # ) def add_item_to_table(dynamodb_client, table_item) dynamodb_client.put_item(table_item) puts "Added movie: #{table_item[:item]['title']} " \ "(#{table_item[:item]['year']})" rescue StandardError => e puts 'Error adding movie ' \ "#{table_item[:item]['title']} " \ "(#{table_item[:item]['year']})': #{e.message}" puts 'Program stopped.' exit 1 end # Full example call: def run_me region = 'us-west-2' table_name = 'Movies' data_file = 'moviedata.json' dynamodb_client = Aws::DynamoDB::Client.new(region: region) file = File.read(data_file) movies = JSON.parse(file) puts "Adding movies from file '#{data_file}' " \ "into table '#{table_name}'..." movies.each do |movie| table_item = { table_name: table_name, item: movie } add_item_to_table(dynamodb_client, table_item) end puts 'Done.' end run_me if $PROGRAM_NAME == __FILE__

下面是加载两个电影的 JSON 文件的示例。

[ { "year": 2015, "title": "The Big New Movie", "info": { "plot": "Nothing happens at all.", "rating": 1.0, "directors" : [ "Alice", "Bob" ], "release_date" : "2015-01-18T00:00:00Z", "genres" : [ "Comedy", "Drama" ], "image_url" : "https://d0.awsstatic.com/logos/powered-by-aws.png", "rank" : 11, "running_time_secs" : 5215, "actors" : [ "David", "Ann", "Jonathan" ] } }, { "year": 2017, "title": "The Big New Movie 2", "info": { "plot": "Nothing happens at all again.", "rating": 2.0, "directors" : [ "Joe", "Mary" ], "release_date" : "2017-01-17T00:00:00Z", "genres" : [ "Comedy", "Drama" ], "image_url" : "https://d0.awsstatic.com/logos/powered-by-aws-white.png", "rank" : 10, "running_time_secs" : 5221, "actors" : [ "Bob", "Sue", "Jim" ] } } ]

请参阅 GitHub 上的完整示例JSON 文件