Run batches of PartiQL statements on a DynamoDB table using an Amazon SDK - Amazon DynamoDB
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Run batches of PartiQL statements on a DynamoDB table using an Amazon SDK

The following code examples show how to run batches of PartiQL statements on a DynamoDB table.

Action examples are code excerpts from larger programs and must be run in context. You can see this action in context in the following code example:

.NET
Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Use batches of INSERT statements to add items.

/// <summary> /// Inserts movies imported from a JSON file into the movie table by /// using an Amazon DynamoDB PartiQL INSERT statement. /// </summary> /// <param name="tableName">The name of the table into which the movie /// information will be inserted.</param> /// <param name="movieFileName">The name of the JSON file that contains /// movie information.</param> /// <returns>A Boolean value that indicates the success or failure of /// the insert operation.</returns> public static async Task<bool> InsertMovies(string tableName, string movieFileName) { // Get the list of movies from the JSON file. var movies = ImportMovies(movieFileName); var success = false; if (movies is not null) { // Insert the movies in a batch using PartiQL. Because the // batch can contain a maximum of 25 items, insert 25 movies // at a time. string insertBatch = $"INSERT INTO {tableName} VALUE {{'title': ?, 'year': ?}}"; var statements = new List<BatchStatementRequest>(); try { for (var indexOffset = 0; indexOffset < 250; indexOffset += 25) { for (var i = indexOffset; i < indexOffset + 25; i++) { statements.Add(new BatchStatementRequest { Statement = insertBatch, Parameters = new List<AttributeValue> { new AttributeValue { S = movies[i].Title }, new AttributeValue { N = movies[i].Year.ToString() }, }, }); } var response = await Client.BatchExecuteStatementAsync(new BatchExecuteStatementRequest { Statements = statements, }); // Wait between batches for movies to be successfully added. System.Threading.Thread.Sleep(3000); success = response.HttpStatusCode == System.Net.HttpStatusCode.OK; // Clear the list of statements for the next batch. statements.Clear(); } } catch (AmazonDynamoDBException ex) { Console.WriteLine(ex.Message); } } return success; } /// <summary> /// Loads the contents of a JSON file into a list of movies to be /// added to the DynamoDB table. /// </summary> /// <param name="movieFileName">The full path to the JSON file.</param> /// <returns>A generic list of movie objects.</returns> public static List<Movie> ImportMovies(string movieFileName) { if (!File.Exists(movieFileName)) { return null!; } using var sr = new StreamReader(movieFileName); string json = sr.ReadToEnd(); var allMovies = JsonConvert.DeserializeObject<List<Movie>>(json); if (allMovies is not null) { // Return the first 250 entries. return allMovies.GetRange(0, 250); } else { return null!; } }

Use batches of SELECT statements to get items.

/// <summary> /// Gets movies from the movie table by /// using an Amazon DynamoDB PartiQL SELECT statement. /// </summary> /// <param name="tableName">The name of the table.</param> /// <param name="title1">The title of the first movie.</param> /// <param name="title2">The title of the second movie.</param> /// <param name="year1">The year of the first movie.</param> /// <param name="year2">The year of the second movie.</param> /// <returns>True if successful.</returns> public static async Task<bool> GetBatch( string tableName, string title1, string title2, int year1, int year2) { var getBatch = $"SELECT FROM {tableName} WHERE title = ? AND year = ?"; var statements = new List<BatchStatementRequest> { new BatchStatementRequest { Statement = getBatch, Parameters = new List<AttributeValue> { new AttributeValue { S = title1 }, new AttributeValue { N = year1.ToString() }, }, }, new BatchStatementRequest { Statement = getBatch, Parameters = new List<AttributeValue> { new AttributeValue { S = title2 }, new AttributeValue { N = year2.ToString() }, }, } }; var response = await Client.BatchExecuteStatementAsync(new BatchExecuteStatementRequest { Statements = statements, }); if (response.Responses.Count > 0) { response.Responses.ForEach(r => { Console.WriteLine($"{r.Item["title"]}\t{r.Item["year"]}"); }); return true; } else { Console.WriteLine($"Couldn't find either {title1} or {title2}."); return false; } }

Use batches of UPDATE statements to update items.

/// <summary> /// Updates information for multiple movies. /// </summary> /// <param name="tableName">The name of the table containing the /// movies to be updated.</param> /// <param name="producer1">The producer name for the first movie /// to update.</param> /// <param name="title1">The title of the first movie.</param> /// <param name="year1">The year that the first movie was released.</param> /// <param name="producer2">The producer name for the second /// movie to update.</param> /// <param name="title2">The title of the second movie.</param> /// <param name="year2">The year that the second movie was released.</param> /// <returns>A Boolean value that indicates the success of the update.</returns> public static async Task<bool> UpdateBatch( string tableName, string producer1, string title1, int year1, string producer2, string title2, int year2) { string updateBatch = $"UPDATE {tableName} SET Producer=? WHERE title = ? AND year = ?"; var statements = new List<BatchStatementRequest> { new BatchStatementRequest { Statement = updateBatch, Parameters = new List<AttributeValue> { new AttributeValue { S = producer1 }, new AttributeValue { S = title1 }, new AttributeValue { N = year1.ToString() }, }, }, new BatchStatementRequest { Statement = updateBatch, Parameters = new List<AttributeValue> { new AttributeValue { S = producer2 }, new AttributeValue { S = title2 }, new AttributeValue { N = year2.ToString() }, }, } }; var response = await Client.BatchExecuteStatementAsync(new BatchExecuteStatementRequest { Statements = statements, }); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; }

Use batches of DELETE statements to delete items.

/// <summary> /// Deletes multiple movies using a PartiQL BatchExecuteAsync /// statement. /// </summary> /// <param name="tableName">The name of the table containing the /// moves that will be deleted.</param> /// <param name="title1">The title of the first movie.</param> /// <param name="year1">The year the first movie was released.</param> /// <param name="title2">The title of the second movie.</param> /// <param name="year2">The year the second movie was released.</param> /// <returns>A Boolean value indicating the success of the operation.</returns> public static async Task<bool> DeleteBatch( string tableName, string title1, int year1, string title2, int year2) { string updateBatch = $"DELETE FROM {tableName} WHERE title = ? AND year = ?"; var statements = new List<BatchStatementRequest> { new BatchStatementRequest { Statement = updateBatch, Parameters = new List<AttributeValue> { new AttributeValue { S = title1 }, new AttributeValue { N = year1.ToString() }, }, }, new BatchStatementRequest { Statement = updateBatch, Parameters = new List<AttributeValue> { new AttributeValue { S = title2 }, new AttributeValue { N = year2.ToString() }, }, } }; var response = await Client.BatchExecuteStatementAsync(new BatchExecuteStatementRequest { Statements = statements, }); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; }
C++
SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Use batches of INSERT statements to add items.

// 2. Add multiple movies using "Insert" statements. (BatchExecuteStatement) Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration); std::vector<Aws::String> titles; std::vector<float> ratings; std::vector<int> years; std::vector<Aws::String> plots; Aws::String doAgain = "n"; do { Aws::String aTitle = askQuestion( "Enter the title of a movie you want to add to the table: "); titles.push_back(aTitle); int aYear = askQuestionForInt("What year was it released? "); years.push_back(aYear); float aRating = askQuestionForFloatRange( "On a scale of 1 - 10, how do you rate it? ", 1, 10); ratings.push_back(aRating); Aws::String aPlot = askQuestion("Summarize the plot for me: "); plots.push_back(aPlot); doAgain = askQuestion(Aws::String("Would you like to add more movies? (y/n) ")); } while (doAgain == "y"); std::cout << "Adding " << titles.size() << (titles.size() == 1 ? " movie " : " movies ") << "to the table using a batch \"INSERT\" statement." << std::endl; { Aws::Vector<Aws::DynamoDB::Model::BatchStatementRequest> statements( titles.size()); std::stringstream sqlStream; sqlStream << "INSERT INTO \"" << MOVIE_TABLE_NAME << "\" VALUE {'" << TITLE_KEY << "': ?, '" << YEAR_KEY << "': ?, '" << INFO_KEY << "': ?}"; std::string sql(sqlStream.str()); for (size_t i = 0; i < statements.size(); ++i) { statements[i].SetStatement(sql); Aws::Vector<Aws::DynamoDB::Model::AttributeValue> attributes; attributes.push_back( Aws::DynamoDB::Model::AttributeValue().SetS(titles[i])); attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetN(years[i])); // Create attribute for the info map. Aws::DynamoDB::Model::AttributeValue infoMapAttribute; std::shared_ptr<Aws::DynamoDB::Model::AttributeValue> ratingAttribute = Aws::MakeShared<Aws::DynamoDB::Model::AttributeValue>( ALLOCATION_TAG.c_str()); ratingAttribute->SetN(ratings[i]); infoMapAttribute.AddMEntry(RATING_KEY, ratingAttribute); std::shared_ptr<Aws::DynamoDB::Model::AttributeValue> plotAttribute = Aws::MakeShared<Aws::DynamoDB::Model::AttributeValue>( ALLOCATION_TAG.c_str()); plotAttribute->SetS(plots[i]); infoMapAttribute.AddMEntry(PLOT_KEY, plotAttribute); attributes.push_back(infoMapAttribute); statements[i].SetParameters(attributes); } Aws::DynamoDB::Model::BatchExecuteStatementRequest request; request.SetStatements(statements); Aws::DynamoDB::Model::BatchExecuteStatementOutcome outcome = dynamoClient.BatchExecuteStatement( request); if (!outcome.IsSuccess()) { std::cerr << "Failed to add the movies: " << outcome.GetError().GetMessage() << std::endl; return false; } }

Use batches of SELECT statements to get items.

// 3. Get the data for multiple movies using "Select" statements. (BatchExecuteStatement) { Aws::Vector<Aws::DynamoDB::Model::BatchStatementRequest> statements( titles.size()); std::stringstream sqlStream; sqlStream << "SELECT * FROM \"" << MOVIE_TABLE_NAME << "\" WHERE " << TITLE_KEY << "=? and " << YEAR_KEY << "=?"; std::string sql(sqlStream.str()); for (size_t i = 0; i < statements.size(); ++i) { statements[i].SetStatement(sql); Aws::Vector<Aws::DynamoDB::Model::AttributeValue> attributes; attributes.push_back( Aws::DynamoDB::Model::AttributeValue().SetS(titles[i])); attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetN(years[i])); statements[i].SetParameters(attributes); } Aws::DynamoDB::Model::BatchExecuteStatementRequest request; request.SetStatements(statements); Aws::DynamoDB::Model::BatchExecuteStatementOutcome outcome = dynamoClient.BatchExecuteStatement( request); if (outcome.IsSuccess()) { const Aws::DynamoDB::Model::BatchExecuteStatementResult &result = outcome.GetResult(); const Aws::Vector<Aws::DynamoDB::Model::BatchStatementResponse> &responses = result.GetResponses(); for (const Aws::DynamoDB::Model::BatchStatementResponse &response: responses) { const Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> &item = response.GetItem(); printMovieInfo(item); } } else { std::cerr << "Failed to retrieve the movie information: " << outcome.GetError().GetMessage() << std::endl; return false; } }

Use batches of UPDATE statements to update items.

// 4. Update the data for multiple movies using "Update" statements. (BatchExecuteStatement) for (size_t i = 0; i < titles.size(); ++i) { ratings[i] = askQuestionForFloatRange( Aws::String("\nLet's update your the movie, \"") + titles[i] + ".\nYou rated it " + std::to_string(ratings[i]) + ", what new rating would you give it? ", 1, 10); } std::cout << "Updating the movie with a batch \"UPDATE\" statement." << std::endl; { Aws::Vector<Aws::DynamoDB::Model::BatchStatementRequest> statements( titles.size()); std::stringstream sqlStream; sqlStream << "UPDATE \"" << MOVIE_TABLE_NAME << "\" SET " << INFO_KEY << "." << RATING_KEY << "=? WHERE " << TITLE_KEY << "=? AND " << YEAR_KEY << "=?"; std::string sql(sqlStream.str()); for (size_t i = 0; i < statements.size(); ++i) { statements[i].SetStatement(sql); Aws::Vector<Aws::DynamoDB::Model::AttributeValue> attributes; attributes.push_back( Aws::DynamoDB::Model::AttributeValue().SetN(ratings[i])); attributes.push_back( Aws::DynamoDB::Model::AttributeValue().SetS(titles[i])); attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetN(years[i])); statements[i].SetParameters(attributes); } Aws::DynamoDB::Model::BatchExecuteStatementRequest request; request.SetStatements(statements); Aws::DynamoDB::Model::BatchExecuteStatementOutcome outcome = dynamoClient.BatchExecuteStatement( request); if (!outcome.IsSuccess()) { std::cerr << "Failed to update movie information: " << outcome.GetError().GetMessage() << std::endl; return false; } }

Use batches of DELETE statements to delete items.

// 6. Delete multiple movies using "Delete" statements. (BatchExecuteStatement) { Aws::Vector<Aws::DynamoDB::Model::BatchStatementRequest> statements( titles.size()); std::stringstream sqlStream; sqlStream << "DELETE FROM \"" << MOVIE_TABLE_NAME << "\" WHERE " << TITLE_KEY << "=? and " << YEAR_KEY << "=?"; std::string sql(sqlStream.str()); for (size_t i = 0; i < statements.size(); ++i) { statements[i].SetStatement(sql); Aws::Vector<Aws::DynamoDB::Model::AttributeValue> attributes; attributes.push_back( Aws::DynamoDB::Model::AttributeValue().SetS(titles[i])); attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetN(years[i])); statements[i].SetParameters(attributes); } Aws::DynamoDB::Model::BatchExecuteStatementRequest request; request.SetStatements(statements); Aws::DynamoDB::Model::BatchExecuteStatementOutcome outcome = dynamoClient.BatchExecuteStatement( request); if (!outcome.IsSuccess()) { std::cerr << "Failed to delete the movies: " << outcome.GetError().GetMessage() << std::endl; return false; } }
Go
SDK for Go V2
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Use batches of INSERT statements to add items.

// AddMovieBatch runs a batch of PartiQL INSERT statements to add multiple movies to the // DynamoDB table. func (runner PartiQLRunner) AddMovieBatch(movies []Movie) error { statementRequests := make([]types.BatchStatementRequest, len(movies)) for index, movie := range movies { params, err := attributevalue.MarshalList([]interface{}{movie.Title, movie.Year, movie.Info}) if err != nil { panic(err) } statementRequests[index] = types.BatchStatementRequest{ Statement: aws.String(fmt.Sprintf( "INSERT INTO \"%v\" VALUE {'title': ?, 'year': ?, 'info': ?}", runner.TableName)), Parameters: params, } } _, err := runner.DynamoDbClient.BatchExecuteStatement(context.TODO(), &dynamodb.BatchExecuteStatementInput{ Statements: statementRequests, }) if err != nil { log.Printf("Couldn't insert a batch of items with PartiQL. Here's why: %v\n", err) } return err }

Use batches of SELECT statements to get items.

// GetMovieBatch runs a batch of PartiQL SELECT statements to get multiple movies from // the DynamoDB table by title and year. func (runner PartiQLRunner) GetMovieBatch(movies []Movie) ([]Movie, error) { statementRequests := make([]types.BatchStatementRequest, len(movies)) for index, movie := range movies { params, err := attributevalue.MarshalList([]interface{}{movie.Title, movie.Year}) if err != nil { panic(err) } statementRequests[index] = types.BatchStatementRequest{ Statement: aws.String( fmt.Sprintf("SELECT * FROM \"%v\" WHERE title=? AND year=?", runner.TableName)), Parameters: params, } } output, err := runner.DynamoDbClient.BatchExecuteStatement(context.TODO(), &dynamodb.BatchExecuteStatementInput{ Statements: statementRequests, }) var outMovies []Movie if err != nil { log.Printf("Couldn't get a batch of items with PartiQL. Here's why: %v\n", err) } else { for _, response := range output.Responses { var movie Movie err = attributevalue.UnmarshalMap(response.Item, &movie) if err != nil { log.Printf("Couldn't unmarshal response. Here's why: %v\n", err) } else { outMovies = append(outMovies, movie) } } } return outMovies, err }

Use batches of UPDATE statements to update items.

// UpdateMovieBatch runs a batch of PartiQL UPDATE statements to update the rating of // multiple movies that already exist in the DynamoDB table. func (runner PartiQLRunner) UpdateMovieBatch(movies []Movie, ratings []float64) error { statementRequests := make([]types.BatchStatementRequest, len(movies)) for index, movie := range movies { params, err := attributevalue.MarshalList([]interface{}{ratings[index], movie.Title, movie.Year}) if err != nil { panic(err) } statementRequests[index] = types.BatchStatementRequest{ Statement: aws.String( fmt.Sprintf("UPDATE \"%v\" SET info.rating=? WHERE title=? AND year=?", runner.TableName)), Parameters: params, } } _, err := runner.DynamoDbClient.BatchExecuteStatement(context.TODO(), &dynamodb.BatchExecuteStatementInput{ Statements: statementRequests, }) if err != nil { log.Printf("Couldn't update the batch of movies. Here's why: %v\n", err) } return err }

Use batches of DELETE statements to delete items.

// DeleteMovieBatch runs a batch of PartiQL DELETE statements to remove multiple movies // from the DynamoDB table. func (runner PartiQLRunner) DeleteMovieBatch(movies []Movie) error { statementRequests := make([]types.BatchStatementRequest, len(movies)) for index, movie := range movies { params, err := attributevalue.MarshalList([]interface{}{movie.Title, movie.Year}) if err != nil { panic(err) } statementRequests[index] = types.BatchStatementRequest{ Statement: aws.String( fmt.Sprintf("DELETE FROM \"%v\" WHERE title=? AND year=?", runner.TableName)), Parameters: params, } } _, err := runner.DynamoDbClient.BatchExecuteStatement(context.TODO(), &dynamodb.BatchExecuteStatementInput{ Statements: statementRequests, }) if err != nil { log.Printf("Couldn't delete the batch of movies. Here's why: %v\n", err) } return err }

Define a Movie struct that is used in this example.

// 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"]) }
JavaScript
SDK for JavaScript (v3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Create a batch of items using PartiQL.

import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; import { DynamoDBDocumentClient, BatchExecuteStatementCommand, } from "@aws-sdk/lib-dynamodb"; const client = new DynamoDBClient({}); const docClient = DynamoDBDocumentClient.from(client); export const main = async () => { const breakfastFoods = ["Eggs", "Bacon", "Sausage"]; const command = new BatchExecuteStatementCommand({ Statements: breakfastFoods.map((food) => ({ Statement: `INSERT INTO BreakfastFoods value {'Name':?}`, Parameters: [food], })), }); const response = await docClient.send(command); console.log(response); return response; };

Get a batch of items using PartiQL.

import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; import { DynamoDBDocumentClient, BatchExecuteStatementCommand, } from "@aws-sdk/lib-dynamodb"; const client = new DynamoDBClient({}); const docClient = DynamoDBDocumentClient.from(client); export const main = async () => { const command = new BatchExecuteStatementCommand({ Statements: [ { Statement: "SELECT * FROM PepperMeasurements WHERE Unit=?", Parameters: ["Teaspoons"], ConsistentRead: true, }, { Statement: "SELECT * FROM PepperMeasurements WHERE Unit=?", Parameters: ["Grams"], ConsistentRead: true, }, ], }); const response = await docClient.send(command); console.log(response); return response; };

Update a batch of items using PartiQL.

import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; import { DynamoDBDocumentClient, BatchExecuteStatementCommand, } from "@aws-sdk/lib-dynamodb"; const client = new DynamoDBClient({}); const docClient = DynamoDBDocumentClient.from(client); export const main = async () => { const eggUpdates = [ ["duck", "fried"], ["chicken", "omelette"], ]; const command = new BatchExecuteStatementCommand({ Statements: eggUpdates.map((change) => ({ Statement: "UPDATE Eggs SET Style=? where Variety=?", Parameters: [change[1], change[0]], })), }); const response = await docClient.send(command); console.log(response); return response; };

Delete a batch of items using PartiQL.

import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; import { DynamoDBDocumentClient, BatchExecuteStatementCommand, } from "@aws-sdk/lib-dynamodb"; const client = new DynamoDBClient({}); const docClient = DynamoDBDocumentClient.from(client); export const main = async () => { const command = new BatchExecuteStatementCommand({ Statements: [ { Statement: "DELETE FROM Flavors where Name=?", Parameters: ["Grape"], }, { Statement: "DELETE FROM Flavors where Name=?", Parameters: ["Strawberry"], }, ], }); const response = await docClient.send(command); console.log(response); return response; };
PHP
SDK for PHP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

public function getItemByPartiQLBatch(string $tableName, array $keys): Result { $statements = []; foreach ($keys as $key) { list($statement, $parameters) = $this->buildStatementAndParameters("SELECT", $tableName, $key['Item']); $statements[] = [ 'Statement' => "$statement", 'Parameters' => $parameters, ]; } return $this->dynamoDbClient->batchExecuteStatement([ 'Statements' => $statements, ]); } public function insertItemByPartiQLBatch(string $statement, array $parameters) { $this->dynamoDbClient->batchExecuteStatement([ 'Statements' => [ [ 'Statement' => "$statement", 'Parameters' => $parameters, ], ], ]); } public function updateItemByPartiQLBatch(string $statement, array $parameters) { $this->dynamoDbClient->batchExecuteStatement([ 'Statements' => [ [ 'Statement' => "$statement", 'Parameters' => $parameters, ], ], ]); } public function deleteItemByPartiQLBatch(string $statement, array $parameters) { $this->dynamoDbClient->batchExecuteStatement([ 'Statements' => [ [ 'Statement' => "$statement", 'Parameters' => $parameters, ], ], ]); }
Python
SDK for Python (Boto3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

class PartiQLBatchWrapper: """ Encapsulates a DynamoDB resource to run PartiQL statements. """ def __init__(self, dyn_resource): """ :param dyn_resource: A Boto3 DynamoDB resource. """ self.dyn_resource = dyn_resource def run_partiql(self, statements, param_list): """ Runs a PartiQL statement. A Boto3 resource is used even though `execute_statement` is called on the underlying `client` object because the resource transforms input and output from plain old Python objects (POPOs) to the DynamoDB format. If you create the client directly, you must do these transforms yourself. :param statements: The batch of PartiQL statements. :param param_list: The batch of PartiQL parameters that are associated with each statement. This list must be in the same order as the statements. :return: The responses returned from running the statements, if any. """ try: output = self.dyn_resource.meta.client.batch_execute_statement( Statements=[ {"Statement": statement, "Parameters": params} for statement, params in zip(statements, param_list) ] ) except ClientError as err: if err.response["Error"]["Code"] == "ResourceNotFoundException": logger.error( "Couldn't execute batch of PartiQL statements because the table " "does not exist." ) else: logger.error( "Couldn't execute batch of PartiQL statements. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return output
Ruby
SDK for Ruby
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Read a batch of items using PartiQL.

class DynamoDBPartiQLBatch attr_reader :dynamo_resource attr_reader :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

Delete a batch of items using PartiQL.

class DynamoDBPartiQLBatch attr_reader :dynamo_resource attr_reader :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

For a complete list of Amazon SDK developer guides and code examples, see Using DynamoDB with an Amazon SDK. This topic also includes information about getting started and details about previous SDK versions.