gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 21:12:04 26 4
gpt4 key购买 nike

我正在使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com