使用新属性更新现有 DDB

问题描述 投票:0回答:1

我的 DDB 表包含“uploadedTimeStamp”,现在我想向现有表中的所有现有项目添加另一个名为“trimmed_timestamp”的属性。 有近 7k 条记录,但下面的 boto3 脚本仅更新了 2k 项。 我有什么遗漏的吗?如何确保我的所有项目均已更新?

或者我应该使用 batch_execute_statement 方法来插入属性?如果我需要以 0 停机时间更新生产 DDB,哪种方法会更好?

import boto3
from datetime import datetime

# Initialize DynamoDB client

dynamodb = boto3.resource('dynamodb')
table_name = 'test-table'
table = dynamodb.Table(table_name)

# Scan the table and update items

response = table.scan()
for item in response['Items']:
    # Get the timestamp attribute
    timestamp = item['uploadedTimeStamp']
    # Trim the timestamp
    trimmed_timestamp = timestamp[:10] # extract dd-mm-yyyy

    # Update the item with the new attribute
    update_expression = 'SET trimmed_timestamp = :value'
    expression_attribute_values = {':value': trimmed_timestamp}
    table.update_item(
        Key={'consumerId': item['consumerId']},
        UpdateExpression=update_expression,
        ExpressionAttributeValues=expression_attribute_values
    )
amazon-web-services amazon-dynamodb boto3
1个回答
0
投票

如果您从 DynamoDB 获取超过 1MB 的数据,则必须实施 paginateion

这里是如何在boto3中实现它的示例。

© www.soinside.com 2019 - 2024. All rights reserved.