Fase 5: esecuzione di query in una tabella - Amazon DynamoDB

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Fase 5: esecuzione di query in una tabella

In questa fase, esegui le query dei dati che hai scritto nella tabella Music in Fase 2: Scrivere i dati in una tabella specificando Artist. In questo modo verranno visualizzate tutte le canzoni associate alla chiave di partizione: Artist.

Per ulteriori informazioni sulle operazioni delle query, consulta Interrogazione di tabelle in DynamoDB.

Segui questi passaggi per utilizzare la console DynamoDB per eseguire query dei dati nella tabella Music.

  1. Apri la console DynamoDB all'indirizzo. https://console.aws.amazon.com/dynamodb/

  2. Nel riquadro di navigazione a sinistra, selezionare Tables (Tabelle).

  3. Nell'elenco delle tabelle, scegli la tabella Musica.

  4. Scegli Explore table items (Esplora elementi della tabella).

  5. In Scan o Query items, assicurati che l'opzione Query sia selezionata.

  6. In Partition key (Chiave di partizione), inserisci Acme Band, quindi scegli Run (Esegui).

I seguenti AWS CLI un esempio interroga un elemento nella Music tabella. È possibile farlo tramite API DynamoDB o PartiQLSQL, un linguaggio di query compatibile con DynamoDB.

DynamoDB API

È possibile interrogare un elemento tramite API DynamoDB query utilizzando e fornendo la chiave di partizione.

Linux

aws dynamodb query \ --table-name Music \ --key-condition-expression "Artist = :name" \ --expression-attribute-values '{":name":{"S":"Acme Band"}}'

Windows CMD

aws dynamodb query ^ --table-name Music ^ --key-condition-expression "Artist = :name" ^ --expression-attribute-values "{\":name\":{\"S\":\"Acme Band\"}}"

L’utilizzo di query restituisce tutte le canzoni associate a questo particolare Artist.

{ "Items": [ { "AlbumTitle": { "S": "Updated Album Title" }, "Awards": { "N": "10" }, "Artist": { "S": "Acme Band" }, "SongTitle": { "S": "Happy Day" } }, { "AlbumTitle": { "S": "Another Album Title" }, "Awards": { "N": "8" }, "Artist": { "S": "Acme Band" }, "SongTitle": { "S": "PartiQL Rocks" } } ], "Count": 2, "ScannedCount": 2, "ConsumedCapacity": null }
PartiQL for DynamoDB

È possibile eseguire una query su PartiQL utilizzando l’istruzione Select e fornendo la chiave di partizione.

Linux

aws dynamodb execute-statement --statement "SELECT * FROM Music \ WHERE Artist='Acme Band'"

Finestre CMD

aws dynamodb execute-statement --statement "SELECT * FROM Music WHERE Artist='Acme Band'"

L’utilizzo della dichiarazione Select in questo modo restituisce tutte le canzoni associate a questo particolare Artist.

{ "Items": [ { "AlbumTitle": { "S": "Updated Album Title" }, "Awards": { "S": "10" }, "Artist": { "S": "Acme Band" }, "SongTitle": { "S": "Happy Day" } }, { "AlbumTitle": { "S": "Another Album Title" }, "Awards": { "S": "8" }, "Artist": { "S": "Acme Band" }, "SongTitle": { "S": "PartiQL Rocks" } } ] }

Per ulteriori informazioni sull’esecuzione di query sui dati con PartiQL, consulta Istruzioni select di PartiQL.

I seguenti esempi di codice mostrano come interrogare una tabella DynamoDB utilizzando un AWS SDK.

.NET
AWS SDK for .NET
Nota

C'è altro su. GitHub Trova l'esempio completo e scopri come configurare ed eseguire in AWS Repository di esempi di codice.

/// <summary> /// Queries the table for movies released in a particular year and /// then displays the information for the movies returned. /// </summary> /// <param name="client">The initialized DynamoDB client object.</param> /// <param name="tableName">The name of the table to query.</param> /// <param name="year">The release year for which we want to /// view movies.</param> /// <returns>The number of movies that match the query.</returns> public static async Task<int> QueryMoviesAsync(AmazonDynamoDBClient client, string tableName, int year) { var movieTable = Table.LoadTable(client, tableName); var filter = new QueryFilter("year", QueryOperator.Equal, year); Console.WriteLine("\nFind movies released in: {year}:"); var config = new QueryOperationConfig() { Limit = 10, // 10 items per page. Select = SelectValues.SpecificAttributes, AttributesToGet = new List<string> { "title", "year", }, ConsistentRead = true, Filter = filter, }; // Value used to track how many movies match the // supplied criteria. var moviesFound = 0; Search search = movieTable.Query(config); do { var movieList = await search.GetNextSetAsync(); moviesFound += movieList.Count; foreach (var movie in movieList) { DisplayDocument(movie); } } while (!search.IsDone); return moviesFound; }
  • Per API i dettagli, vedere Query in AWS SDK for .NET APIRiferimento.

Bash
AWS CLI con script Bash
Nota

C'è altro da fare. GitHub Trova l'esempio completo e scopri come configurare ed eseguire in AWS Repository di esempi di codice.

############################################################################# # function dynamodb_query # # This function queries a DynamoDB table. # # Parameters: # -n table_name -- The name of the table. # -k key_condition_expression -- The key condition expression. # -a attribute_names -- Path to JSON file containing the attribute names. # -v attribute_values -- Path to JSON file containing the attribute values. # [-p projection_expression] -- Optional projection expression. # # Returns: # The items as json output. # And: # 0 - If successful. # 1 - If it fails. ########################################################################### function dynamodb_query() { local table_name key_condition_expression attribute_names attribute_values projection_expression response local option OPTARG # Required to use getopts command in a function. # ###################################### # Function usage explanation ####################################### function usage() { echo "function dynamodb_query" echo "Query a DynamoDB table." echo " -n table_name -- The name of the table." echo " -k key_condition_expression -- The key condition expression." echo " -a attribute_names -- Path to JSON file containing the attribute names." echo " -v attribute_values -- Path to JSON file containing the attribute values." echo " [-p projection_expression] -- Optional projection expression." echo "" } while getopts "n:k:a:v:p:h" option; do case "${option}" in n) table_name="${OPTARG}" ;; k) key_condition_expression="${OPTARG}" ;; a) attribute_names="${OPTARG}" ;; v) 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 "$key_condition_expression" ]]; then errecho "ERROR: You must provide a key condition expression with the -k parameter." usage return 1 fi if [[ -z "$attribute_names" ]]; then errecho "ERROR: You must provide a attribute names with the -a parameter." usage return 1 fi if [[ -z "$attribute_values" ]]; then errecho "ERROR: You must provide a attribute values with the -v parameter." usage return 1 fi if [[ -z "$projection_expression" ]]; then response=$(aws dynamodb query \ --table-name "$table_name" \ --key-condition-expression "$key_condition_expression" \ --expression-attribute-names file://"$attribute_names" \ --expression-attribute-values file://"$attribute_values") else response=$(aws dynamodb query \ --table-name "$table_name" \ --key-condition-expression "$key_condition_expression" \ --expression-attribute-names file://"$attribute_names" \ --expression-attribute-values file://"$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 query operation failed.$response" return 1 fi echo "$response" return 0 }

Le funzioni di utilità utilizzate in questo esempio.

############################################################################### # 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 }
  • Per API i dettagli, vedere Query in AWS CLI Riferimento ai comandi.

C++
SDKper C++
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri come configurare ed eseguire in AWS Repository di esempi di codice.

//! Perform a query on an Amazon DynamoDB Table and retrieve items. /*! \sa queryItem() \param tableName: The table name. \param partitionKey: The partition key. \param partitionValue: The value for the partition key. \param projectionExpression: The projections expression, which is ignored if empty. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ /* * The partition key attribute is searched with the specified value. By default, all fields and values * contained in the item are returned. If an optional projection expression is * specified on the command line, only the specified fields and values are * returned. */ bool AwsDoc::DynamoDB::queryItems(const Aws::String &tableName, const Aws::String &partitionKey, const Aws::String &partitionValue, const Aws::String &projectionExpression, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration); Aws::DynamoDB::Model::QueryRequest request; request.SetTableName(tableName); if (!projectionExpression.empty()) { request.SetProjectionExpression(projectionExpression); } // Set query key condition expression. request.SetKeyConditionExpression(partitionKey + "= :valueToMatch"); // Set Expression AttributeValues. Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> attributeValues; attributeValues.emplace(":valueToMatch", partitionValue); request.SetExpressionAttributeValues(attributeValues); bool result = true; // "exclusiveStartKey" is used for pagination. Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> exclusiveStartKey; do { if (!exclusiveStartKey.empty()) { request.SetExclusiveStartKey(exclusiveStartKey); exclusiveStartKey.clear(); } // Perform Query operation. const Aws::DynamoDB::Model::QueryOutcome &outcome = dynamoClient.Query(request); if (outcome.IsSuccess()) { // Reference the retrieved items. const Aws::Vector<Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue>> &items = outcome.GetResult().GetItems(); if (!items.empty()) { std::cout << "Number of items retrieved from Query: " << items.size() << std::endl; // Iterate each item and print. for (const auto &item: items) { std::cout << "******************************************************" << std::endl; // Output each retrieved field and its value. for (const auto &i: item) std::cout << i.first << ": " << i.second.GetS() << std::endl; } } else { std::cout << "No item found in table: " << tableName << std::endl; } exclusiveStartKey = outcome.GetResult().GetLastEvaluatedKey(); } else { std::cerr << "Failed to Query items: " << outcome.GetError().GetMessage(); result = false; break; } } while (!exclusiveStartKey.empty()); return result; }
  • Per API i dettagli, vedere Query in AWS SDK for C++ APIRiferimento.

CLI
AWS CLI

Esempio 1: interrogare una tabella

L'queryesempio seguente interroga gli elementi della MusicCollection tabella. La tabella ha una chiave hash-and-range primaria (ArtisteSongTitle), ma questa query specifica solo il valore della chiave hash. Restituisce i titoli delle canzoni dell'artista chiamato «No One You Know».

aws dynamodb query \ --table-name MusicCollection \ --projection-expression "SongTitle" \ --key-condition-expression "Artist = :v1" \ --expression-attribute-values file://expression-attributes.json \ --return-consumed-capacity TOTAL

Contenuto di expression-attributes.json.

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

Output:

{ "Items": [ { "SongTitle": { "S": "Call Me Today" }, "SongTitle": { "S": "Scared of My Shadow" } } ], "Count": 2, "ScannedCount": 2, "ConsumedCapacity": { "TableName": "MusicCollection", "CapacityUnits": 0.5 } }

Per ulteriori informazioni, consulta Working with Queries in DynamoDB nella Amazon DynamoDB Developer Guide.

Esempio 2: interrogare una tabella utilizzando letture fortemente coerenti e attraversare l'indice in ordine decrescente

L'esempio seguente esegue la stessa query del primo esempio, ma restituisce i risultati in ordine inverso e utilizza letture fortemente coerenti.

aws dynamodb query \ --table-name MusicCollection \ --projection-expression "SongTitle" \ --key-condition-expression "Artist = :v1" \ --expression-attribute-values file://expression-attributes.json \ --consistent-read \ --no-scan-index-forward \ --return-consumed-capacity TOTAL

Contenuto di expression-attributes.json.

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

Output:

{ "Items": [ { "SongTitle": { "S": "Scared of My Shadow" } }, { "SongTitle": { "S": "Call Me Today" } } ], "Count": 2, "ScannedCount": 2, "ConsumedCapacity": { "TableName": "MusicCollection", "CapacityUnits": 1.0 } }

Per ulteriori informazioni, consulta Working with Queries in DynamoDB nella Amazon DynamoDB Developer Guide.

Esempio 3: per filtrare risultati specifici

L'esempio seguente interroga MusicCollection ma esclude i risultati con valori specifici nell'AlbumTitleattributo. Si noti che ciò non influisce sull'ScannedCountoConsumedCapacity, poiché il filtro viene applicato dopo la lettura degli elementi.

aws dynamodb query \ --table-name MusicCollection \ --key-condition-expression "#n1 = :v1" \ --filter-expression "NOT (#n2 IN (:v2, :v3))" \ --expression-attribute-names file://names.json \ --expression-attribute-values file://values.json \ --return-consumed-capacity TOTAL

Contenuto di values.json.

{ ":v1": {"S": "No One You Know"}, ":v2": {"S": "Blue Sky Blues"}, ":v3": {"S": "Greatest Hits"} }

Contenuto di names.json.

{ "#n1": "Artist", "#n2": "AlbumTitle" }

Output:

{ "Items": [ { "AlbumTitle": { "S": "Somewhat Famous" }, "Artist": { "S": "No One You Know" }, "SongTitle": { "S": "Call Me Today" } } ], "Count": 1, "ScannedCount": 2, "ConsumedCapacity": { "TableName": "MusicCollection", "CapacityUnits": 0.5 } }

Per ulteriori informazioni, consulta Working with Queries in DynamoDB nella Amazon DynamoDB Developer Guide.

Esempio 4: per recuperare solo il numero di articoli

L'esempio seguente recupera il conteggio degli elementi corrispondenti alla query, ma non recupera nessuno degli elementi stessi.

aws dynamodb query \ --table-name MusicCollection \ --select COUNT \ --key-condition-expression "Artist = :v1" \ --expression-attribute-values file://expression-attributes.json

Contenuto di expression-attributes.json.

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

Output:

{ "Count": 2, "ScannedCount": 2, "ConsumedCapacity": null }

Per ulteriori informazioni, consulta Working with Queries in DynamoDB nella Amazon DynamoDB Developer Guide.

Esempio 5: interrogare un indice

L'esempio seguente esegue una query sull'indice AlbumTitleIndex secondario locale. La query restituisce tutti gli attributi della tabella di base che sono stati proiettati nell'indice secondario locale. Si noti che quando si esegue una query su un indice secondario locale o su un indice secondario globale, è necessario fornire anche il nome della tabella di base utilizzando il table-name parametro.

aws dynamodb query \ --table-name MusicCollection \ --index-name AlbumTitleIndex \ --key-condition-expression "Artist = :v1" \ --expression-attribute-values file://expression-attributes.json \ --select ALL_PROJECTED_ATTRIBUTES \ --return-consumed-capacity INDEXES

Contenuto di expression-attributes.json.

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

Output:

{ "Items": [ { "AlbumTitle": { "S": "Blue Sky Blues" }, "Artist": { "S": "No One You Know" }, "SongTitle": { "S": "Scared of My Shadow" } }, { "AlbumTitle": { "S": "Somewhat Famous" }, "Artist": { "S": "No One You Know" }, "SongTitle": { "S": "Call Me Today" } } ], "Count": 2, "ScannedCount": 2, "ConsumedCapacity": { "TableName": "MusicCollection", "CapacityUnits": 0.5, "Table": { "CapacityUnits": 0.0 }, "LocalSecondaryIndexes": { "AlbumTitleIndex": { "CapacityUnits": 0.5 } } } }

Per ulteriori informazioni, consulta Working with Queries in DynamoDB nella Amazon DynamoDB Developer Guide.

Go
SDKper Go V2
Nota

C'è altro da fare. GitHub Trova l'esempio completo e scopri come configurare ed eseguire in AWS Repository di esempi di codice.

// 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 } // Query gets all movies in the DynamoDB table that were released in the specified year. // The function uses the `expression` package to build the key condition expression // that is used in the query. func (basics TableBasics) Query(releaseYear int) ([]Movie, error) { var err error var response *dynamodb.QueryOutput var movies []Movie keyEx := expression.Key("year").Equal(expression.Value(releaseYear)) expr, err := expression.NewBuilder().WithKeyCondition(keyEx).Build() if err != nil { log.Printf("Couldn't build expression for query. Here's why: %v\n", err) } else { queryPaginator := dynamodb.NewQueryPaginator(basics.DynamoDbClient, &dynamodb.QueryInput{ TableName: aws.String(basics.TableName), ExpressionAttributeNames: expr.Names(), ExpressionAttributeValues: expr.Values(), KeyConditionExpression: expr.KeyCondition(), }) for queryPaginator.HasMorePages() { response, err = queryPaginator.NextPage(context.TODO()) if err != nil { log.Printf("Couldn't query for movies released in %v. Here's why: %v\n", releaseYear, 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"]) }
  • Per API i dettagli, vedere Query in AWS SDK for Go APIRiferimento.

Java
SDKper Java 2.x
Nota

C'è altro su. GitHub Trova l'esempio completo e scopri come configurare ed eseguire in AWS Repository di esempi di codice.

Interroga una tabella utilizzando. DynamoDbClient

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.QueryRequest; import software.amazon.awssdk.services.dynamodb.model.QueryResponse; import java.util.HashMap; /** * 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 query items from an Amazon DynamoDB table using the AWS SDK for Java V2, * its better practice to use the * Enhanced Client. See the EnhancedQueryRecords example. */ public class Query { public static void main(String[] args) { final String usage = """ Usage: <tableName> <partitionKeyName> <partitionKeyVal> Where: tableName - The Amazon DynamoDB table to put the item in (for example, Music3). partitionKeyName - The partition key name of the Amazon DynamoDB table (for example, Artist). partitionKeyVal - The value of the partition key that should match (for example, Famous Band). """; if (args.length != 3) { System.out.println(usage); System.exit(1); } String tableName = args[0]; String partitionKeyName = args[1]; String partitionKeyVal = args[2]; // For more information about an alias, see: // https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ExpressionAttributeNames.html String partitionAlias = "#a"; System.out.format("Querying %s", tableName); System.out.println(""); Region region = Region.US_EAST_1; DynamoDbClient ddb = DynamoDbClient.builder() .region(region) .build(); int count = queryTable(ddb, tableName, partitionKeyName, partitionKeyVal, partitionAlias); System.out.println("There were " + count + " record(s) returned"); ddb.close(); } public static int queryTable(DynamoDbClient ddb, String tableName, String partitionKeyName, String partitionKeyVal, String partitionAlias) { // Set up an alias for the partition key name in case it's a reserved word. HashMap<String, String> attrNameAlias = new HashMap<String, String>(); attrNameAlias.put(partitionAlias, partitionKeyName); // Set up mapping of the partition name with the value. HashMap<String, AttributeValue> attrValues = new HashMap<>(); attrValues.put(":" + partitionKeyName, AttributeValue.builder() .s(partitionKeyVal) .build()); QueryRequest queryReq = QueryRequest.builder() .tableName(tableName) .keyConditionExpression(partitionAlias + " = :" + partitionKeyName) .expressionAttributeNames(attrNameAlias) .expressionAttributeValues(attrValues) .build(); try { QueryResponse response = ddb.query(queryReq); return response.count(); } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } return -1; } }

Esegue query su una tabella utilizzando DynamoDbClient e un indice secondario.

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.QueryRequest; import software.amazon.awssdk.services.dynamodb.model.QueryResponse; import java.util.HashMap; import java.util.Map; /** * 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 * * Create the Movies table by running the Scenario example and loading the Movie * data from the JSON file. Next create a secondary * index for the Movies table that uses only the year column. Name the index * **year-index**. For more information, see: * * https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html */ public class QueryItemsUsingIndex { public static void main(String[] args) { String tableName = "Movies"; Region region = Region.US_EAST_1; DynamoDbClient ddb = DynamoDbClient.builder() .region(region) .build(); queryIndex(ddb, tableName); ddb.close(); } public static void queryIndex(DynamoDbClient ddb, String tableName) { try { Map<String, String> expressionAttributesNames = new HashMap<>(); expressionAttributesNames.put("#year", "year"); Map<String, AttributeValue> expressionAttributeValues = new HashMap<>(); expressionAttributeValues.put(":yearValue", AttributeValue.builder().n("2013").build()); QueryRequest request = QueryRequest.builder() .tableName(tableName) .indexName("year-index") .keyConditionExpression("#year = :yearValue") .expressionAttributeNames(expressionAttributesNames) .expressionAttributeValues(expressionAttributeValues) .build(); System.out.println("=== Movie Titles ==="); QueryResponse response = ddb.query(request); response.items() .forEach(movie -> System.out.println(movie.get("title").s())); } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } } }
  • Per API i dettagli, vedere Query in AWS SDK for Java 2.x APIRiferimento.

JavaScript
SDKper JavaScript (v3)
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri come configurare ed eseguire in AWS Repository di esempi di codice.

Questo esempio utilizza il client di documenti per semplificare il lavoro con gli elementi in DynamoDB. Per i API dettagli, vedere QueryCommand.

import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; import { QueryCommand, DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb"; const client = new DynamoDBClient({}); const docClient = DynamoDBDocumentClient.from(client); export const main = async () => { const command = new QueryCommand({ TableName: "CoffeeCrop", KeyConditionExpression: "OriginCountry = :originCountry AND RoastDate > :roastDate", ExpressionAttributeValues: { ":originCountry": "Ethiopia", ":roastDate": "2023-05-01", }, ConsistentRead: true, }); const response = await docClient.send(command); console.log(response); return response; };
SDKper JavaScript (v2)
Nota

C'è altro da fare. GitHub Trova l'esempio completo e scopri come configurare ed eseguire in AWS Repository di esempi di codice.

// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set the region AWS.config.update({ region: "REGION" }); // Create DynamoDB document client var docClient = new AWS.DynamoDB.DocumentClient({ apiVersion: "2012-08-10" }); var params = { ExpressionAttributeValues: { ":s": 2, ":e": 9, ":topic": "PHRASE", }, KeyConditionExpression: "Season = :s and Episode > :e", FilterExpression: "contains (Subtitle, :topic)", TableName: "EPISODES_TABLE", }; docClient.query(params, function (err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.Items); } });
Kotlin
SDKper Kotlin
Nota

c'è altro da fare. GitHub Trova l'esempio completo e scopri come configurare ed eseguire in AWS Repository di esempi di codice.

suspend fun queryDynTable( tableNameVal: String, partitionKeyName: String, partitionKeyVal: String, partitionAlias: String, ): Int { val attrNameAlias = mutableMapOf<String, String>() attrNameAlias[partitionAlias] = partitionKeyName // Set up mapping of the partition name with the value. val attrValues = mutableMapOf<String, AttributeValue>() attrValues[":$partitionKeyName"] = AttributeValue.S(partitionKeyVal) val request = QueryRequest { tableName = tableNameVal keyConditionExpression = "$partitionAlias = :$partitionKeyName" expressionAttributeNames = attrNameAlias this.expressionAttributeValues = attrValues } DynamoDbClient { region = "us-east-1" }.use { ddb -> val response = ddb.query(request) return response.count } }
  • Per API i dettagli, vedere Query in AWS SDKper riferimento a KotlinAPI.

PHP
SDK per PHP
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri come configurare ed eseguire in AWS Repository di esempi di codice.

$birthKey = [ 'Key' => [ 'year' => [ 'N' => "$birthYear", ], ], ]; $result = $service->query($tableName, $birthKey); public function query(string $tableName, $key) { $expressionAttributeValues = []; $expressionAttributeNames = []; $keyConditionExpression = ""; $index = 1; foreach ($key as $name => $value) { $keyConditionExpression .= "#" . array_key_first($value) . " = :v$index,"; $expressionAttributeNames["#" . array_key_first($value)] = array_key_first($value); $hold = array_pop($value); $expressionAttributeValues[":v$index"] = [ array_key_first($hold) => array_pop($hold), ]; } $keyConditionExpression = substr($keyConditionExpression, 0, -1); $query = [ 'ExpressionAttributeValues' => $expressionAttributeValues, 'ExpressionAttributeNames' => $expressionAttributeNames, 'KeyConditionExpression' => $keyConditionExpression, 'TableName' => $tableName, ]; return $this->dynamoDbClient->query($query); }
  • Per API i dettagli, vedere Query in AWS SDK for PHP APIRiferimento.

PowerShell
Strumenti per PowerShell

Esempio 1: richiama una query che restituisce elementi DynamoDB con l'elemento Artist specificato. SongTitle

$invokeDDBQuery = @{ TableName = 'Music' KeyConditionExpression = ' SongTitle = :SongTitle and Artist = :Artist' ExpressionAttributeValues = @{ ':SongTitle' = 'Somewhere Down The Road' ':Artist' = 'No One You Know' } | ConvertTo-DDBItem } Invoke-DDBQuery @invokeDDBQuery | ConvertFrom-DDBItem

Output:

Name Value ---- ----- Genre Country Artist No One You Know Price 1.94 CriticRating 9 SongTitle Somewhere Down The Road AlbumTitle Somewhat Famous
Python
SDKper Python (Boto3)
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri come configurare ed eseguire in AWS Repository di esempi di codice.

Esegui la query degli elementi utilizzando un'espressione di condizione chiave.

class Movies: """Encapsulates an Amazon DynamoDB table of movie data. Example data structure for a movie record in this table: { "year": 1999, "title": "For Love of the Game", "info": { "directors": ["Sam Raimi"], "release_date": "1999-09-15T00:00:00Z", "rating": 6.3, "plot": "A washed up pitcher flashes through his career.", "rank": 4987, "running_time_secs": 8220, "actors": [ "Kevin Costner", "Kelly Preston", "John C. Reilly" ] } } """ 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 query_movies(self, year): """ Queries for movies that were released in the specified year. :param year: The year to query. :return: The list of movies that were released in the specified year. """ try: response = self.table.query(KeyConditionExpression=Key("year").eq(year)) except ClientError as err: logger.error( "Couldn't query for movies released in %s. Here's why: %s: %s", year, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return response["Items"]

Esegui la query degli elementi e proiettali per restituire un sottoinsieme di dati.

class UpdateQueryWrapper: def __init__(self, table): self.table = table def query_and_project_movies(self, year, title_bounds): """ Query for movies that were released in a specified year and that have titles that start within a range of letters. A projection expression is used to return a subset of data for each movie. :param year: The release year to query. :param title_bounds: The range of starting letters to query. :return: The list of movies. """ try: response = self.table.query( ProjectionExpression="#yr, title, info.genres, info.actors[0]", ExpressionAttributeNames={"#yr": "year"}, KeyConditionExpression=( Key("year").eq(year) & Key("title").between( title_bounds["first"], title_bounds["second"] ) ), ) except ClientError as err: if err.response["Error"]["Code"] == "ValidationException": logger.warning( "There's a validation error. Here's the message: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) else: logger.error( "Couldn't query for movies. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return response["Items"]
  • Per API i dettagli, vedere Query in AWS SDKper Python (Boto3) Reference. API

Ruby
SDKper Ruby
Nota

c'è altro da fare. GitHub Trova l'esempio completo e scopri come configurare ed eseguire in AWS Repository di esempi di codice.

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 # 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
  • Per API i dettagli, vedere Query in AWS SDK for Ruby APIRiferimento.

Rust
SDKper Rust
Nota

c'è altro da fare GitHub. Trova l'esempio completo e scopri come configurare ed eseguire in AWS Repository di esempi di codice.

Trova i filmati realizzati nell'anno specificato.

pub async fn movies_in_year( client: &Client, table_name: &str, year: u16, ) -> Result<Vec<Movie>, MovieError> { let results = client .query() .table_name(table_name) .key_condition_expression("#yr = :yyyy") .expression_attribute_names("#yr", "year") .expression_attribute_values(":yyyy", AttributeValue::N(year.to_string())) .send() .await?; if let Some(items) = results.items { let movies = items.iter().map(|v| v.into()).collect(); Ok(movies) } else { Ok(vec![]) } }
  • Per API i dettagli, vedere Query in AWS SDKper API riferimento a Rust.

SAP ABAP
SDKper SAP ABAP
Nota

C'è altro da fare GitHub. Trova l'esempio completo e scopri come configurare ed eseguire in AWS Repository di esempi di codice.

TRY. " Query movies for a given year . DATA(lt_attributelist) = VALUE /aws1/cl_dynattributevalue=>tt_attributevaluelist( ( NEW /aws1/cl_dynattributevalue( iv_n = |{ iv_year }| ) ) ). DATA(lt_key_conditions) = VALUE /aws1/cl_dyncondition=>tt_keyconditions( ( VALUE /aws1/cl_dyncondition=>ts_keyconditions_maprow( key = 'year' value = NEW /aws1/cl_dyncondition( it_attributevaluelist = lt_attributelist iv_comparisonoperator = |EQ| ) ) ) ). oo_result = lo_dyn->query( iv_tablename = iv_table_name it_keyconditions = lt_key_conditions ). DATA(lt_items) = oo_result->get_items( ). "You can loop over the results to get item attributes. LOOP AT lt_items INTO DATA(lt_item). DATA(lo_title) = lt_item[ key = 'title' ]-value. DATA(lo_year) = lt_item[ key = 'year' ]-value. ENDLOOP. DATA(lv_count) = oo_result->get_count( ). MESSAGE 'Item count is: ' && lv_count TYPE 'I'. CATCH /aws1/cx_dynresourcenotfoundex. MESSAGE 'The table or index does not exist' TYPE 'E'. ENDTRY.
  • Per API i dettagli, vedere Query in AWS SDKper SAP ABAP API riferimento.

Swift
SDKper Swift
Nota

Questa è la documentazione preliminare per una versione SDK in anteprima. ed è soggetta a modifiche.

Nota

C'è altro su. GitHub Trova l'esempio completo e scopri come configurare ed eseguire in AWS Repository di esempi di codice.

import AWSDynamoDB /// Get all the movies released in the specified year. /// /// - Parameter year: The release year of the movies to return. /// /// - Returns: An array of `Movie` objects describing each matching movie. /// func getMovies(fromYear year: Int) async throws -> [Movie] { guard let client = self.ddbClient else { throw MoviesError.UninitializedClient } let input = QueryInput( expressionAttributeNames: [ "#y": "year" ], expressionAttributeValues: [ ":y": .n(String(year)) ], keyConditionExpression: "#y = :y", tableName: self.tableName ) let output = try await client.query(input: input) guard let items = output.items else { throw MoviesError.ItemNotFound } // Convert the found movies into `Movie` objects and return an array // of them. var movieList: [Movie] = [] for item in items { let movie = try Movie(withItem: item) movieList.append(movie) } return movieList }
  • Per API i dettagli, vedere Query in AWS SDKper API riferimento a Swift.

Per ulteriori esempi di DynamoDB, consulta Esempi di codice per l'utilizzo di DynamoDB AWS SDKs.

Per creare un indice globale secondario per la tua tabella, procedi con Fase 6: (Facoltativo) Eliminare la tabella per ripulire le risorse.