-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
136 lines (127 loc) · 4.29 KB
/
index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import decimal
import json
import boto3
from boto3.dynamodb.conditions import Key
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table("backupSymbolInfo-maptool")
# テーブルスキャン
def operation_scan():
try:
scanData = table.scan()
items=scanData['Items']
print(items)
return scanData
except Exception as e:
print("Error Exception.")
print(e)
# リスト検索
def operation_query_list(partitionKey):
try:
queryData = table.query(
KeyConditionExpression = Key("backupTitle").eq(partitionKey)
)
items=queryData['Items']
print(items)
return queryData
except Exception as e:
print("Error Exception.")
print(e)
# レコード検索
def operation_query(partitionKey, sortKey):
try:
queryData = table.query(
KeyConditionExpression = Key("backupTitle").eq(partitionKey) & Key("id").eq(sortKey)
)
items=queryData['Items']
print(items)
return queryData
except Exception as e:
print("Error Exception.")
print(e)
# レコード追加・更新
def operation_put(items):
try:
with table.batch_writer() as batch:
for item in items:
# カラム名 Typo の吸収
if 'longitude' in item:
longitude = item['longitude']
else:
longitude = item['longtitude']
baseItem={
'backupTitle': item['backupTitle'],
'id': item['id'],
'title': item['title'],
'describe': item['describe'],
'dateTime': item['dateTime'],
'latitude': item['latitude'],
'longitude': longitude,
'prefecture': item['prefecture'],
'municipalities': item['municipalities']
}
convItem = json.loads(json.dumps(baseItem), parse_float=decimal.Decimal)
batch.put_item(
Item=convItem
)
print('PUT Successed.')
return 'PUT Successed.'
except Exception as e:
print("Error Exception.")
print(e)
# レコード削除
def operation_delete(partitionKey, sortKey):
try:
delResponse = table.delete_item(
Key={
'backupTitle': partitionKey,
'id': sortKey
}
)
if delResponse['ResponseMetadata']['HTTPStatusCode'] != 200:
print(delResponse)
else:
print('DEL Successed.')
return delResponse
except Exception as e:
print("Error Exception.")
print(e)
# レコード一括削除(同一パーティションキー)
def operation_delete_list(partitionKey):
try:
queryData = table.query(
KeyConditionExpression = Key("backupTitle").eq(partitionKey)
)
items=queryData['Items']
print(items)
for item in items:
operation_delete(partitionKey=partitionKey, sortKey=item['id'])
return queryData
except Exception as e:
print("Error Exception.")
print(e)
def lambda_handler(event, context):
print("Received event: " + json.dumps(event))
OperationType = event['OperationType']
try:
if OperationType == 'SCAN':
return operation_scan()
if OperationType == 'LIST':
PartitionKey = event['Keys']['backupTitle']
return operation_query_list(PartitionKey)
if OperationType == 'QUERY':
PartitionKey = event['Keys']['backupTitle']
SortKey = event['Keys']['id']
return operation_query(PartitionKey, SortKey)
if OperationType == 'PUT':
items = event['Keys']['items']
return operation_put(items)
if OperationType == 'DELETE':
PartitionKey = event['Keys']['backupTitle']
SortKey = event['Keys']['id']
return operation_delete(PartitionKey, SortKey)
if OperationType == 'DELETE_LIST':
PartitionKey = event['Keys']['backupTitle']
return operation_delete_list(PartitionKey)
except Exception as e:
print("Error Exception.")
print(e)