第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何使用boto3刪除dynamodb中的所有項(xiàng)目

如何使用boto3刪除dynamodb中的所有項(xiàng)目

汪汪一只貓 2023-10-06 16:33:29
我的代碼如下是從表名中刪除內(nèi)容details下面的代碼將根據(jù)Capacitydynamodb 刪除一些項(xiàng)目,該項(xiàng)目工作正常如何刪除所有項(xiàng)目    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我用帶有標(biāo)志條件的循環(huán)編寫。   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貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超5個贊

對于 DynamoDB,如果您想刪除所有項(xiàng)目,最好的方法是刪除并重新創(chuàng)建表,因?yàn)槭褂?boto3,每頁的元素?cái)?shù)限制為 1000 個。


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


順便一提...


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)


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

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個贊

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

查看代碼,它只掃描并刪除一次項(xiàng)目。

LastEvaluatedKey這是使用密鑰來確定是否需要重新掃描的工作代碼。當(dāng)掃描達(dá)到最大數(shù)據(jù)集大小限制 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)


查看完整回答
反對 回復(fù) 2023-10-06
  • 2 回答
  • 0 關(guān)注
  • 178 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號