DynamoDB에서 item을 get하는 방식
- query: partition key를 이용함
- getitem: primary key (partition key + sort key)를 이용함
- 기존에 query를 사용했음 -> table에 5년 * 12달 = 60번의 query를 보내야 해서 api 요청 시간이 오래 걸림
- boto3 library의 batchgetitem을 사용하여 시간 단축 -> primary key 값들을 list에 넣어두어 for문 없이 한번에 item을 get할 수 있음
- batchgetitem 1회 요청 최대 row 개수는 100개
-> 100개 이상의 요청을 보낼 경우 100개씩 나누어 여러 번 보내야함
import boto3
import json
from datetime import datetime
from boto3.dynamodb.conditions import Key, Attr
from decimal import Decimal
from multiprocessing import Manager, Process
dynamodb = boto3.resource("dynamodb", region_name = "ap-northeast-2")
table1 = dynamodb.Table("BUILDING_ENERGY_SIDO")
table2 = dynamodb.Table("BUILDINGENERGY_SIGUNGU")
def lambda_handler(event, context):
Addr = event["params"]["querystring"]["addr"]
Date=''
year= datetime.today().year
month=datetime.today().month
if(month<=3):
month+=9
year-=1
else:
month-=3
if(month<10):
month='0'+str(month)
month=str(month)
current_date=str(year)+str(month)
response = table1.query(
KeyConditionExpression = Key("date").eq(current_date)
)
while(response['Count']==0):
if((int(current_date)-1)%100==0):
current_date= str(int(current_date)-100+11)
else:
current_date = str(int(current_date)-1)
response = table1.query(
KeyConditionExpression = Key("date").eq(current_date)
)
Date=current_date
intYear = (int(Date) // 100) * 100
startyear = ((intYear // 100) - 5) * 100
endyear = ((intYear // 100) - 1) * 100
YEAR = []
for x in range(5):
for j in range(12):
YEAR.append(str(startyear + (j + 1)))
startyear = startyear + 100
Addr_trunc = Addr[:5] + "00000"
items_to_get = [0 for i in range(len(YEAR))]
for i in range(len(YEAR)):
item_dict = {"date":YEAR[i], "addr":Addr_trunc}
items_to_get[i] = item_dict
response = dynamodb.batch_get_item(
RequestItems={
"BUILDINGENERGY_SIGUNGU":{
"Keys":items_to_get
}
}
)
response = response["Responses"]["BUILDINGENERGY_SIGUNGU"]
ans= [0 for i in range(5)]
x = 0
for i in range(0, 60, 12):
ans[x] = {"gas":0,"electricity":0,"gas_co2":0,"electricity_co2":0,"energy_sum":0,"co2_sum":0,"YEAR":YEAR[i][:4]}
x = x + 1
for i in range(len(YEAR)):
yyy = response[i]["date"][:4]
if yyy == YEAR[0][:4]:
ans[0]["gas"] += response[i]["gas"]
ans[0]["electricity"] += response[i]["electricity"]
ans[0]["gas_co2"] += response[i]["gas_co2"]
ans[0]["electricity_co2"] += response[i]["electricity_co2"]
ans[0]["energy_sum"] += response[i]["energy_sum"]
ans[0]["co2_sum"] += response[i]["co2_sum"]
elif yyy == YEAR[12][:4]:
ans[1]["gas"] += response[i]["gas"]
ans[1]["electricity"] += response[i]["electricity"]
ans[1]["gas_co2"] += response[i]["gas_co2"]
ans[1]["electricity_co2"] += response[i]["electricity_co2"]
ans[1]["energy_sum"] += response[i]["energy_sum"]
ans[1]["co2_sum"] += response[i]["co2_sum"]
elif yyy == YEAR[24][:4]:
ans[2]["gas"] += response[i]["gas"]
ans[2]["electricity"] += response[i]["electricity"]
ans[2]["gas_co2"] += response[i]["gas_co2"]
ans[2]["electricity_co2"] += response[i]["electricity_co2"]
ans[2]["energy_sum"] += response[i]["energy_sum"]
ans[2]["co2_sum"] += response[i]["co2_sum"]
elif yyy == YEAR[36][:4]:
ans[3]["gas"] += response[i]["gas"]
ans[3]["electricity"] += response[i]["electricity"]
ans[3]["gas_co2"] += response[i]["gas_co2"]
ans[3]["electricity_co2"] += response[i]["electricity_co2"]
ans[3]["energy_sum"] += response[i]["energy_sum"]
ans[3]["co2_sum"] += response[i]["co2_sum"]
else:
ans[4]["gas"] += response[i]["gas"]
ans[4]["electricity"] += response[i]["electricity"]
ans[4]["gas_co2"] += response[i]["gas_co2"]
ans[4]["electricity_co2"] += response[i]["electricity_co2"]
ans[4]["energy_sum"] += response[i]["energy_sum"]
ans[4]["co2_sum"] += response[i]["co2_sum"]
return ans
'''
ans=[]
sgas = 0; selectricity = 0; sgas_co2 = 0; selectricity_co2 = 0; senergy_sum = 0; sco2_sum = 0 #sum
for i in range(len(YEAR)): #len(YEAR)가 60 -> query 60회 보내야함
Addr_trunc = Addr[:5] + "00000"
response = table2.query(
KeyConditionExpression = Key("date").eq(YEAR[i]) & Key("addr").eq(Addr_trunc)
)
sgas += response['Items'][0]['gas']
selectricity += response['Items'][0]['electricity']
sgas_co2 += response['Items'][0]['gas_co2']
selectricity_co2 += response['Items'][0]['electricity_co2']
senergy_sum += response['Items'][0]['energy_sum']
sco2_sum += response['Items'][0]['co2_sum']
if (i+1) % 12 == 0:
yyy = YEAR[i]
item = {
"gas":int(float(sgas) * 0.095),
"electricity":selectricity,
"gas_co2":sgas_co2,
"electricity_co2":selectricity_co2,
"energy_sum":senergy_sum,
"co2_sum":sco2_sum,
"YEAR":yyy[:4]
}
ans.append(item)
sgas = 0; selectricity = 0; sgas_co2 = 0; selectricity_co2 = 0; senergy_sum = 0; sco2_sum = 0
else:
continue
return ans
'''