Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.
Ejemplos de DynamoDB usando SDK para Ruby
Los siguientes ejemplos de código muestran cómo realizar acciones e implementar escenarios comunes mediante DynamoDB. AWS SDK for Ruby
Los conceptos básicos son ejemplos de código que muestran cómo realizar las operaciones esenciales dentro de un servicio.
Las acciones son extractos de código de programas más grandes y deben ejecutarse en contexto. Mientras las acciones muestran cómo llamar a las distintas funciones de servicio, es posible ver las acciones en contexto en los escenarios relacionados.
Los escenarios son ejemplos de código que muestran cómo llevar a cabo una tarea específica a través de llamadas a varias funciones dentro del servicio o combinado con otros Servicios de AWS.
En cada ejemplo se incluye un enlace al código de origen completo, con instrucciones de configuración y ejecución del código en el contexto.
Introducción
En los siguientes ejemplos de código, se muestra cómo empezar a utilizar DynamoDB.
- SDK para Ruby
-
nota
Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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
-
Para obtener más información sobre la API, consulta ListTablesla Referencia AWS SDK for Ruby de la API.
-
Conceptos básicos
En el siguiente ejemplo de código, se muestra cómo:
Creación de una tabla que pueda contener datos de películas.
Colocar, obtener y actualizar una sola película en la tabla.
Escribir los datos de películas en la tabla a partir de un archivo JSON de ejemplo.
Consultar películas que se hayan estrenado en un año determinado.
Buscar películas que se hayan estrenado en un intervalo de años.
Eliminación de una película de la tabla y, a continuación, eliminar la tabla.
- SDK para Ruby
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. Crear una clase que encapsula una tabla de 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
Crear una función auxiliar para descargar y extraer el archivo JSON de muestra.
# 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
Ejecutar un escenario interactivo para crear la tabla y realizar acciones en ella.
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
-
Para obtener información sobre la API, consulte los siguientes temas en la Referencia de la API de AWS SDK for Ruby .
-
Acciones
En el siguiente ejemplo de código, se muestra cómo utilizar BatchExecuteStatement
.
- SDK para Ruby
-
nota
Hay más información GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. Leer un lote de elementos con 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
Eliminar un lote de elementos con 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
-
Para obtener más información sobre la API, consulta BatchExecuteStatementla Referencia AWS SDK for Ruby de la API.
-
En el siguiente ejemplo de código, se muestra cómo utilizar BatchWriteItem
.
- SDK para Ruby
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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
-
Para obtener más información sobre la API, consulta BatchWriteItemla Referencia AWS SDK for Ruby de la API.
-
En el siguiente ejemplo de código, se muestra cómo utilizar CreateTable
.
- SDK para Ruby
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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
-
Para obtener más información sobre la API, consulta CreateTablela Referencia AWS SDK for Ruby de la API.
-
En el siguiente ejemplo de código, se muestra cómo utilizar DeleteItem
.
- SDK para Ruby
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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
-
Para obtener más información sobre la API, consulta DeleteItemla Referencia AWS SDK for Ruby de la API.
-
En el siguiente ejemplo de código, se muestra cómo utilizar DeleteTable
.
- SDK para Ruby
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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
-
Para obtener más información sobre la API, consulta DeleteTablela Referencia AWS SDK for Ruby de la API.
-
En el siguiente ejemplo de código, se muestra cómo utilizar DescribeTable
.
- SDK para Ruby
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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
-
Para obtener más información sobre la API, consulta DescribeTablela Referencia AWS SDK for Ruby de la API.
-
En el siguiente ejemplo de código, se muestra cómo utilizar ExecuteStatement
.
- SDK para Ruby
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. Seleccionar un solo elemento con 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
Actualizar un solo elemento con 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
Añadir un solo elemento con 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
Eliminar un solo elemento con 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
-
Para obtener más información sobre la API, consulta ExecuteStatementla Referencia AWS SDK for Ruby de la API.
-
En el siguiente ejemplo de código, se muestra cómo utilizar GetItem
.
- SDK para Ruby
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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
-
Para obtener más información sobre la API, consulta GetItemla Referencia AWS SDK for Ruby de la API.
-
En el siguiente ejemplo de código, se muestra cómo utilizar ListTables
.
- SDK para Ruby
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. Determinar si existe una tabla.
# 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
-
Para obtener más información sobre la API, consulta ListTablesla Referencia AWS SDK for Ruby de la API.
-
En el siguiente ejemplo de código, se muestra cómo utilizar PutItem
.
- SDK para Ruby
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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
-
Para obtener más información sobre la API, consulta PutItemla Referencia AWS SDK for Ruby de la API.
-
En el siguiente ejemplo de código, se muestra cómo utilizar Query
.
- SDK para Ruby
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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
-
Para obtener información sobre la API, consulte Query en la referencia de la API de AWS SDK for Ruby .
-
En el siguiente ejemplo de código, se muestra cómo utilizar Scan
.
- SDK para Ruby
-
nota
Hay más información GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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
-
Para obtener información sobre la API, consulte Scan en la referencia de la API de AWS SDK for Ruby .
-
En el siguiente ejemplo de código, se muestra cómo utilizar UpdateItem
.
- SDK para Ruby
-
nota
Hay más información GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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
-
Para obtener más información sobre la API, consulta UpdateItemla Referencia AWS SDK for Ruby de la API.
-
Escenarios
En el siguiente ejemplo de código, se muestra cómo:
Obtención de un lote de elementos mediante la ejecución de varias instrucciones SELECT.
Agregar un lote de elementos mediante la ejecución de varias instrucciones INSERT.
Actualizar un lote de elementos con la ejecución de varias instrucciones UPDATE.
Eliminación de un lote de elementos con la ejecución de varias instrucciones DELETE.
- SDK para Ruby
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. Ejecutar un escenario que crea una tabla y ejecuta lotes de consultas 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
-
Para obtener más información sobre la API, consulta BatchExecuteStatementla Referencia AWS SDK for Ruby de la API.
-
En el siguiente ejemplo de código, se muestra cómo:
Obtención de un artículo mediante una instrucción SELECT.
Agregar un elemento mediante una instrucción INSERT.
Actualizar un elemento mediante una instrucción UPDATE.
Eliminación de un elemento mediante una instrucción DELETE.
- SDK para Ruby
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. Ejecutar un escenario que crea una tabla y ejecuta consultas 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
-
Para obtener más información sobre la API, consulta ExecuteStatementla Referencia AWS SDK for Ruby de la API.
-
Ejemplos de tecnología sin servidor
En el siguiente ejemplo de código se muestra cómo implementar una función de Lambda que recibe un evento que se desencadena al recibir registros de una transmisión de DynamoDB. La función recupera la carga útil de DynamoDB y registra el contenido del registro.
- SDK para Ruby
-
nota
Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el repositorio de ejemplos de tecnología sin servidor
. Consumo de un evento de DynamoDB con Lambda mediante Ruby.
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
El siguiente ejemplo de código muestra cómo implementar una respuesta por lotes parcial para las funciones de Lambda que reciben eventos de una transmisión de DynamoDB. La función informa los errores de los elementos del lote en la respuesta y le indica a Lambda que vuelva a intentar esos mensajes más adelante.
- SDK para Ruby
-
nota
Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el repositorio de ejemplos de tecnología sin servidor
. Notificación de los errores de los elementos del lote de DynamoDB con Lambda mediante Ruby.
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