获取有关所有 Amazon DynamoDB 表的信息 - 适用于 Ruby 的 Amazon 开发工具包
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

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

获取有关所有 Amazon DynamoDB 表的信息

以下示例列出了 us-west-2 区域的每个表中的项目的名称和数量。

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 require 'aws-sdk-dynamodb' # Gets a list of available table names in Amazon DynamoDB. # # @param dynamodb_client [Aws::DynamoDB::Client] An initialized # Amazon DynamoDB client. # @return [Array] The list of available table names as an array of type String. # @example # table_names = get_table_names( # Aws::DynamoDB::Client.new(region, 'us-west-2') # ) # table_names.each do |table_name| # puts table_name # end def get_table_names(dynamodb_client) result = dynamodb_client.list_tables result.table_names rescue StandardError => e puts "Error getting table names: #{e.message}" 'Error' end # Gets a count of items in a table in Amazon DynamoDB. # # @param dynamodb_client [Aws::DynamoDB::Client] An initialized # Amazon DynamoDB client. # @param table_name [String] The name of the table. # @return [Integer] The number of items in the table. # @example # puts get_count_of_table_items( # Aws::DynamoDB::Client.new(region, 'us-west-2'), # 'Movies' # ) def get_count_of_table_items(dynamodb_client, table_name) result = dynamodb_client.scan(table_name: table_name) result.items.count rescue StandardError => e puts "Error getting items for table '#{table_name}': #{e.message}" 'Error' end # Full example call: def run_me region = 'us-west-2' dynamodb_client = Aws::DynamoDB::Client.new(region: region) table_names = get_table_names(dynamodb_client) if table_names == 'Error' puts 'Cannot get table names. Stopping program.' elsif table_names.count.zero? puts "Cannot find any tables in AWS Region '#{region}'." else puts "Found #{table_names.count} tables in AWS Region '#{region}':" puts "(Displaying information for only the first 100 tables)" if table_names.count > 100 table_names.each do |table_name| table_items_count = get_count_of_table_items(dynamodb_client, table_name) if table_items_count == 'Error' puts "Cannot get count of items for table '#{table_name}'." elsif table_items_count.zero? puts "Table '#{table_name}' has no items." else puts "Table '#{table_name}' has #{table_items_count} items." end end end end run_me if $PROGRAM_NAME == __FILE__