-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
189 lines (154 loc) · 7.38 KB
/
main.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
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
#!/usr/bin/python
# Загружаем библиотеки
import pandas
import os
import jaydebeapi
from py_scripts.my_module import declutter_stg, insert_stg, insert_scd2, merge_scd2, upd_meta
# Установка соединения с сервером
conn = jaydebeapi.connect('oracle.jdbc.driver.OracleDriver','jdbc:oracle:thin:demipt/*********@d*******.******.ru:****/*****',['******','******'],'/home/demipt/ojdbc8.jar')
conn.jconn.setAutoCommit(False)
curs = conn.cursor()
# Объявляем переменные
meta_tab = 'DEMIPT.SNZH_META_LOAD'
src_attr_clients = 'client_id, last_name, first_name, patronymic, date_of_birth,\
passport_num, passport_valid_to, phone, create_dt, update_dt'
stg_attr_clients = 'client_id, last_name, first_name, patronymic, date_of_birth,\
passport_num, passport_valid_to, phone'
dwh_attr_clients = 'client_id, last_name, first_name, patronymic,date_of_birth,\
passport_num, passport_valid_to, phone,\
effective_from, effective_to, deleted_flg'
stg_key_clients = 'client_id'
dwh_key_clients = 'client_id'
src_attr_accounts = 'account, valid_to, client, create_dt, update_dt'
stg_attr_accounts = 'account_num, valid_to, client'
dwh_attr_accounts = 'account_num, valid_to, client,\
effective_from, effective_to, deleted_flg'
stg_key_accounts = 'account_num'
dwh_key_accounts = 'account_num'
src_attr_cards = 'card_num, account, create_dt, update_dt'
stg_attr_cards = 'card_num, account_num'
dwh_attr_cards = 'card_num, account_num, effective_from, effective_to, deleted_flg'
stg_key_cards = 'card_num'
dwh_key_cards = 'card_num'
stg_tab_list = ['DEMIPT.SNZH_STG_CLIENTS', 'DEMIPT.SNZH_STG_ACCOUNTS', 'DEMIPT.SNZH_STG_CARDS',
'DEMIPT.SNZH_STG_TRANSACTIONS', 'DEMIPT.SNZH_STG_TERMINALS',
'DEMIPT.SNZH_STG_PSSPRT_BLACKLIST', 'DEMIPT.SNZH_STG_CLIENTS_KEYS',
'DEMIPT.SNZH_STG_ACCOUNTS_KEYS', 'DEMIPT.SNZH_STG_CARDS_KEYS',
'DEMIPT.SNZH_STG_TERMINALS_KEYS']
# Очистка STG
for table_name in stg_tab_list:
declutter_stg(table_name)
# Загрузка файлов из сторонних источников
path = '/home/demipt/SNZH/'
field = os.listdir(path)
## Загрузка паспортов
for file in field:
index = file.find('pas')
if index != -1:
passports = file[index:]
passp_bl_list_df = pandas.read_excel(passports, sheet_name='blacklist', header=0, index_col=None)
passp_bl_list_df['date'] = passp_bl_list_df['date'].astype(str)
## Загрузка терминалов
for file in field:
index = file.find('ter')
if index != -1:
terminals = file[index:]
date_terminals = terminals.split('_')[1].split('.')[0]
terminals_df = pandas.read_excel(terminals, sheet_name='terminals', header=0, index_col=None)
## Загрузка транзакций
for file in field:
index = file.find('tr')
if index != -1:
transactions = file[index:]
transactions_df = pandas.read_csv(transactions, sep = ';', header=0, index_col=None)
transactions_df['transaction_id'] = transactions_df['transaction_id'].astype(str)
rep_date = transactions.split('_')[1].split('.')[0]
# Перемещение файлов в архив
passp_path_old = path + passports
passp_path_new = path +'archive/'+ passports + '.backup'
term_path_old = path + terminals
term_path_new = path + 'archive/' +terminals + '.backup'
trans_path_old = path + transactions
trans_path_new = path + 'archive/' + transactions + '.backup'
os.rename(passp_path_old, passp_path_new)
os.rename(term_path_old, term_path_new)
os.rename(trans_path_old, trans_path_new)
# Выполнение ETL для сведений, хранящихся в СУБД ORACLE
## Захват данных в STG
insert_stg(stg_tab_name= 'DEMIPT.SNZH_STG_CLIENTS',
stg_attr_list = stg_attr_clients,
src_attr_list = src_attr_clients,
src_tab_name = 'BANK.CLIENTS',
meta_tab_name = meta_tab, db_name = 'DEMIPT',
dwh_tab_name = 'SNZH_DWH_DIM_CLIENTS_HIST')
insert_stg( stg_tab_name = 'DEMIPT.SNZH_STG_ACCOUNTS',
stg_attr_list = stg_attr_accounts,
src_attr_list = src_attr_accounts,
src_tab_name = 'BANK.ACCOUNTS',
meta_tab_name = meta_tab, db_name = 'DEMIPT',
dwh_tab_name = 'SNZH_DWH_DIM_ACCOUNTS_HIST')
insert_stg(stg_tab_name= 'DEMIPT.SNZH_STG_CARDS',
stg_attr_list = stg_attr_cards,
src_attr_list = src_attr_cards,
src_tab_name = 'BANK.CARDS',
meta_tab_name = meta_tab, db_name= 'DEMIPT',
dwh_tab_name = 'SNZH_DWH_DIM_CARDS_HIST')
## Переливаем измерения из STG в хранилище SCD2
insert_scd2(dwh_tab_name = 'DEMIPT.SNZH_DWH_DIM_CLIENTS_HIST',
dwh_attr_list = dwh_attr_clients,
stg_attr_list = stg_attr_clients,
stg_tab_name = 'DEMIPT.SNZH_STG_CLIENTS')
insert_scd2(dwh_tab_name = 'DEMIPT.SNZH_DWH_DIM_ACCOUNTS_HIST',
dwh_attr_list = dwh_attr_accounts,
stg_attr_list = stg_attr_accounts,
stg_tab_name = 'DEMIPT.SNZH_STG_ACCOUNTS')
insert_scd2(dwh_tab_name = 'DEMIPT.SNZH_DWH_DIM_CARDS_HIST',
dwh_attr_list = dwh_attr_cards,
stg_attr_list = stg_attr_cards,
stg_tab_name = 'DEMIPT.SNZH_STG_CARDS')
## Обновляем данные в хранилище SCD2
merge_scd2(dwh_tab_name = 'DEMIPT.SNZH_DWH_DIM_CLIENTS_HIST',
stg_tab_name = 'DEMIPT.SNZH_STG_CLIENTS',
dwh_key = dwh_key_clients,
scd_key = stg_key_clients)
merge_scd2(dwh_tab_name = 'DEMIPT.SNZH_DWH_DIM_ACCOUNTS_HIST',
stg_tab_name = 'DEMIPT.SNZH_STG_ACCOUNTS',
dwh_key = dwh_key_accounts,
scd_key = stg_key_accounts)
merge_scd2(dwh_tab_name = 'DEMIPT.SNZH_DWH_DIM_CARDS_HIST',
stg_tab_name = 'DEMIPT.SNZH_STG_CARDS',
dwh_key = dwh_key_cards,
scd_key = stg_key_cards)
# Обработка удалений
with open('/home/demipt/SNZH/sql_scripts/del_sql.txt', 'r') as file:
sql = file.read()
for sql_statement in sql.split(";"):
curs.execute(sql_statement)
## Обновляем метаданные - дату максимальной загрузки
upd_meta(meta_tab_name = meta_tab, stg_tab_name = 'DEMIPT.SNZH_STG_CLIENTS',
db_name = 'DEMIPT', short_dwh_tab_name = 'SNZH_DWH_DIM_CLIENTS_HIST')
upd_meta(meta_tab_name = meta_tab, stg_tab_name = 'DEMIPT.SNZH_STG_ACCOUNTS',
db_name = 'DEMIPT', short_dwh_tab_name = 'SNZH_DWH_DIM_ACCOUNTS_HIST')
upd_meta(meta_tab_name = meta_tab, stg_tab_name = 'DEMIPT.SNZH_STG_CARDS',
db_name = 'DEMIPT', short_dwh_tab_name = 'SNZH_DWH_DIM_CARDS_HIST')
# Выполнение ETL для сведений, хранящихся в файлах
## Захват данных в STG
with open('/home/demipt/SNZH/sql_scripts/incert_files.txt', 'r') as file:
sql = file.read().split(";")
curs.executemany(sql[0], passp_bl_list_df.values.tolist())
curs.executemany(sql[1], transactions_df.values.tolist())
curs.executemany(sql[2], terminals_df.values.tolist())
## Реализация остальных этапов ETL
with open('/home/demipt/SNZH/sql_scripts/incr_files.txt', 'r') as file:
sql = file.read()
for sql_statement in sql.split(";"):
curs.execute(sql_statement.format(date_terminals))
# Строим отчет
with open('/home/demipt/SNZH/sql_scripts/reports_sql.txt', 'r') as file:
sql = file.read()
for sql_statement in sql.split(";"):
curs.execute(sql_statement.format(rep_date))
conn.commit()
# Закрытие соединения
curs.close()
conn.close()