如何使用boto3从DynamoDB有线协议手动转换为原生Python对象? [英] How to convert from DynamoDB wire protocol to native Python object manually with boto3?

查看:99
本文介绍了如何使用boto3从DynamoDB有线协议手动转换为原生Python对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由DynamoDB流触发的Lambda。 Lambda进行一些处理,然后在SNS中创建有关主题的通知。理想情况下,我希望将整个新文档包括在发给SNS的通知中,以使下游客户端不必点击DynamoDB即可获取数据。

I have a Lambda that is being triggered by a DynamoDB stream. The Lambda does some processing and then creates a notification on a topic in SNS. Ideally I would like to include the entire new document in the notification that goes out to SNS so that downstream clients don't have to hit DynamoDB to get the data.

我遇到的问题是来自DynamoDB流的数据是DynamoDB有线格式的(映射包括作为关键字的数据类型)。当我向下游客户端发送通知时,我不希望他们必须了解DynamoDB有线格式来解析消息(例如,如果我切换到新的基础数据存储,则必须重新创建该格式)。

The problem I'm running into is that the data coming from the DynamoDB stream is in DynamoDB wire format (the maps include the data type as a key). When I send out the notification to downstream clients I don't want them to have to understand DynamoDB wire format to parse the message (for example if I switch to a new underlying data store I would then have to recreate that format).

显然,boto3客户端能够将这种格式解析为Python对象,我是否有办法自行访问解析器?据我所知,它是从DynamoDB提取数据的一部分而被调用,但是我找不到自己的方法。

Obviously the boto3 client is capable of parsing this format into a Python object, is there a way for me to access the parser on my own? As far as I can tell it gets called as part of fetching data from DynamoDB but I can't find a way to call it on my own.

推荐答案

我也遇到类似的情况,我使用了以下方法:

I have a similar situation and I used the following an approach like this:

from boto3.dynamodb.types import TypeDeserializer

deser = TypeDeserializer()

...
<in handler>
    for record in event['Records']:
        old = record['dynamodb'].get('OldImage')
        new = record['dynamodb'].get('NewImage')
        if old:
            d = {}
            for key in old:
                d[key] = deser.deserialize(old[key])

这种方法对我有用。生成的字典 d 包含转换后的对象,而不是传递给处理程序的有线格式版本。

This approach works for me. The resulting dictionary d contains the converted object rather than the wire-format version passed to the handler.

这篇关于如何使用boto3从DynamoDB有线协议手动转换为原生Python对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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