如何在Python中将boto3 Dynamo DB项目转换为常规词典? [英] How to convert a boto3 Dynamo DB item to a regular dictionary in Python?

查看:87
本文介绍了如何在Python中将boto3 Dynamo DB项目转换为常规词典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,当使用boto3从Dynamo DB中检索项目时,将获得如下所示的模式.

In Python, when an item is retrieved from Dynamo DB using boto3, a schema like the following is obtained.

{
  "ACTIVE": {
    "BOOL": true
  },
  "CRC": {
    "N": "-1600155180"
  },
  "ID": {
    "S": "bewfv43843b"
  },
  "params": {
    "M": {
      "customer": {
        "S": "TEST"
      },
      "index": {
        "N": "1"
      }
    }
  },
  "THIS_STATUS": {
    "N": "10"
  },
  "TYPE": {
    "N": "22"
  }
}

此外,在插入或扫描时,还必须以这种方式转换字典.我一直找不到能够处理这种转换的包装器.由于boto3显然不支持此功能,是否有比实现代码更好的替代方法?

Also when inserting or scanning, dictionaries have to be converted in this fashion. I haven't been able to find a wrapper that takes care of such conversion. Since apparently boto3 does not support this, are there better alternatives than implementing code for it?

推荐答案

为了了解如何解决此问题,重要的是要认识到boto3具有两种基本的操作模式:一种使用低级客户端 API,并且该API使用更高级别的抽象,例如.问题中显示的数据结构是低级API消耗/产生了什么的示例,AWS CLI和dynamodb Web服务也使用了低级API.

In order to understand how to solve this, it's important to recognize that boto3 has two basic modes of operation: one that uses the low-level Client API, and one that uses higher level abstractions like Table. The data structure shown in the question is an example of what is consumed/produced by the low-level API, which is also used by the AWS CLI and the dynamodb web services.

要回答您的问题-如果在使用boto3时可以只使用 Table 之类的高级抽象,那么注释将为您带来很多便利.然后,您可以回避整个问题-为您处理python类型与底层数据格式之间的相互封送.

To answer your question - if you can work exclusively with the high-level abstractions like Table when using boto3 then things will be quite a bit easier for you, as the comments suggest. Then you can sidestep the whole problem - python types are marshaled to and from the low-level data format for you.

但是,有时无法单独使用这些高级构造.在处理附加到Lambdas的DynamoDB流时,我特别遇到了此问题. lambda的输入始终为低级格式,而该格式更难与IMO一起使用.

However, there are some times when it's not possible to use those high-level constructs exclusively. I specifically ran into this problem when dealing with DynamoDB streams attached to Lambdas. The inputs to the lambda are always in the low-level format, and that format is harder to work with IMO.

经过一番挖掘,我发现boto3本身具有一些漂亮的功能,可用于进行转换.这些功能在前面提到的所有内部转换中都隐式使用.要直接使用它们,请导入TypeDeserializer/TypeSerializer类,并将其与dict理解相结合,如下所示:

After some digging I found that boto3 itself has some nifty features tucked away for doing conversions. These features are used implicitly in all of the internal conversions mentioned previously. To use them directly, import the TypeDeserializer/TypeSerializer classes and combine them with dict comprehensions like so:

import boto3

low_level_data = {
  "ACTIVE": {
    "BOOL": True
  },
  "CRC": {
    "N": "-1600155180"
  },
  "ID": {
    "S": "bewfv43843b"
  },
  "params": {
    "M": {
      "customer": {
        "S": "TEST"
      },
      "index": {
        "N": "1"
      }
    }
  },
  "THIS_STATUS": {
    "N": "10"
  },
  "TYPE": {
    "N": "22"
  }
}

# Lazy-eval the dynamodb attribute (boto3 is dynamic!)
boto3.resource('dynamodb')

# To go from low-level format to python
deserializer = boto3.dynamodb.types.TypeDeserializer()
python_data = {k: deserializer.deserialize(v) for k,v in low_level_data.items()}

# To go from python to low-level format
serializer = boto3.dynamodb.types.TypeSerializer()
low_level_copy = {k: serializer.serialize(v) for k,v in python_data.items()}

assert low_level_data == low_level_copy

这篇关于如何在Python中将boto3 Dynamo DB项目转换为常规词典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆