Go语言 AWS DynamoDB:正在解组BatchGetItem响应

x759pob2  于 2023-10-14  发布在  Go
关注(0)|答案(2)|浏览(147)

我正在使用GO SDK和DynamnoDB BatchGetItem API。
我看到了这个代码示例-
https://github.com/aws/aws-sdk-go/blob/master/service/dynamodb/examples_test.go
是否有其他代码示例显示来自BatchGetItem API的响应的解组?

8ftvxx2r

8ftvxx2r1#

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

  1. placeIDs := []string { "london_123", "sanfran_15", "moscow_9" }
  2. type Place {
  3. ID string `json:"id"`
  4. Name string `json:"name"`
  5. Description string `json:"description"`
  6. }
  7. mapOfAttrKeys := []map[string]*dynamodb.AttributeValue{}
  8. for _, place := range placeIDs {
  9. mapOfAttrKeys = append(mapOfAttrKeys, map[string]*dynamodb.AttributeValue{
  10. "id": &dynamodb.AttributeValue{
  11. S: aws.String(place),
  12. },
  13. "attr": &dynamodb.AttributeValue{
  14. S: aws.String("place"),
  15. },
  16. })
  17. }
  18. input := &dynamodb.BatchGetItemInput{
  19. RequestItems: map[string]*dynamodb.KeysAndAttributes{
  20. tableName: &dynamodb.KeysAndAttributes{
  21. Keys: mapOfAttrKeys,
  22. },
  23. },
  24. }
  25. batch, err := db.BatchGetItem(input)
  26. if err != nil {
  27. panic(fmt.Errorf("batch load of places failed, err: %w", err))
  28. }
  29. for _, table := range batch.Responses {
  30. for _, item := range table {
  31. var place Place
  32. err = dynamodbattribute.UnmarshalMap(item, &place)
  33. if err != nil {
  34. panic(fmt.Errorf("failed to unmarshall place from dynamodb response, err: %w", err))
  35. }
  36. places = append(places, place)
  37. }
  38. }
展开查看全部
pes8fvy9

pes8fvy92#

这些都是很老的问题和答案,但它们非常有用,感谢TP @dattatray和@itmeze
尽管如此,今天亚马逊建议使用新的实现,AWS SDK for Go V2,其中相同的东西可以以更简洁和更具表现力的方式实现。

  1. import (
  2. "context"
  3. "fmt"
  4. "github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
  5. "github.com/aws/aws-sdk-go-v2/service/dynamodb"
  6. "github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
  7. )
  8. type Place struct {
  9. ID string `dynamodbav:"id" json:"id"`
  10. Name string `dynamodbav:"name" json:"name"`
  11. Description string `dynamodbav:"description" json:"description"`
  12. }
  13. // define your context and DynamoDB client
  14. var ctx context.Context
  15. var db dynamodb.Client
  16. placeIDs := []string { "london_123", "sanfran_15", "moscow_9" }
  17. tableName := "Places"
  18. //marshall and collect into a slice the keys to be queried
  19. var requests types.KeysAndAttributes
  20. for _, place := range placeIDs {
  21. key := struct {
  22. Name string `dynamodbav:"name" json:"name"`
  23. } {Name: place}
  24. item, err := attributevalue.MarshalMap(key)
  25. if err != nil {
  26. panic(fmt.Errorf("failed to marshal key %s, err: %w", place, err))
  27. }
  28. requests.Keys = append(requests.Keys, item)
  29. }
  30. //prepare and issue `BatchGetItem` request
  31. batch := make(map[string]types.KeysAndAttributes)
  32. batch[tableName] = requests
  33. op, err := db.BatchGetItem(ctx, &dynamodb.BatchGetItemInput{
  34. RequestItems: batch,
  35. })
  36. if err != nil {
  37. panic(fmt.Errorf("couldn't send a batch of items to dynamo DB %w", err))
  38. }
  39. //here we collect the keys for which we didn't get results
  40. //basically they can be later passed to retry BatchGetItem call
  41. var missingKeys []map[string]types.AttributeValue
  42. if missing, ok := op.UnprocessedKeys[tableName]; ok {
  43. copy(missingKeys, missing.Keys)
  44. }
  45. //collecting the results into the slice
  46. var resultItems []Place
  47. for _, items := range op.Responses {
  48. var unmarshaledItems []Place
  49. if err := attributevalue.UnmarshalListOfMaps(items, unmarshaledItems); err != nil {
  50. panic(fmt.Errorf("failed to unmarshal item, err: %w", err))
  51. }
  52. resultItems = append(resultItems, unmarshaledItems...)
  53. }
展开查看全部

相关问题