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

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

问题描述

在 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 有两种基本的操作模式:一种使用低级 Client 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.

但是,有时无法专门使用这些高级构造.在处理附加到 Lambda 的 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 类并将它们与字典推导组合,如下所示:

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

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

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