<legend id='afEQ5'><style id='afEQ5'><dir id='afEQ5'><q id='afEQ5'></q></dir></style></legend>
    <tfoot id='afEQ5'></tfoot>
    <i id='afEQ5'><tr id='afEQ5'><dt id='afEQ5'><q id='afEQ5'><span id='afEQ5'><b id='afEQ5'><form id='afEQ5'><ins id='afEQ5'></ins><ul id='afEQ5'></ul><sub id='afEQ5'></sub></form><legend id='afEQ5'></legend><bdo id='afEQ5'><pre id='afEQ5'><center id='afEQ5'></center></pre></bdo></b><th id='afEQ5'></th></span></q></dt></tr></i><div id='afEQ5'><tfoot id='afEQ5'></tfoot><dl id='afEQ5'><fieldset id='afEQ5'></fieldset></dl></div>
    • <bdo id='afEQ5'></bdo><ul id='afEQ5'></ul>

    <small id='afEQ5'></small><noframes id='afEQ5'>

    1. 如何将 boto3 Dynamo DB 项转换为 Python 中的常规字典?

      时间:2023-07-05
      <tfoot id='RmGPK'></tfoot>
        <tbody id='RmGPK'></tbody>
        <legend id='RmGPK'><style id='RmGPK'><dir id='RmGPK'><q id='RmGPK'></q></dir></style></legend>
            <bdo id='RmGPK'></bdo><ul id='RmGPK'></ul>
            <i id='RmGPK'><tr id='RmGPK'><dt id='RmGPK'><q id='RmGPK'><span id='RmGPK'><b id='RmGPK'><form id='RmGPK'><ins id='RmGPK'></ins><ul id='RmGPK'></ul><sub id='RmGPK'></sub></form><legend id='RmGPK'></legend><bdo id='RmGPK'><pre id='RmGPK'><center id='RmGPK'></center></pre></bdo></b><th id='RmGPK'></th></span></q></dt></tr></i><div id='RmGPK'><tfoot id='RmGPK'></tfoot><dl id='RmGPK'><fieldset id='RmGPK'></fieldset></dl></div>

              <small id='RmGPK'></small><noframes id='RmGPK'>

              • 本文介绍了如何将 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 中的常规字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Amazon 的 Alexa Skill Kit (ASK) Lambda 交互故障排除 下一篇:如何检查 DynamoDB 表是否存在?

                相关文章

              • <i id='oZWcg'><tr id='oZWcg'><dt id='oZWcg'><q id='oZWcg'><span id='oZWcg'><b id='oZWcg'><form id='oZWcg'><ins id='oZWcg'></ins><ul id='oZWcg'></ul><sub id='oZWcg'></sub></form><legend id='oZWcg'></legend><bdo id='oZWcg'><pre id='oZWcg'><center id='oZWcg'></center></pre></bdo></b><th id='oZWcg'></th></span></q></dt></tr></i><div id='oZWcg'><tfoot id='oZWcg'></tfoot><dl id='oZWcg'><fieldset id='oZWcg'></fieldset></dl></div>

                    <legend id='oZWcg'><style id='oZWcg'><dir id='oZWcg'><q id='oZWcg'></q></dir></style></legend>
                      <bdo id='oZWcg'></bdo><ul id='oZWcg'></ul>
                  1. <small id='oZWcg'></small><noframes id='oZWcg'>

                  2. <tfoot id='oZWcg'></tfoot>