amazon-web-services - 亚马逊云数据库 : Unmarshalliing BatchGetItem response

标签 amazon-web-services go amazon-dynamodb aws-sdk-go

我正在使用 GO SDK 并使用 DynamnoDB BatchGetItem API。

我看到了这个代码示例 -

https://github.com/aws/aws-sdk-go/blob/master/service/dynamodb/examples_test.go

是否有任何其他代码示例显示来自 BatchGetItem API 的响应的解码?

最佳答案

让我分享一段代码。理解它的关键是,当您向 dynamodb 发送 GetBatchItem 请求时,您指定了表名和该表的键的映射,因此您得到的响应是表名和匹配项的映射

placeIDs := []string { "london_123", "sanfran_15", "moscow_9" }

type Place {
    ID string `json:"id"`
    Name string `json:"name"`
    Description string `json:"description"`
}

mapOfAttrKeys := []map[string]*dynamodb.AttributeValue{}

for _, place := range placeIDs {
    mapOfAttrKeys = append(mapOfAttrKeys, map[string]*dynamodb.AttributeValue{
        "id": &dynamodb.AttributeValue{
            S: aws.String(place),
        },
        "attr": &dynamodb.AttributeValue{
            S: aws.String("place"),
        },
    })
}

input := &dynamodb.BatchGetItemInput{
    RequestItems: map[string]*dynamodb.KeysAndAttributes{
        tableName: &dynamodb.KeysAndAttributes{
            Keys: mapOfAttrKeys,
        },
    },
}

batch, err := db.BatchGetItem(input)

if err != nil {
    panic(fmt.Errorf("batch load of places failed, err: %w", err))
}

for _, table := range batch.Responses {
    for _, item := range table {
        var place Place

        err = dynamodbattribute.UnmarshalMap(item, &place)

        if err != nil {
            panic(fmt.Errorf("failed to unmarshall place from dynamodb response, err: %w", err))
        }

        places = append(places, place)
    }
}

关于amazon-web-services - 亚马逊云数据库 : Unmarshalliing BatchGetItem response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56889918/

相关文章:

go - 如何增加 golang.org/x/crypto/ssh 的详细程度

google-app-engine - Google App Engine 高性能代理解决方法

ubuntu - 无法在 Linux 上使用 alexbrainman/odbc 连接到 Go 中的第 3 方 ODBC 驱动程序

json - 有没有办法从 CloudFormation 模板填充 Amazon DynamoDB 表?

node.js - DynamoDB ConditionalExpression 无法识别属性名称或值

python - 对于 UpdateExpression 语法,在 DynamoDB 中放置/更新项目失败

amazon-web-services - AWS CloudFormation 是否支持 ECS 任务放置策略?

java - 如何提高 DynamoDB 请求的速度

python - 在 AWS (lambda) 上安装 python-Levenshtein 以加速 fuzzywuzzy

amazon-web-services - 在 AWS 控制台上对嵌套属性使用 DynamoDB 过滤器