亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用boto3刪除dynamodb中的所有項目

如何使用boto3刪除dynamodb中的所有項目

汪汪一只貓 2023-10-06 16:33:29
我的代碼如下是從表名中刪除內容details下面的代碼將根據Capacitydynamodb 刪除一些項目,該項目工作正常如何刪除所有項目    import boto3    def lambda_handler(event, context):        try:            table_name = 'details'            dynamodb = boto3.resource('dynamodb')            table = dynamodb.Table(table_name)            scan = table.scan()            with table.batch_writer() as batch:                for each in scan['Items']:                    batch.delete_item(                        Key={                            'id': each['id']                        }                    )        except Exception as e:           print (e)while我用帶有標志條件的循環編寫。   import boto3    def lambda_handler(event, context):        try:            flag = False            table_name = 'details'            dynamodb = boto3.resource('dynamodb')            table = dynamodb.Table(table_name)            scan = table.scan()            while True:                with table.batch_writer() as batch:                    for each in scan['Items']:                        if each is not None:                            batch.delete_item(                                 Key={                                 'id': each['id']                                 }                             )                         else:                            Flag = True        except Exception as e:           print (e)
查看完整描述

2 回答

?
翻閱古今

TA貢獻1780條經驗 獲得超5個贊

對于 DynamoDB,如果您想刪除所有項目,最好的方法是刪除并重新創建表,因為使用 boto3,每頁的元素數限制為 1000 個。


使用 boto3 執行此操作的問題是昂貴的成本......每次刪除都是一個寫入請求。如果您不想支付不必要的費用(這是最好的方法),請刪除并重新創建:)


順便一提...


import boto3

    def lambda_handler(event, context):

        try:

            flag = False

            table_name = 'details'

            dynamodb = boto3.resource('dynamodb')

            table = dynamodb.Table(table_name)

            scan = table.scan()

            while !flag:

                with table.batch_writer() as batch:

                    for each in scan['Items']:

                        batch.delete_item(

                                 Key={

                                 'id': each['id']

                                 }

                             )

                    flag = True

        except Exception as e:

           print (e)


查看完整回答
反對 回復 2023-10-06
?
互換的青春

TA貢獻1797條經驗 獲得超6個贊

由于編輯隊列已滿,我無法編輯已接受的答案。

查看代碼,它只掃描并刪除一次項目。

LastEvaluatedKey這是使用密鑰來確定是否需要重新掃描的工作代碼。當掃描達到最大數據集大小限制 1 MB時,該鍵存在。

import boto3


def lambda_handler(event, context):

? ? ?try:

? ? ? ? ? table_name = 'details'

? ? ? ? ? dynamodb = boto3.resource('dynamodb')

? ? ? ? ? table = dynamodb.Table(table_name)


? ? ? ? ? flag = True

? ? ? ? ? while flag:

? ? ? ? ? ? ? ?scan = table.scan()

? ? ? ? ? ? ? ?print(f"Deleting {scan['ScannedCount']} records...")

? ? ? ? ? ? ? ?flag = 'LastEvaluatedKey' in scan and scan['LastEvaluatedKey']

? ? ? ? ? ? ? ?with table.batch_writer() as batch:

? ? ? ? ? ? ? ? ? ? for each in scan['Items']:

? ? ? ? ? ? ? ? ? ? ? ? ?batch.delete_item(

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Key={

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'id': each['id']

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? ?)

? ? ?except Exception as e:

? ? ? ? ? print(e)


查看完整回答
反對 回復 2023-10-06
  • 2 回答
  • 0 關注
  • 169 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號