Delete item in DynamoDB

ewillwin·2022년 12월 26일
0

TSMtech Record

목록 보기
19/39
  • python boto3 library를 이용함
  • boto3에선 dynamodb의 partition key와 sort key를 모두 이용해야 item을 delete 할 수 있음
  • 현재 dynamodb에 삽입된 202205의 행정동 addr를 모두 읽고, 그 addr(sort key)와, 202205(partitionkey)를 이용하여 delete
#-*-coding:utf-8 -*-
from urllib.request import urlopen
import requests as rq
from bs4 import BeautifulSoup as bs
import time
from datetime import datetime
import zipfile
import os
import boto3
from typing import final
from venv import create
import pandas as pd
import chunk
import warnings
from multiprocessing import Process
import bisect
import pandas as pd
from typing import final
from venv import create
import requests
from decimal import Decimal
import json
import shutil
import boto3
import json
from boto3.dynamodb.conditions import Key, Attr
from decimal import Decimal

warnings.simplefilter(action='ignore', category=FutureWarning)
dynamodb = boto3.resource('dynamodb', region_name='ap-northeast-2', aws_access_key_id='', aws_secret_access_key='')

table0 = dynamodb.Table('BUILDINGENERGY')
table1 = dynamodb.Table('BUILDING_ENERGY_SIDO')
table2 = dynamodb.Table('BUILDINGENERGY_SIGUNGU')

may = table0.query(
    KeyConditionExpression = Key("date").eq("202205")
)

maylist = may["Items"] # dictionary로 이루어진 list
length = len(maylist)
addrlist = [0 for i in range(length)]

for i in range(length):
    addrlist[i] = maylist[i]["addr"]
#print(addrlist)

items_to_delete = [0 for i in range(length)]
for i in range(length):
    item = {"date":"202205", "addr":addrlist[i]}
    items_to_delete[i] = item
print(items_to_delete)

with table0.batch_writer() as batch:
    for item in items_to_delete:
        response = batch.delete_item(
            Key = {
                "date": item["date"],
                "addr": item["addr"]
            }
        )
profile
Software Engineer @ LG Electronics

0개의 댓글