DynamoDB テーブルをスキャンする - Amazon DynamoDB

DynamoDB テーブルをスキャンする

AWS Management Console、AWS CLI、または AWS SDK を使用して DynamoDB テーブルのスキャンを実行できます。スキャンの詳細については、「DynamoDB でのスキャンの使用」を参照してください。

AWS SDK を使用して、DynamoDB テーブルをスキャンする

次のコード例は、AWS SDK を使用して DynamoDB テーブルをスキャンする方法を示しています。

.NET
AWS SDK for .NET
注記

GitHub には、その他のリソースもあります。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

public static async Task<int> ScanTableAsync( AmazonDynamoDBClient client, string tableName, int startYear, int endYear) { var request = new ScanRequest { TableName = tableName, ExpressionAttributeNames = new Dictionary<string, string> { { "#yr", "year" }, }, ExpressionAttributeValues = new Dictionary<string, AttributeValue> { { ":y_a", new AttributeValue { N = startYear.ToString() } }, { ":y_z", new AttributeValue { N = endYear.ToString() } }, }, FilterExpression = "#yr between :y_a and :y_z", ProjectionExpression = "#yr, title, info.actors[0], info.directors, info.running_time_secs", Limit = 10 // Set a limit to demonstrate using the LastEvaluatedKey. }; // Keep track of how many movies were found. int foundCount = 0; var response = new ScanResponse(); do { response = await client.ScanAsync(request); foundCount += response.Items.Count; response.Items.ForEach(i => DisplayItem(i)); request.ExclusiveStartKey = response.LastEvaluatedKey; } while (response.LastEvaluatedKey.Count > 0); return foundCount; }
  • API の詳細については、「AWS SDK for .NET API リファレンス」の「Scan」を参照してください。

Bash
Bash スクリプトを使用した AWS CLI
注記

GitHub には、その他のリソースもあります。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

############################################################################# # function dynamodb_scan # # This function scans a DynamoDB table. # # Parameters: # -n table_name -- The name of the table. # -f filter_expression -- The filter expression. # -a expression_attribute_names -- Path to JSON file containing the expression attribute names. # -v expression_attribute_values -- Path to JSON file containing the expression attribute values. # [-p projection_expression] -- Optional projection expression. # # Returns: # The items as json output. # And: # 0 - If successful. # 1 - If it fails. ########################################################################### function dynamodb_scan() { local table_name filter_expression expression_attribute_names expression_attribute_values projection_expression response local option OPTARG # Required to use getopts command in a function. # ###################################### # Function usage explanation ####################################### function usage() { echo "function dynamodb_scan" echo "Scan a DynamoDB table." echo " -n table_name -- The name of the table." echo " -f filter_expression -- The filter expression." echo " -a expression_attribute_names -- Path to JSON file containing the expression attribute names." echo " -v expression_attribute_values -- Path to JSON file containing the expression attribute values." echo " [-p projection_expression] -- Optional projection expression." echo "" } while getopts "n:f:a:v:p:h" option; do case "${option}" in n) table_name="${OPTARG}" ;; f) filter_expression="${OPTARG}" ;; a) expression_attribute_names="${OPTARG}" ;; v) expression_attribute_values="${OPTARG}" ;; p) projection_expression="${OPTARG}" ;; h) usage return 0 ;; \?) echo "Invalid parameter" usage return 1 ;; esac done export OPTIND=1 if [[ -z "$table_name" ]]; then errecho "ERROR: You must provide a table name with the -n parameter." usage return 1 fi if [[ -z "$filter_expression" ]]; then errecho "ERROR: You must provide a filter expression with the -f parameter." usage return 1 fi if [[ -z "$expression_attribute_names" ]]; then errecho "ERROR: You must provide expression attribute names with the -a parameter." usage return 1 fi if [[ -z "$expression_attribute_values" ]]; then errecho "ERROR: You must provide expression attribute values with the -v parameter." usage return 1 fi if [[ -z "$projection_expression" ]]; then response=$(aws dynamodb scan \ --table-name "$table_name" \ --filter-expression "$filter_expression" \ --expression-attribute-names file://"$expression_attribute_names" \ --expression-attribute-values file://"$expression_attribute_values") else response=$(aws dynamodb scan \ --table-name "$table_name" \ --filter-expression "$filter_expression" \ --expression-attribute-names file://"$expression_attribute_names" \ --expression-attribute-values file://"$expression_attribute_values" \ --projection-expression "$projection_expression") fi local error_code=${?} if [[ $error_code -ne 0 ]]; then aws_cli_error_log $error_code errecho "ERROR: AWS reports scan operation failed.$response" return 1 fi echo "$response" return 0 }

この例で使用されているユーティリティ関数。

############################################################################### # function errecho # # This function outputs everything sent to it to STDERR (standard error output). ############################################################################### function errecho() { printf "%s\n" "$*" 1>&2 } ############################################################################## # function aws_cli_error_log() # # This function is used to log the error messages from the AWS CLI. # # See https://docs.aws.amazon.com/cli/latest/topic/return-codes.html#cli-aws-help-return-codes. # # The function expects the following argument: # $1 - The error code returned by the AWS CLI. # # Returns: # 0: - Success. # ############################################################################## function aws_cli_error_log() { local err_code=$1 errecho "Error code : $err_code" if [ "$err_code" == 1 ]; then errecho " One or more S3 transfers failed." elif [ "$err_code" == 2 ]; then errecho " Command line failed to parse." elif [ "$err_code" == 130 ]; then errecho " Process received SIGINT." elif [ "$err_code" == 252 ]; then errecho " Command syntax invalid." elif [ "$err_code" == 253 ]; then errecho " The system environment or configuration was invalid." elif [ "$err_code" == 254 ]; then errecho " The service returned an error." elif [ "$err_code" == 255 ]; then errecho " 255 is a catch-all error." fi return 0 }
  • API の詳細については、「AWS CLI コマンドリファレンス」の「Scan」を参照してください。

C++
SDK for C++
注記

GitHub には、その他のリソースもあります。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

//! Scan an Amazon DynamoDB table. /*! \sa scanTable() \param tableName: Name for the DynamoDB table. \param projectionExpression: An optional projection expression, ignored if empty. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::DynamoDB::scanTable(const Aws::String &tableName, const Aws::String &projectionExpression, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration); Aws::DynamoDB::Model::ScanRequest request; request.SetTableName(tableName); if (!projectionExpression.empty()) request.SetProjectionExpression(projectionExpression); Aws::Vector<Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue>> all_items; Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> last_evaluated_key; // Used for pagination; do { if (!last_evaluated_key.empty()) { request.SetExclusiveStartKey(last_evaluated_key); } const Aws::DynamoDB::Model::ScanOutcome &outcome = dynamoClient.Scan(request); if (outcome.IsSuccess()) { // Reference the retrieved items. const Aws::Vector<Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue>> &items = outcome.GetResult().GetItems(); all_items.insert(all_items.end(), items.begin(), items.end()); last_evaluated_key = outcome.GetResult().GetLastEvaluatedKey(); } else { std::cerr << "Failed to Scan items: " << outcome.GetError().GetMessage() << std::endl; return false; } } while (!last_evaluated_key.empty()); if (!all_items.empty()) { std::cout << "Number of items retrieved from scan: " << all_items.size() << std::endl; // Iterate each item and print. for (const Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> &itemMap: all_items) { std::cout << "******************************************************" << std::endl; // Output each retrieved field and its value. for (const auto &itemEntry: itemMap) std::cout << itemEntry.first << ": " << itemEntry.second.GetS() << std::endl; } } else { std::cout << "No items found in table: " << tableName << std::endl; } return true; }
  • API の詳細については、「AWS SDK for C++ API リファレンス」の「Scan」を参照してください。

CLI
AWS CLI

テーブルをスキャンするには

次の scan の例は、MusicCollection テーブル全体をスキャンし、その結果をアーティスト「No One You Know」の曲に絞り込みます。各項目について、アルバムタイトルと曲タイトルのみが返されます。

aws dynamodb scan \ --table-name MusicCollection \ --filter-expression "Artist = :a" \ --projection-expression "#ST, #AT" \ --expression-attribute-names file://expression-attribute-names.json \ --expression-attribute-values file://expression-attribute-values.json

expression-attribute-names.json の内容:

{ "#ST": "SongTitle", "#AT":"AlbumTitle" }

expression-attribute-values.json の内容:

{ ":a": {"S": "No One You Know"} }

出力:

{ "Count": 2, "Items": [ { "SongTitle": { "S": "Call Me Today" }, "AlbumTitle": { "S": "Somewhat Famous" } }, { "SongTitle": { "S": "Scared of My Shadow" }, "AlbumTitle": { "S": "Blue Sky Blues" } } ], "ScannedCount": 3, "ConsumedCapacity": null }

詳細については、「Amazon DynamoDB ディベロッパーガイド」の「DynamoDB でのスキャンの使用」を参照してください。

  • API の詳細については、「AWS CLI コマンドリファレンス」の「Scan」を参照してください。

Go
SDK for Go V2
注記

GitHub には、その他のリソースもあります。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

// TableBasics encapsulates the Amazon DynamoDB service actions used in the examples. // It contains a DynamoDB service client that is used to act on the specified table. type TableBasics struct { DynamoDbClient *dynamodb.Client TableName string } // Scan gets all movies in the DynamoDB table that were released in a range of years // and projects them to return a reduced set of fields. // The function uses the `expression` package to build the filter and projection // expressions. func (basics TableBasics) Scan(startYear int, endYear int) ([]Movie, error) { var movies []Movie var err error var response *dynamodb.ScanOutput filtEx := expression.Name("year").Between(expression.Value(startYear), expression.Value(endYear)) projEx := expression.NamesList( expression.Name("year"), expression.Name("title"), expression.Name("info.rating")) expr, err := expression.NewBuilder().WithFilter(filtEx).WithProjection(projEx).Build() if err != nil { log.Printf("Couldn't build expressions for scan. Here's why: %v\n", err) } else { scanPaginator := dynamodb.NewScanPaginator(basics.DynamoDbClient, &dynamodb.ScanInput{ TableName: aws.String(basics.TableName), ExpressionAttributeNames: expr.Names(), ExpressionAttributeValues: expr.Values(), FilterExpression: expr.Filter(), ProjectionExpression: expr.Projection(), }) for scanPaginator.HasMorePages() { response, err = scanPaginator.NextPage(context.TODO()) if err != nil { log.Printf("Couldn't scan for movies released between %v and %v. Here's why: %v\n", startYear, endYear, err) break } else { var moviePage []Movie err = attributevalue.UnmarshalListOfMaps(response.Items, &moviePage) if err != nil { log.Printf("Couldn't unmarshal query response. Here's why: %v\n", err) break } else { movies = append(movies, moviePage...) } } } } return movies, err } // Movie encapsulates data about a movie. Title and Year are the composite primary key // of the movie in Amazon DynamoDB. Title is the sort key, Year is the partition key, // and Info is additional data. type Movie struct { Title string `dynamodbav:"title"` Year int `dynamodbav:"year"` Info map[string]interface{} `dynamodbav:"info"` } // GetKey returns the composite primary key of the movie in a format that can be // sent to DynamoDB. func (movie Movie) GetKey() map[string]types.AttributeValue { title, err := attributevalue.Marshal(movie.Title) if err != nil { panic(err) } year, err := attributevalue.Marshal(movie.Year) if err != nil { panic(err) } return map[string]types.AttributeValue{"title": title, "year": year} } // String returns the title, year, rating, and plot of a movie, formatted for the example. func (movie Movie) String() string { return fmt.Sprintf("%v\n\tReleased: %v\n\tRating: %v\n\tPlot: %v\n", movie.Title, movie.Year, movie.Info["rating"], movie.Info["plot"]) }
  • API の詳細については、「AWS SDK for Go API リファレンス」の「Scan」を参照してください。

Java
SDK for Java 2.x
注記

GitHub には、その他のリソースもあります。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

DynamoDbClient を使用して Amazon DynamoDB テーブルをスキャンします。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.AttributeValue; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import software.amazon.awssdk.services.dynamodb.model.ScanRequest; import software.amazon.awssdk.services.dynamodb.model.ScanResponse; import java.util.Map; import java.util.Set; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html * * To scan items from an Amazon DynamoDB table using the AWS SDK for Java V2, * its better practice to use the * Enhanced Client, See the EnhancedScanRecords example. */ public class DynamoDBScanItems { public static void main(String[] args) { final String usage = """ Usage: <tableName> Where: tableName - The Amazon DynamoDB table to get information from (for example, Music3). """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String tableName = args[0]; Region region = Region.US_EAST_1; DynamoDbClient ddb = DynamoDbClient.builder() .region(region) .build(); scanItems(ddb, tableName); ddb.close(); } public static void scanItems(DynamoDbClient ddb, String tableName) { try { ScanRequest scanRequest = ScanRequest.builder() .tableName(tableName) .build(); ScanResponse response = ddb.scan(scanRequest); for (Map<String, AttributeValue> item : response.items()) { Set<String> keys = item.keySet(); for (String key : keys) { System.out.println("The key name is " + key + "\n"); System.out.println("The value is " + item.get(key).s()); } } } catch (DynamoDbException e) { e.printStackTrace(); System.exit(1); } } }
  • API の詳細については、「AWS SDK for Java 2.x API リファレンス」の「Scan」を参照してください。

JavaScript
SDK for JavaScript (v3)
注記

GitHub には、その他のリソースもあります。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

この例では、ドキュメントクライアントを使用して DynamoDB での項目の操作を簡略化しています。API の詳細については、「ScanCommand」を参照してください。

import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; import { DynamoDBDocumentClient, ScanCommand } from "@aws-sdk/lib-dynamodb"; const client = new DynamoDBClient({}); const docClient = DynamoDBDocumentClient.from(client); export const main = async () => { const command = new ScanCommand({ ProjectionExpression: "#Name, Color, AvgLifeSpan", ExpressionAttributeNames: { "#Name": "Name" }, TableName: "Birds", }); const response = await docClient.send(command); for (const bird of response.Items) { console.log(`${bird.Name} - (${bird.Color}, ${bird.AvgLifeSpan})`); } return response; };
  • API の詳細については、「AWS SDK for JavaScript API リファレンス」の「Scan」を参照してください。

SDK for JavaScript (v2)
注記

GitHub には、その他のリソースもあります。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

// Load the AWS SDK for Node.js. var AWS = require("aws-sdk"); // Set the AWS Region. AWS.config.update({ region: "REGION" }); // Create DynamoDB service object. var ddb = new AWS.DynamoDB({ apiVersion: "2012-08-10" }); const params = { // Specify which items in the results are returned. FilterExpression: "Subtitle = :topic AND Season = :s AND Episode = :e", // Define the expression attribute value, which are substitutes for the values you want to compare. ExpressionAttributeValues: { ":topic": { S: "SubTitle2" }, ":s": { N: 1 }, ":e": { N: 2 }, }, // Set the projection expression, which are the attributes that you want. ProjectionExpression: "Season, Episode, Title, Subtitle", TableName: "EPISODES_TABLE", }; ddb.scan(params, function (err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); data.Items.forEach(function (element, index, array) { console.log( "printing", element.Title.S + " (" + element.Subtitle.S + ")" ); }); } });
Kotlin
SDK for Kotlin
注記

GitHub には、その他のリソースもあります。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

suspend fun scanItems(tableNameVal: String) { val request = ScanRequest { tableName = tableNameVal } DynamoDbClient { region = "us-east-1" }.use { ddb -> val response = ddb.scan(request) response.items?.forEach { item -> item.keys.forEach { key -> println("The key name is $key\n") println("The value is ${item[key]}") } } } }
  • API の詳細については、「AWS SDK for Kotlin API リファレンス」の「Scan」を参照してください。

PHP
SDK for PHP
注記

GitHub には、その他のリソースもあります。用例一覧を検索し、AWS コードサンプルリポジトリでの設定と実行の方法を確認してください。

$yearsKey = [ 'Key' => [ 'year' => [ 'N' => [ 'minRange' => 1990, 'maxRange' => 1999, ], ], ], ]; $filter = "year between 1990 and 1999"; echo "\nHere's a list of all the movies released in the 90s:\n"; $result = $service->scan($tableName, $yearsKey, $filter); foreach ($result['Items'] as $movie) { $movie = $marshal->unmarshalItem($movie); echo $movie['title'] . "\n"; } public function scan(string $tableName, array $key, string $filters) { $query = [ 'ExpressionAttributeNames' => ['#year' => 'year'], 'ExpressionAttributeValues' => [ ":min" => ['N' => '1990'], ":max" => ['N' => '1999'], ], 'FilterExpression' => "#year between :min and :max", 'TableName' => $tableName, ]; return $this->dynamoDbClient->scan($query); }
  • API の詳細については、「AWS SDK for PHP API リファレンス」の「Scan」を参照してください。

PowerShell
Tools for PowerShell

例 1: Music テーブルのすべての項目を返します。

Invoke-DDBScan -TableName 'Music' | ConvertFrom-DDBItem

出力:

Name Value ---- ----- Genre Country Artist No One You Know Price 1.94 CriticRating 9 SongTitle Somewhere Down The Road AlbumTitle Somewhat Famous Genre Country Artist No One You Know Price 1.98 CriticRating 8.4 SongTitle My Dog Spot AlbumTitle Hey Now

例 2: Music テーブル内の CriticRating が 9 以上の項目を返します。

$scanFilter = @{ CriticRating = [Amazon.DynamoDBv2.Model.Condition]@{ AttributeValueList = @(@{N = '9'}) ComparisonOperator = 'GE' } } Invoke-DDBScan -TableName 'Music' -ScanFilter $scanFilter | ConvertFrom-DDBItem

出力:

Name Value ---- ----- Genre Country Artist No One You Know Price 1.94 CriticRating 9 SongTitle Somewhere Down The Road AlbumTitle Somewhat Famous
  • API の詳細については、「AWS Tools for PowerShell Cmdlet Reference」の「Scan」を参照してください。

Python
SDK for Python (Boto3)
注記

GitHub には、その他のリソースもあります。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

class Movies: """Encapsulates an Amazon DynamoDB table of movie data.""" def __init__(self, dyn_resource): """ :param dyn_resource: A Boto3 DynamoDB resource. """ self.dyn_resource = dyn_resource # The table variable is set during the scenario in the call to # 'exists' if the table exists. Otherwise, it is set by 'create_table'. self.table = None def scan_movies(self, year_range): """ 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: The range of years to retrieve. :return: The list of movies released in the specified years. """ movies = [] scan_kwargs = { "FilterExpression": Key("year").between( year_range["first"], year_range["second"] ), "ProjectionExpression": "#yr, title, info.rating", "ExpressionAttributeNames": {"#yr": "year"}, } try: done = False start_key = None while not done: if start_key: scan_kwargs["ExclusiveStartKey"] = start_key response = self.table.scan(**scan_kwargs) movies.extend(response.get("Items", [])) start_key = response.get("LastEvaluatedKey", None) done = start_key is None except ClientError as err: logger.error( "Couldn't scan for movies. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise return movies
  • API の詳細については、「AWS SDK for Python (Boto3) API リファレンス」の「Scan」を参照してください。

Ruby
SDK for Ruby
注記

GitHub には、その他のリソースもあります。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

class DynamoDBBasics attr_reader :dynamo_resource attr_reader :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
  • API の詳細については、「AWS SDK for Ruby API リファレンス」の「Scan」を参照してください。

Rust
SDK for Rust
注記

GitHub には、その他のリソースもあります。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

pub async fn list_items(client: &Client, table: &str, page_size: Option<i32>) -> Result<(), Error> { let page_size = page_size.unwrap_or(10); let items: Result<Vec<_>, _> = client .scan() .table_name(table) .limit(page_size) .into_paginator() .items() .send() .collect() .await; println!("Items in table (up to {page_size}):"); for item in items? { println!(" {:?}", item); } Ok(()) }
  • API の詳細については、「AWS SDK for Rust API リファレンス」の「Scan」を参照してください。

SAP ABAP
SDK for SAP ABAP
注記

GitHub には、その他のリソースもあります。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

TRY. " Scan movies for rating greater than or equal to the rating specified DATA(lt_attributelist) = VALUE /aws1/cl_dynattributevalue=>tt_attributevaluelist( ( NEW /aws1/cl_dynattributevalue( iv_n = |{ iv_rating }| ) ) ). DATA(lt_filter_conditions) = VALUE /aws1/cl_dyncondition=>tt_filterconditionmap( ( VALUE /aws1/cl_dyncondition=>ts_filterconditionmap_maprow( key = 'rating' value = NEW /aws1/cl_dyncondition( it_attributevaluelist = lt_attributelist iv_comparisonoperator = |GE| ) ) ) ). oo_scan_result = lo_dyn->scan( iv_tablename = iv_table_name it_scanfilter = lt_filter_conditions ). DATA(lt_items) = oo_scan_result->get_items( ). LOOP AT lt_items INTO DATA(lo_item). " You can loop over to get individual attributes. DATA(lo_title) = lo_item[ key = 'title' ]-value. DATA(lo_year) = lo_item[ key = 'year' ]-value. ENDLOOP. DATA(lv_count) = oo_scan_result->get_count( ). MESSAGE 'Found ' && lv_count && ' items' TYPE 'I'. CATCH /aws1/cx_dynresourcenotfoundex. MESSAGE 'The table or index does not exist' TYPE 'E'. ENDTRY.
  • API の詳細については、「AWS SDK for SAP ABAP API リファレンス」の「Scan」を参照してください。

Swift
SDK for Swift
注記

これはプレビューリリースの SDK に関するプレリリースドキュメントです。このドキュメントは変更される可能性があります。

注記

GitHub には、その他のリソースもあります。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

/// Return an array of `Movie` objects released in the specified range of /// years. /// /// - Parameters: /// - firstYear: The first year of movies to return. /// - lastYear: The last year of movies to return. /// - startKey: A starting point to resume processing; always use `nil`. /// /// - Returns: An array of `Movie` objects describing the matching movies. /// /// > Note: The `startKey` parameter is used by this function when /// recursively calling itself, and should always be `nil` when calling /// directly. /// func getMovies(firstYear: Int, lastYear: Int, startKey: [Swift.String:DynamoDBClientTypes.AttributeValue]? = nil) async throws -> [Movie] { var movieList: [Movie] = [] guard let client = self.ddbClient else { throw MoviesError.UninitializedClient } let input = ScanInput( consistentRead: true, exclusiveStartKey: startKey, expressionAttributeNames: [ "#y": "year" // `year` is a reserved word, so use `#y` instead. ], expressionAttributeValues: [ ":y1": .n(String(firstYear)), ":y2": .n(String(lastYear)) ], filterExpression: "#y BETWEEN :y1 AND :y2", tableName: self.tableName ) let output = try await client.scan(input: input) guard let items = output.items else { return movieList } // Build an array of `Movie` objects for the returned items. for item in items { let movie = try Movie(withItem: item) movieList.append(movie) } // Call this function recursively to continue collecting matching // movies, if necessary. if output.lastEvaluatedKey != nil { let movies = try await self.getMovies(firstYear: firstYear, lastYear: lastYear, startKey: output.lastEvaluatedKey) movieList += movies } return movieList }
  • API の詳細については、「AWS SDK for Swift API リファレンス」の「Scan」を参照してください。

DynamoDB のその他の例については、「AWS SDK を使用した DynamoDB のコード例」を参照してください。