-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlambda_function.py
More file actions
202 lines (180 loc) · 8.3 KB
/
lambda_function.py
File metadata and controls
202 lines (180 loc) · 8.3 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import boto3
import os
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
glue = boto3.client(service_name='glue', region_name='us-east-1',
endpoint_url='https://glue.us-east-1.amazonaws.com')
def create_partition_list(db_name, table_name, parts):
response = glue.get_table(CatalogId=os.environ['catalogId'], DatabaseName=db_name, Name=table_name)
# Parsing table info required to create partitions from table
input_format = response['Table']['StorageDescriptor']['InputFormat']
output_format = response['Table']['StorageDescriptor']['OutputFormat']
table_location = response['Table']['StorageDescriptor']['Location']
serde_info = response['Table']['StorageDescriptor']['SerdeInfo']
partition_keys = response['Table']['PartitionKeys']
input_list = []
for i in range(len(parts)) :
#print(parts[i])
response = glue.batch_delete_partition(
CatalogId=os.environ['catalogId'],
DatabaseName=db_name,
TableName=table_name,
PartitionsToDelete=[
{
'Values': [parts[i]]
}
]
)
input_dict = {
'Values': [
parts[i]
],
'StorageDescriptor': {
'Location': table_location + '/port_dt=' + parts[i].replace(':','%3A') + '/',
'InputFormat': input_format,
'OutputFormat': output_format,
'SerdeInfo': serde_info
}
}
input_list.append(input_dict.copy())
print (table_location)
return input_list
def create_partition(event, context):
for i in range(len(event['TableNames'])) :
input_list = create_partition_list(event['DatabaseName'], event['TableNames'][i], event['PortfolioDate'])
#print('Adding partitions for table - ' + event['TableNames'][i] + ' @ ' + input_list[1]['StorageDescriptor']['Location'])
create_partition_response = glue.batch_create_partition(
CatalogId=os.environ['catalogId'],
DatabaseName=event['DatabaseName'],
TableName=event['TableNames'][i],
PartitionInputList=input_list
)
return ('Partition added for ' + event['TableNames'][i])
def update_table(event, context):
for i in range(len(event['TableNames'])) :
response = glue.get_table(CatalogId=os.environ['catalogId'], DatabaseName=event['DatabaseName'], Name=event['TableNames'][i])
glue.update_table(
CatalogId=os.environ['catalogId'],
DatabaseName=event['DatabaseName'],
TableInput={
'Name': event['TableNames'][i],
'StorageDescriptor': {
'Columns': response['Table']['StorageDescriptor']['Columns'],
'Location': str(response['Table']['StorageDescriptor']['Location']).replace("devl","acpt"),
'InputFormat': response['Table']['StorageDescriptor']['InputFormat'],
'OutputFormat': response['Table']['StorageDescriptor']['OutputFormat'],
'SerdeInfo': response['Table']['StorageDescriptor']['SerdeInfo']
}
}
)
return ('Partition added for ' + event['TableNames'][i])
def drop_table(event, context):
for i in range(len(event['TableNames'])) :
response = glue.delete_table(
CatalogId=os.environ['catalogId'],
DatabaseName=event['DatabaseName'],
Name=event['TableNames'][i]
)
def drop_invalid_partition(event, context):
for i in range(len(event['TableNames'])) :
response = glue.delete_partition(
CatalogId=os.environ['catalogId'],
DatabaseName=event['DatabaseName'],
Name=event['TableNames'][i],
PartitionValues=[
'load_id=__HIVE_DEFAULT_PARTITION__'
]
)
def RemoveDupes(duplicate):
final_list = []
for item in duplicate:
if item not in final_list:
final_list.append(item)
return final_list
def create_Additional_partitions(event):
logger.info(" Adding multiple partitions ..")
delresp={ }
for i in range(len(event['TableNames'])) :
#for prtn in event['partitionlist']:
logger.info("partn list")
logger.info(event['partitionlist'])
#logger.info("****"+prtn)
cleanList=RemoveDupes(event['partitionlist'])
input_list = create_additional_partition_list(event['DatabaseName'], event['TableNames'][i], cleanList,event['Deletefirst'],event['TableLocationPath'])
logger.info(" Partition List")
logger.info(input_list)
create_partition_response = glue.batch_create_partition(
CatalogId=os.environ['catalogId'],
DatabaseName=event['DatabaseName'],
TableName=event['TableNames'][i],
PartitionInputList=input_list
)
print("Input list")
print(input_list)
return ('Partition added for ' + event['TableNames'][i])
def create_additional_partition_list(db_name, table_name, parts,delflag,loc):
response = glue.get_table(CatalogId=os.environ['catalogId'], DatabaseName=db_name, Name=table_name)
# Parsing table info required to create partitions from table
input_format = response['Table']['StorageDescriptor']['InputFormat']
output_format = response['Table']['StorageDescriptor']['OutputFormat']
#table_location = response['Table']['StorageDescriptor']['Location']
if not loc:
table_location = response['Table']['StorageDescriptor']['Location']
else:
table_location =loc
print(table_location)
serde_info = response['Table']['StorageDescriptor']['SerdeInfo']
for part in parts :
input_list=[]
fldrval=[]
logger.info("part="+part)
pathlist=part.split("/")
for fldr in pathlist:
logger.info("folder="+fldr)
fldrval.append(fldr.split('=')[1])
logger.info(" partition ::{} folderval ::{}".format(part,fldrval))
input_dict = {
'Values':fldrval,
'StorageDescriptor': {
'Location': table_location+'/'+part.replace(':','%3A')+'/',
'InputFormat': input_format,
'OutputFormat': output_format,
'SerdeInfo': serde_info
}
}
input_list.append(input_dict.copy())
logger.info(input_list)
if delflag=="true":
delresp = glue.batch_delete_partition(
CatalogId=os.environ['catalogId'],
DatabaseName=db_name,
TableName=table_name,
PartitionsToDelete=[ { 'Values': fldrval } ]
)
if delresp['Errors']:
logger.info(" Delete Has error")
logger.info(delresp)
else:
logger.info("deleted partition")
#logger.info(input_list)
return input_list
def lambda_handler(event, contxt):
# TODO implement
# response = glue.get_table(CatalogId='703959857724', DatabaseName='fre01_acptedl_app_mrp_isgt_01', Name=event['TableName'])
if event['Action'] in ['AddPartitions'] :
print('Executing action :: ' + event['Action'] + ' on tabled :: '+str(event['TableNames']))
create_partition(event, contxt)
elif event['Action'] in ['UpdateTable'] :
print('Executing action :: ' + event['Action'] + ' on tabled :: '+str(event['TableNames']))
update_table(event, contxt)
elif event['Action'] in ['AddAdditionalPartitions'] :
logger.info('Executing action :: ' + event['Action'] + ' on table :: '+str(event['TableNames']))
resp=create_Additional_partitions(event)
#elif event['Action'] in ['xDropTable'] :
# print('Executing action :: ' + event['Action'] + ' on tabled :: '+str(event['TableNames']))
# drop_table(event, contxt)
elif event['Action'] in ['DropInvalidPartitions'] :
print('Executing action :: ' + event['Action'] + ' on tabled :: '+str(event['TableNames']))
drop_invalid_partition(event, contxt)
return 'Success'