用于 Ruby 的 DynamoDB 示例 SDK - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

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

用于 Ruby 的 DynamoDB 示例 SDK

以下代码示例向您展示了如何在 DynamoDB 中使用来执行操作和实现常见场景。 AWS SDK for Ruby

基础知识是向您展示如何在服务中执行基本操作的代码示例。

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。

场景是向您展示如何通过在一个服务中调用多个函数或与其他 AWS 服务结合来完成特定任务的代码示例。

每个示例都包含一个指向完整源代码的链接,您可以在其中找到有关如何在上下文中设置和运行代码的说明。

开始使用

以下代码示例演示如何开始使用 DynamoDB。

SDK对于 Ruby
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

require 'aws-sdk-dynamodb' require 'logger' # DynamoDBManager is a class responsible for managing DynamoDB operations # such as listing all tables in the current AWS account. class DynamoDBManager def initialize(client) @client = client @logger = Logger.new($stdout) end # Lists and prints all DynamoDB tables in the current AWS account. def list_tables @logger.info('Here are the DynamoDB tables in your account:') paginator = @client.list_tables(limit: 10) table_names = [] paginator.each_page do |page| page.table_names.each do |table_name| @logger.info("- #{table_name}") table_names << table_name end end if table_names.empty? @logger.info("You don't have any DynamoDB tables in your account.") else @logger.info("\nFound #{table_names.length} tables.") end end end if $PROGRAM_NAME == __FILE__ dynamodb_client = Aws::DynamoDB::Client.new manager = DynamoDBManager.new(dynamodb_client) manager.list_tables end
  • 有关API详细信息,请参阅 “AWS SDK for Ruby API参考 ListTables” 中的。

基础知识

以下代码示例展示了如何:

  • 创建可保存电影数据的表。

  • 在表中加入单一电影,获取并更新此电影。

  • 将样本JSON文件中的影片数据写入表中。

  • 查询在给定年份发行的电影。

  • 扫描在年份范围内发行的电影。

  • 删除表中的电影后再删除表。

SDK对于 Ruby
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

创建封装 DynamoDB 表的类。

# Creates an Amazon DynamoDB table that can be used to store movie data. # The table uses the release year of the movie as the partition key and the # title as the sort key. # # @param table_name [String] The name of the table to create. # @return [Aws::DynamoDB::Table] The newly created table. def create_table(table_name) @table = @dynamo_resource.create_table( table_name: table_name, 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 } ) @dynamo_resource.client.wait_until(:table_exists, table_name: table_name) @table rescue Aws::DynamoDB::Errors::ServiceError => e @logger.error("Failed create table #{table_name}:\n#{e.code}: #{e.message}") raise end

创建辅助函数来下载和解压缩示例JSON文件。

# Gets sample movie data, either from a local file or by first downloading it from # the Amazon DynamoDB Developer Guide. # # @param movie_file_name [String] The local file name where the movie data is stored in JSON format. # @return [Hash] The movie data as a Hash. def fetch_movie_data(movie_file_name) if !File.file?(movie_file_name) @logger.debug("Downloading #{movie_file_name}...") movie_content = URI.open( 'https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/samples/moviedata.zip' ) movie_json = '' Zip::File.open_buffer(movie_content) do |zip| zip.each do |entry| movie_json = entry.get_input_stream.read end end else movie_json = File.read(movie_file_name) end movie_data = JSON.parse(movie_json) # The sample file lists over 4000 movies. This returns only the first 250. movie_data.slice(0, 250) rescue StandardError => e puts("Failure downloading movie data:\n#{e}") raise end

运行交互式场景以创建表并对其执行操作。

table_name = "doc-example-table-movies-#{rand(10**4)}" scaffold = Scaffold.new(table_name) dynamodb_wrapper = DynamoDBBasics.new(table_name) new_step(1, 'Create a new DynamoDB table if none already exists.') unless scaffold.exists?(table_name) puts("\nNo such table: #{table_name}. Creating it...") scaffold.create_table(table_name) print "Done!\n".green end new_step(2, 'Add a new record to the DynamoDB table.') my_movie = {} my_movie[:title] = CLI::UI::Prompt.ask('Enter the title of a movie to add to the table. E.g. The Matrix') my_movie[:year] = CLI::UI::Prompt.ask('What year was it released? E.g. 1989').to_i my_movie[:rating] = CLI::UI::Prompt.ask('On a scale of 1 - 10, how do you rate it? E.g. 7').to_i my_movie[:plot] = CLI::UI::Prompt.ask('Enter a brief summary of the plot. E.g. A man awakens to a new reality.') dynamodb_wrapper.add_item(my_movie) puts("\nNew record added:") puts JSON.pretty_generate(my_movie).green print "Done!\n".green new_step(3, 'Update a record in the DynamoDB table.') my_movie[:rating] = CLI::UI::Prompt.ask("Let's update the movie you added with a new rating, e.g. 3:").to_i response = dynamodb_wrapper.update_item(my_movie) puts("Updated '#{my_movie[:title]}' with new attributes:") puts JSON.pretty_generate(response).green print "Done!\n".green new_step(4, 'Get a record from the DynamoDB table.') puts("Searching for #{my_movie[:title]} (#{my_movie[:year]})...") response = dynamodb_wrapper.get_item(my_movie[:title], my_movie[:year]) puts JSON.pretty_generate(response).green print "Done!\n".green new_step(5, 'Write a batch of items into the DynamoDB table.') download_file = 'moviedata.json' puts("Downloading movie database to #{download_file}...") movie_data = scaffold.fetch_movie_data(download_file) puts("Writing movie data from #{download_file} into your table...") scaffold.write_batch(movie_data) puts("Records added: #{movie_data.length}.") print "Done!\n".green new_step(5, 'Query for a batch of items by key.') loop do release_year = CLI::UI::Prompt.ask('Enter a year between 1972 and 2018, e.g. 1999:').to_i results = dynamodb_wrapper.query_items(release_year) if results.any? puts("There were #{results.length} movies released in #{release_year}:") results.each do |movie| print "\t #{movie['title']}".green end break else continue = CLI::UI::Prompt.ask("Found no movies released in #{release_year}! Try another year? (y/n)") break unless continue.eql?('y') end end print "\nDone!\n".green new_step(6, 'Scan for a batch of items using a filter expression.') years = {} years[:start] = CLI::UI::Prompt.ask('Enter a starting year between 1972 and 2018:') years[:end] = CLI::UI::Prompt.ask('Enter an ending year between 1972 and 2018:') releases = dynamodb_wrapper.scan_items(years) if !releases.empty? puts("Found #{releases.length} movies.") count = Question.ask( 'How many do you want to see? ', method(:is_int), in_range(1, releases.length) ) puts("Here are your #{count} movies:") releases.take(count).each do |release| puts("\t#{release['title']}") end else puts("I don't know about any movies released between #{years[:start]} "\ "and #{years[:end]}.") end print "\nDone!\n".green new_step(7, 'Delete an item from the DynamoDB table.') answer = CLI::UI::Prompt.ask("Do you want to remove '#{my_movie[:title]}'? (y/n) ") if answer.eql?('y') dynamodb_wrapper.delete_item(my_movie[:title], my_movie[:year]) puts("Removed '#{my_movie[:title]}' from the table.") print "\nDone!\n".green end new_step(8, 'Delete the DynamoDB table.') answer = CLI::UI::Prompt.ask('Delete the table? (y/n)') if answer.eql?('y') scaffold.delete_table puts("Deleted #{table_name}.") else puts("Don't forget to delete the table when you're done!") end print "\nThanks for watching!\n".green rescue Aws::Errors::ServiceError puts('Something went wrong with the demo.') rescue Errno::ENOENT true end

操作

以下代码示例演示如何使用 BatchExecuteStatement

SDK对于 Ruby
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

使用 PartiQL 读取一批项目。

class DynamoDBPartiQLBatch attr_reader :dynamo_resource, :table def initialize(table_name) client = Aws::DynamoDB::Client.new(region: 'us-east-1') @dynamodb = Aws::DynamoDB::Resource.new(client: client) @table = @dynamodb.table(table_name) end # Selects a batch of items from a table using PartiQL # # @param batch_titles [Array] Collection of movie titles # @return [Aws::DynamoDB::Types::BatchExecuteStatementOutput] def batch_execute_select(batch_titles) request_items = batch_titles.map do |title, year| { statement: "SELECT * FROM \"#{@table.name}\" WHERE title=? and year=?", parameters: [title, year] } end @dynamodb.client.batch_execute_statement({ statements: request_items }) end

使用 PartiQL 删除一批项目。

class DynamoDBPartiQLBatch attr_reader :dynamo_resource, :table def initialize(table_name) client = Aws::DynamoDB::Client.new(region: 'us-east-1') @dynamodb = Aws::DynamoDB::Resource.new(client: client) @table = @dynamodb.table(table_name) end # Deletes a batch of items from a table using PartiQL # # @param batch_titles [Array] Collection of movie titles # @return [Aws::DynamoDB::Types::BatchExecuteStatementOutput] def batch_execute_write(batch_titles) request_items = batch_titles.map do |title, year| { statement: "DELETE FROM \"#{@table.name}\" WHERE title=? and year=?", parameters: [title, year] } end @dynamodb.client.batch_execute_statement({ statements: request_items }) end

以下代码示例演示如何使用 BatchWriteItem

SDK对于 Ruby
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

class DynamoDBBasics attr_reader :dynamo_resource, :table def initialize(table_name) client = Aws::DynamoDB::Client.new(region: 'us-east-1') @dynamo_resource = Aws::DynamoDB::Resource.new(client: client) @table = @dynamo_resource.table(table_name) end # Fills an Amazon DynamoDB table with the specified data. Items are sent in # batches of 25 until all items are written. # # @param movies [Enumerable] The data to put in the table. Each item must contain at least # the keys required by the schema that was specified when the # table was created. def write_batch(movies) index = 0 slice_size = 25 while index < movies.length movie_items = [] movies[index, slice_size].each do |movie| movie_items.append({ put_request: { item: movie } }) end @dynamo_resource.client.batch_write_item({ request_items: { @table.name => movie_items } }) index += slice_size end rescue Aws::DynamoDB::Errors::ServiceError => e puts( "Couldn't load data into table #{@table.name}. Here's why:" ) puts("\t#{e.code}: #{e.message}") raise end
  • 有关API详细信息,请参阅 “AWS SDK for Ruby API参考 BatchWriteItem” 中的。

以下代码示例演示如何使用 CreateTable

SDK对于 Ruby
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

# Encapsulates an Amazon DynamoDB table of movie data. class Scaffold attr_reader :dynamo_resource, :table_name, :table def initialize(table_name) client = Aws::DynamoDB::Client.new(region: 'us-east-1') @dynamo_resource = Aws::DynamoDB::Resource.new(client: client) @table_name = table_name @table = nil @logger = Logger.new($stdout) @logger.level = Logger::DEBUG end # Creates an Amazon DynamoDB table that can be used to store movie data. # The table uses the release year of the movie as the partition key and the # title as the sort key. # # @param table_name [String] The name of the table to create. # @return [Aws::DynamoDB::Table] The newly created table. def create_table(table_name) @table = @dynamo_resource.create_table( table_name: table_name, 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 } ) @dynamo_resource.client.wait_until(:table_exists, table_name: table_name) @table rescue Aws::DynamoDB::Errors::ServiceError => e @logger.error("Failed create table #{table_name}:\n#{e.code}: #{e.message}") raise end
  • 有关API详细信息,请参阅 “AWS SDK for Ruby API参考 CreateTable” 中的。

以下代码示例演示如何使用 DeleteItem

SDK对于 Ruby
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

class DynamoDBBasics attr_reader :dynamo_resource, :table def initialize(table_name) client = Aws::DynamoDB::Client.new(region: 'us-east-1') @dynamo_resource = Aws::DynamoDB::Resource.new(client: client) @table = @dynamo_resource.table(table_name) end # Deletes a movie from the table. # # @param title [String] The title of the movie to delete. # @param year [Integer] The release year of the movie to delete. def delete_item(title, year) @table.delete_item(key: { 'year' => year, 'title' => title }) rescue Aws::DynamoDB::Errors::ServiceError => e puts("Couldn't delete movie #{title}. Here's why:") puts("\t#{e.code}: #{e.message}") raise end
  • 有关API详细信息,请参阅 “AWS SDK for Ruby API参考 DeleteItem” 中的。

以下代码示例演示如何使用 DeleteTable

SDK对于 Ruby
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

# Encapsulates an Amazon DynamoDB table of movie data. class Scaffold attr_reader :dynamo_resource, :table_name, :table def initialize(table_name) client = Aws::DynamoDB::Client.new(region: 'us-east-1') @dynamo_resource = Aws::DynamoDB::Resource.new(client: client) @table_name = table_name @table = nil @logger = Logger.new($stdout) @logger.level = Logger::DEBUG end # Deletes the table. def delete_table @table.delete @table = nil rescue Aws::DynamoDB::Errors::ServiceError => e puts("Couldn't delete table. Here's why:") puts("\t#{e.code}: #{e.message}") raise end
  • 有关API详细信息,请参阅 “AWS SDK for Ruby API参考 DeleteTable” 中的。

以下代码示例演示如何使用 DescribeTable

SDK对于 Ruby
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

# Encapsulates an Amazon DynamoDB table of movie data. class Scaffold attr_reader :dynamo_resource, :table_name, :table def initialize(table_name) client = Aws::DynamoDB::Client.new(region: 'us-east-1') @dynamo_resource = Aws::DynamoDB::Resource.new(client: client) @table_name = table_name @table = nil @logger = Logger.new($stdout) @logger.level = Logger::DEBUG end # Determines whether a table exists. As a side effect, stores the table in # a member variable. # # @param table_name [String] The name of the table to check. # @return [Boolean] True when the table exists; otherwise, False. def exists?(table_name) @dynamo_resource.client.describe_table(table_name: table_name) @logger.debug("Table #{table_name} exists") rescue Aws::DynamoDB::Errors::ResourceNotFoundException @logger.debug("Table #{table_name} doesn't exist") false rescue Aws::DynamoDB::Errors::ServiceError => e puts("Couldn't check for existence of #{table_name}:\n") puts("\t#{e.code}: #{e.message}") raise end
  • 有关API详细信息,请参阅 “AWS SDK for Ruby API参考 DescribeTable” 中的。

以下代码示例演示如何使用 ExecuteStatement

SDK对于 Ruby
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

使用 PartiQL 选择单个项目。

class DynamoDBPartiQLSingle attr_reader :dynamo_resource, :table def initialize(table_name) client = Aws::DynamoDB::Client.new(region: 'us-east-1') @dynamodb = Aws::DynamoDB::Resource.new(client: client) @table = @dynamodb.table(table_name) end # Gets a single record from a table using PartiQL. # Note: To perform more fine-grained selects, # use the Client.query instance method instead. # # @param title [String] The title of the movie to search. # @return [Aws::DynamoDB::Types::ExecuteStatementOutput] def select_item_by_title(title) request = { statement: "SELECT * FROM \"#{@table.name}\" WHERE title=?", parameters: [title] } @dynamodb.client.execute_statement(request) end

使用 PartiQL 更新单个项目。

class DynamoDBPartiQLSingle attr_reader :dynamo_resource, :table def initialize(table_name) client = Aws::DynamoDB::Client.new(region: 'us-east-1') @dynamodb = Aws::DynamoDB::Resource.new(client: client) @table = @dynamodb.table(table_name) end # Updates a single record from a table using PartiQL. # # @param title [String] The title of the movie to update. # @param year [Integer] The year the movie was released. # @param rating [Float] The new rating to assign the title. # @return [Aws::DynamoDB::Types::ExecuteStatementOutput] def update_rating_by_title(title, year, rating) request = { statement: "UPDATE \"#{@table.name}\" SET info.rating=? WHERE title=? and year=?", parameters: [{ "N": rating }, title, year] } @dynamodb.client.execute_statement(request) end

使用 PartiQL 添加单个项目。

class DynamoDBPartiQLSingle attr_reader :dynamo_resource, :table def initialize(table_name) client = Aws::DynamoDB::Client.new(region: 'us-east-1') @dynamodb = Aws::DynamoDB::Resource.new(client: client) @table = @dynamodb.table(table_name) end # Adds a single record to a table using PartiQL. # # @param title [String] The title of the movie to update. # @param year [Integer] The year the movie was released. # @param plot [String] The plot of the movie. # @param rating [Float] The new rating to assign the title. # @return [Aws::DynamoDB::Types::ExecuteStatementOutput] def insert_item(title, year, plot, rating) request = { statement: "INSERT INTO \"#{@table.name}\" VALUE {'title': ?, 'year': ?, 'info': ?}", parameters: [title, year, { 'plot': plot, 'rating': rating }] } @dynamodb.client.execute_statement(request) end

使用 PartiQL 删除单个项目。

class DynamoDBPartiQLSingle attr_reader :dynamo_resource, :table def initialize(table_name) client = Aws::DynamoDB::Client.new(region: 'us-east-1') @dynamodb = Aws::DynamoDB::Resource.new(client: client) @table = @dynamodb.table(table_name) end # Deletes a single record from a table using PartiQL. # # @param title [String] The title of the movie to update. # @param year [Integer] The year the movie was released. # @return [Aws::DynamoDB::Types::ExecuteStatementOutput] def delete_item_by_title(title, year) request = { statement: "DELETE FROM \"#{@table.name}\" WHERE title=? and year=?", parameters: [title, year] } @dynamodb.client.execute_statement(request) end
  • 有关API详细信息,请参阅 “AWS SDK for Ruby API参考 ExecuteStatement” 中的。

以下代码示例演示如何使用 GetItem

SDK对于 Ruby
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

class DynamoDBBasics attr_reader :dynamo_resource, :table def initialize(table_name) client = Aws::DynamoDB::Client.new(region: 'us-east-1') @dynamo_resource = Aws::DynamoDB::Resource.new(client: client) @table = @dynamo_resource.table(table_name) end # Gets movie data from the table for a specific movie. # # @param title [String] The title of the movie. # @param year [Integer] The release year of the movie. # @return [Hash] The data about the requested movie. def get_item(title, year) @table.get_item(key: { 'year' => year, 'title' => title }) rescue Aws::DynamoDB::Errors::ServiceError => e puts("Couldn't get movie #{title} (#{year}) from table #{@table.name}:\n") puts("\t#{e.code}: #{e.message}") raise end
  • 有关API详细信息,请参阅 “AWS SDK for Ruby API参考 GetItem” 中的。

以下代码示例演示如何使用 ListTables

SDK对于 Ruby
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

确定表是否存在。

# Encapsulates an Amazon DynamoDB table of movie data. class Scaffold attr_reader :dynamo_resource, :table_name, :table def initialize(table_name) client = Aws::DynamoDB::Client.new(region: 'us-east-1') @dynamo_resource = Aws::DynamoDB::Resource.new(client: client) @table_name = table_name @table = nil @logger = Logger.new($stdout) @logger.level = Logger::DEBUG end # Determines whether a table exists. As a side effect, stores the table in # a member variable. # # @param table_name [String] The name of the table to check. # @return [Boolean] True when the table exists; otherwise, False. def exists?(table_name) @dynamo_resource.client.describe_table(table_name: table_name) @logger.debug("Table #{table_name} exists") rescue Aws::DynamoDB::Errors::ResourceNotFoundException @logger.debug("Table #{table_name} doesn't exist") false rescue Aws::DynamoDB::Errors::ServiceError => e puts("Couldn't check for existence of #{table_name}:\n") puts("\t#{e.code}: #{e.message}") raise end
  • 有关API详细信息,请参阅 “AWS SDK for Ruby API参考 ListTables” 中的。

以下代码示例演示如何使用 PutItem

SDK对于 Ruby
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

class DynamoDBBasics attr_reader :dynamo_resource, :table def initialize(table_name) client = Aws::DynamoDB::Client.new(region: 'us-east-1') @dynamo_resource = Aws::DynamoDB::Resource.new(client: client) @table = @dynamo_resource.table(table_name) end # Adds a movie to the table. # # @param movie [Hash] The title, year, plot, and rating of the movie. def add_item(movie) @table.put_item( item: { 'year' => movie[:year], 'title' => movie[:title], 'info' => { 'plot' => movie[:plot], 'rating' => movie[:rating] } } ) rescue Aws::DynamoDB::Errors::ServiceError => e puts("Couldn't add movie #{title} to table #{@table.name}. Here's why:") puts("\t#{e.code}: #{e.message}") raise end
  • 有关API详细信息,请参阅 “AWS SDK for Ruby API参考 PutItem” 中的。

以下代码示例演示如何使用 Query

SDK对于 Ruby
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

class DynamoDBBasics attr_reader :dynamo_resource, :table def initialize(table_name) client = Aws::DynamoDB::Client.new(region: 'us-east-1') @dynamo_resource = Aws::DynamoDB::Resource.new(client: client) @table = @dynamo_resource.table(table_name) end # Queries for movies that were released in the specified year. # # @param year [Integer] The year to query. # @return [Array] The list of movies that were released in the specified year. def query_items(year) response = @table.query( key_condition_expression: '#yr = :year', expression_attribute_names: { '#yr' => 'year' }, expression_attribute_values: { ':year' => year } ) rescue Aws::DynamoDB::Errors::ServiceError => e puts("Couldn't query for movies released in #{year}. Here's why:") puts("\t#{e.code}: #{e.message}") raise else response.items end
  • 有关API详细信息,请参阅AWS SDK for Ruby API参考中的查询

以下代码示例演示如何使用 Scan

SDK对于 Ruby
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

class DynamoDBBasics attr_reader :dynamo_resource, :table def initialize(table_name) client = Aws::DynamoDB::Client.new(region: 'us-east-1') @dynamo_resource = Aws::DynamoDB::Resource.new(client: client) @table = @dynamo_resource.table(table_name) end # Scans for movies that were released in a range of years. # Uses a projection expression to return a subset of data for each movie. # # @param year_range [Hash] The range of years to retrieve. # @return [Array] The list of movies released in the specified years. def scan_items(year_range) movies = [] scan_hash = { filter_expression: '#yr between :start_yr and :end_yr', projection_expression: '#yr, title, info.rating', expression_attribute_names: { '#yr' => 'year' }, expression_attribute_values: { ':start_yr' => year_range[:start], ':end_yr' => year_range[:end] } } done = false start_key = nil until done scan_hash[:exclusive_start_key] = start_key unless start_key.nil? response = @table.scan(scan_hash) movies.concat(response.items) unless response.items.empty? start_key = response.last_evaluated_key done = start_key.nil? end rescue Aws::DynamoDB::Errors::ServiceError => e puts("Couldn't scan for movies. Here's why:") puts("\t#{e.code}: #{e.message}") raise else movies end

以下代码示例演示如何使用 UpdateItem

SDK对于 Ruby
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

class DynamoDBBasics attr_reader :dynamo_resource, :table def initialize(table_name) client = Aws::DynamoDB::Client.new(region: 'us-east-1') @dynamo_resource = Aws::DynamoDB::Resource.new(client: client) @table = @dynamo_resource.table(table_name) end # Updates rating and plot data for a movie in the table. # # @param movie [Hash] The title, year, plot, rating of the movie. def update_item(movie) response = @table.update_item( key: { 'year' => movie[:year], 'title' => movie[:title] }, update_expression: 'set info.rating=:r', expression_attribute_values: { ':r' => movie[:rating] }, return_values: 'UPDATED_NEW' ) rescue Aws::DynamoDB::Errors::ServiceError => e puts("Couldn't update movie #{movie[:title]} (#{movie[:year]}) in table #{@table.name}\n") puts("\t#{e.code}: #{e.message}") raise else response.attributes end
  • 有关API详细信息,请参阅 “AWS SDK for Ruby API参考 UpdateItem” 中的。

场景

以下代码示例展示了如何:

  • 通过运行多个SELECT语句获取一批项目。

  • 通过运行多个INSERT语句来添加一批项目。

  • 通过运行多个UPDATE语句来更新一批项目。

  • 通过运行多个DELETE语句来删除一批项目。

SDK对于 Ruby
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

运行创建表并运行批量 PartiQL 查询的场景。

table_name = "doc-example-table-movies-partiql-#{rand(10**4)}" scaffold = Scaffold.new(table_name) sdk = DynamoDBPartiQLBatch.new(table_name) new_step(1, 'Create a new DynamoDB table if none already exists.') unless scaffold.exists?(table_name) puts("\nNo such table: #{table_name}. Creating it...") scaffold.create_table(table_name) print "Done!\n".green end new_step(2, 'Populate DynamoDB table with movie data.') download_file = 'moviedata.json' puts("Downloading movie database to #{download_file}...") movie_data = scaffold.fetch_movie_data(download_file) puts("Writing movie data from #{download_file} into your table...") scaffold.write_batch(movie_data) puts("Records added: #{movie_data.length}.") print "Done!\n".green new_step(3, 'Select a batch of items from the movies table.') puts "Let's select some popular movies for side-by-side comparison." response = sdk.batch_execute_select([['Mean Girls', 2004], ['Goodfellas', 1977], ['The Prancing of the Lambs', 2005]]) puts("Items selected: #{response['responses'].length}\n") print "\nDone!\n".green new_step(4, 'Delete a batch of items from the movies table.') sdk.batch_execute_write([['Mean Girls', 2004], ['Goodfellas', 1977], ['The Prancing of the Lambs', 2005]]) print "\nDone!\n".green new_step(5, 'Delete the table.') return unless scaffold.exists?(table_name) scaffold.delete_table end

以下代码示例展示了如何:

  • 通过运行SELECT语句获取项目。

  • 通过运行INSERT语句添加项目。

  • 通过运行UPDATE语句更新项目。

  • 通过运行DELETE语句删除项目。

SDK对于 Ruby
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

运行创建表并运行 PartiQL 查询的场景。

table_name = "doc-example-table-movies-partiql-#{rand(10**8)}" scaffold = Scaffold.new(table_name) sdk = DynamoDBPartiQLSingle.new(table_name) new_step(1, 'Create a new DynamoDB table if none already exists.') unless scaffold.exists?(table_name) puts("\nNo such table: #{table_name}. Creating it...") scaffold.create_table(table_name) print "Done!\n".green end new_step(2, 'Populate DynamoDB table with movie data.') download_file = 'moviedata.json' puts("Downloading movie database to #{download_file}...") movie_data = scaffold.fetch_movie_data(download_file) puts("Writing movie data from #{download_file} into your table...") scaffold.write_batch(movie_data) puts("Records added: #{movie_data.length}.") print "Done!\n".green new_step(3, 'Select a single item from the movies table.') response = sdk.select_item_by_title('Star Wars') puts("Items selected for title 'Star Wars': #{response.items.length}\n") print response.items.first.to_s.yellow print "\n\nDone!\n".green new_step(4, 'Update a single item from the movies table.') puts "Let's correct the rating on The Big Lebowski to 10.0." sdk.update_rating_by_title('The Big Lebowski', 1998, 10.0) print "\nDone!\n".green new_step(5, 'Delete a single item from the movies table.') puts "Let's delete The Silence of the Lambs because it's just too scary." sdk.delete_item_by_title('The Silence of the Lambs', 1991) print "\nDone!\n".green new_step(6, 'Insert a new item into the movies table.') puts "Let's create a less-scary movie called The Prancing of the Lambs." sdk.insert_item('The Prancing of the Lambs', 2005, 'A movie about happy livestock.', 5.0) print "\nDone!\n".green new_step(7, 'Delete the table.') return unless scaffold.exists?(table_name) scaffold.delete_table end
  • 有关API详细信息,请参阅 “AWS SDK for Ruby API参考 ExecuteStatement” 中的。

无服务器示例

以下代码示例演示如何实现 Lambda 函数,该函数接收通过从 DynamoDB 流接收记录而触发的事件。该函数检索 DynamoDB 有效负载,并记录下记录内容。

SDK对于 Ruby
注意

还有更多相关信息 GitHub。在无服务器示例存储库中查找完整示例,并了解如何进行设置和运行。

通过 Ruby 将 DynamoDB 事件与 Lambda 结合使用。

def lambda_handler(event:, context:) return 'received empty event' if event['Records'].empty? event['Records'].each do |record| log_dynamodb_record(record) end "Records processed: #{event['Records'].length}" end def log_dynamodb_record(record) puts record['eventID'] puts record['eventName'] puts "DynamoDB Record: #{JSON.generate(record['dynamodb'])}" end

以下代码示例演示如何为接收来自 DynamoDB 流的事件的 Lambda 函数实现部分批量响应。该函数在响应中报告批处理项目失败,并指示 Lambda 稍后重试这些消息。

SDK对于 Ruby
注意

还有更多相关信息 GitHub。在无服务器示例存储库中查找完整示例,并了解如何进行设置和运行。

报告使用 Ruby 通过 Lambda 进行 DynamoDB 批处理项目失败。

def lambda_handler(event:, context:) records = event["Records"] cur_record_sequence_number = "" records.each do |record| begin # Process your record cur_record_sequence_number = record["dynamodb"]["SequenceNumber"] rescue StandardError => e # Return failed record's sequence number return {"batchItemFailures" => [{"itemIdentifier" => cur_record_sequence_number}]} end end {"batchItemFailures" => []} end