AWS SDK for .NET Documentation
BatchGetItem Method (batchGetItemRequest)
Amazon ► Amazon.DynamoDB ► AmazonDynamoDBClient ► BatchGetItem(BatchGetItemRequest)
Did this page help you? Yes No Tell us about it...

Retrieves the attributes for multiple items from multiple tables using their primary keys.

The maximum number of item attributes that can be retrieved for a single operation is 100. Also, the number of items retrieved is constrained by a 1 MB the size limit. If the response size limit is exceeded or a partial result is returned due to an internal processing failure, Amazon DynamoDB returns an UnprocessedKeys value so you can retry the operation starting with the next item to get.

Amazon DynamoDB automatically adjusts the number of items returned per page to enforce this limit. For example, even if you ask to retrieve 100 items, but each individual item is 50k in size, the system returns 20 items and an appropriate UnprocessedKeys value so you can get the next page of results. If necessary, your application needs its own logic to assemble the pages of results into one set.

Declaration Syntax

C#

    public BatchGetItemResponse BatchGetItem(
    	BatchGetItemRequest batchGetItemRequest
    )

Parameters

batchGetItemRequest (BatchGetItemRequest)
  Container for the necessary parameters to execute the BatchGetItem service method on AmazonDynamoDB.

Return Value
The response from the BatchGetItem service method, as returned by AmazonDynamoDB.
Examples

The following examples show how to batch get item from two tables.

This example will construct the collection of keys for the items to retrieve from the first table.

CopyBatchGet sample - First table

    // Construct get-request for first table
    KeysAndAttributes sampleTableItems = new KeysAndAttributes();
    sampleTableItems.WithAttributesToGet("Author", "Title", "Year");
    sampleTableItems.WithKeys(
        new Key()
            .WithHashKeyElement(new AttributeValue().WithS("Mark Twain"))
            .WithRangeKeyElement(new AttributeValue().WithS("The Adventures of Tom Sawyer")),
        new Key()
            .WithHashKeyElement(new AttributeValue().WithS("Mark Twain"))
            .WithRangeKeyElement(new AttributeValue().WithS("Adventures of Huckleberry Finn"))
    );

This example will construct the collection of keys for the items to retrieve from the second table.

CopyBatchGet sample - Second table

    // Construct get-request for second table
    KeysAndAttributes authorsTableItems = new KeysAndAttributes();
    // Skip setting AttributesToGet property to retrieve all attributes
    sampleTableItems.WithKeys(
        // AuthorsTable only has a hash-key, no range-key
        new Key().WithHashKeyElement(new AttributeValue().WithS("Mark Twain")),
        new Key().WithHashKeyElement(new AttributeValue().WithS("Booker Taliaferro Washington"))
    );

This example will construct the BatchGet request from the two earlier-created collections, will issue the call and in case some items are not processed, will attempt to retrieve the remaining items.

CopyBatchGet sample - Service calls

    // Create a client
    AmazonDynamoDBClient client = new AmazonDynamoDBClient();

    // Construct table-keys mapping
    Dictionary<string, KeysAndAttributes> requestItems = new Dictionary<string, KeysAndAttributes>();
    requestItems["SampleTable"] = sampleTableItems;
    requestItems["AuthorsTable"] = authorsTableItems;

    // Construct request
    BatchGetItemRequest request = new BatchGetItemRequest
    {
        RequestItems = requestItems
    };

    BatchGetItemResult result;
    do
    {
        // Issue request and retrieve items
        result = client.BatchGetItem(request).BatchGetItemResult;

        // Iterate through responses
        Dictionary<string, BatchResponse> responses = result.Responses;
        foreach (string tableName in responses.Keys)
        {
            // Get items for each table
            BatchResponse tableItems = responses[tableName];

            // View items
            List<Dictionary<string, AttributeValue>> items = tableItems.Items;
            foreach (Dictionary<string, AttributeValue> item in items)
            {
                Console.WriteLine("Item:");
                foreach (var keyValuePair in item)
                {
                    Console.WriteLine("{0} : S={1}, N={2}, SS=[{3}], NS=[{4}]",
                        keyValuePair.Key,
                        keyValuePair.Value.S,
                        keyValuePair.Value.N,
                        string.Join(", ", keyValuePair.Value.SS.ToArray()),
                        string.Join(", ", keyValuePair.Value.SS.ToArray()));
                }
            }
        }

        // Some items may not have been retrieved!
        //  Set RequestItems to the result's UnprocessedKeys and reissue request
        request.RequestItems = result.UnprocessedKeys;

    } while (result.UnprocessedKeys.Count > 0);

Exceptions

Exception | Condition
--- | ---
ProvisionedThroughputExceededException |
InternalServerErrorException |
ResourceNotFoundException |

Assembly: AWSSDK (Module: AWSSDK) Version: 1.5.60.0 (1.5.60.0)