簡體   English   中英

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

[英]How to delete all the items in the dynamodb with boto3

我的代碼如下是從表名稱details中刪除內容

  • 下面的代碼將根據工作正常的 dynamodb 的Capacity刪除一些項目
  • 如何刪除所有項目
    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)

我寫了帶有 Flag 條件的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)

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

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

下面是使用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)

對於 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM