DEV Community

Abhishek Gupta for AWS

Posted on • Originally published at community.aws

3 1

[20 Days of DynamoDB] Day 17 - BatchGetItem operation

Posted: 5/Feb/2024

You can club multiple (up to 100) GetItem requests in a single BatchGetItem operation - this can be done across multiple tables.

Here is an example that fetches includes four GetItem calls across two different tables:

    resp, err := client.BatchGetItem(context.Background(), &dynamodb.BatchGetItemInput{
        RequestItems: map[string]types.KeysAndAttributes{
            "customer": types.KeysAndAttributes{
                Keys: []map[string]types.AttributeValue{
                    {
                        "email": &types.AttributeValueMemberS{Value: "c1@foo.com"},
                    },
                    {
                        "email": &types.AttributeValueMemberS{Value: "c2@foo.com"},
                    },
                },
            },
            "Thread": types.KeysAndAttributes{
                Keys: []map[string]types.AttributeValue{
                    {
                        "ForumName": &types.AttributeValueMemberS{Value: "Amazon DynamoDB"},
                        "Subject":   &types.AttributeValueMemberS{Value: "DynamoDB Thread 1"},
                    },
                    {
                        "ForumName": &types.AttributeValueMemberS{Value: "Amazon S3"},
                        "Subject":   &types.AttributeValueMemberS{Value: "S3 Thread 1"},
                    },
                },
                ProjectionExpression: aws.String("Message"),
            },
        },
        ReturnConsumedCapacity: types.ReturnConsumedCapacityTotal,
    })
Enter fullscreen mode Exit fullscreen mode

Just like an individual GetItem call, you can include Projection Expressions and return RCUs. Note that BatchGetItem can only retrieve up to 16 MB of data.

Recommended reading: BatchGetItem API doc

API Trace View

How I Cut 22.3 Seconds Off an API Call with Sentry 👀

Struggling with slow API calls? Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay