如何使用DICT数据类型(boto3)更新DynamoDB表 [英] How to update DynamoDB table with DICT data type (boto3)

查看:122
本文介绍了如何使用DICT数据类型(boto3)更新DynamoDB表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个DynamoDB表,该表用于存储元数据,该表具有用于不同类型数据的不同属性(文件大小,日期等,作为单独的属性)。



<我正在尝试使用Python3字典并将其用作一堆需要上载到表的属性的源。到目前为止,我已经能够成功地将整个字典作为一个属性上载,但是我希望每个key:value对在数据库中都是其自己的属性。换句话说,Python3词典中的每个key:value对都应作为DynamoDB表中自己的key:value对结束。我知道下面的代码到目前为止还没有设计。



这是到目前为止的代码:

  file_attributes = dict(file.items())

#将所有元数据添加到DynamoDB:
响应=表.update_item(
UpdateExpression = set File_attributes =:f,
Key = {
'file_name':key,
},
ExpressionAttributeValues = {
':f':file_attributes,

},
ReturnValues = UPDATED_NEW


解决方案

我认为您正在寻找符合以下条件的东西:

  update_expression ='SET {}'。format(','。join(f'#{k} =:{k}'对于文件中的k)))
expression_attribute_values = {f': {k}':v表示k,v表示file.items()}
expression_attribute_names = {f'#{k}':k表示文件中的k}

响应=表。 update_item(
Key = {
'file_name':key,
},
UpdateExpression = update_expression,
ExpressionAttributeValues = expression_attribute_values,
ExpressionAttributeNames = expression_attribute_names,
ReturnValues ='UPDATED_NEW',

我修改了示例,以同时使用ExpressionAttributeValues和ExpressionAttributeNames动态生成更新表达式。必须使用名称别名,因为我们不知道 file 词典中项目的名称是什么。生成的更新表达式将包含 file 词典中每个顶级关键字的set语句。属性名称的前缀为,属性值的前缀为


I created a DynamoDB table that is used for storing metadata, with different attributes for different types of data (file size, date, etc., as separate attributes).

I am trying to take a Python3 dictionary and use it as the source for a bunch of attributes that need to be uploaded to the table. So far, I've been able to successfully upload the entire dictionary as one attribute, but I want each key:value pair to be its own attribute in the database. In other words, each key:value pair from the Python3 dictionary should end up as its own key:value pair in the DynamoDB table. I'm aware my code below is not designed to that so far.

Here is my code so far:

file_attributes = dict(file.items())

    # Add all metadata to DynamoDB:
    response = table.update_item(
        UpdateExpression="set File_attributes = :f,"
        Key={
            'file_name': key,
        },
        ExpressionAttributeValues={
            ':f': file_attributes,

        },
        ReturnValues="UPDATED_NEW"
    )

解决方案

I think you are looking for something along these lines:

update_expression = 'SET {}'.format(','.join(f'#{k}=:{k}' for k in file))
expression_attribute_values = {f':{k}': v for k, v in file.items()}
expression_attribute_names = {f'#{k}': k for k in file}

response = table.update_item(
    Key={
        'file_name': key,
    },
    UpdateExpression=update_expression,
    ExpressionAttributeValues=expression_attribute_values,
    ExpressionAttributeNames=expression_attribute_names,
    ReturnValues='UPDATED_NEW',
)

I modified your example to generate update expression dynamically using both ExpressionAttributeValues and ExpressionAttributeNames. Usage of name aliases was required, because we do not know what are the names of the items inside file dictionary. Generated update expression will contain set statement for each top-level key inside file dictionary. Attribute names are prefixed with # sign, and attribute values are prefixed with : sign.

这篇关于如何使用DICT数据类型(boto3)更新DynamoDB表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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