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
import os
import sys
sys.path.append(os.path.dirname(sys.path[0]))
from utils.ssh_util import SSHUtil
from utils.common_util import CommonUtil
from utils.db_util import DBUtil,DbTypes
if __name__ == '__main__':
site_name = CommonUtil.get_sys_arg(1, None)
db_type = DbTypes.postgresql.name
print("导出到PG库中")
export_master_tb = f"{site_name}_st_year_week"
export_tb = f"{export_master_tb}_tmp"
engine = DBUtil.get_db_engine(db_type, site_name)
with engine.connect() as connection:
sql = f"""
drop table if exists {export_master_tb}_old;
CREATE TABLE {export_tb} (LIKE {export_master_tb} INCLUDING ALL);
"""
print("================================执行sql================================")
print(sql)
connection.execute(sql)
# 导出表名
sh = CommonUtil.build_export_sh(
site_name=site_name,
db_type=db_type,
hive_tb="dim_st_year_week",
export_tb=export_tb,
col=[
"search_term",
"st_key",
"year_week"
],
partition_dict={
"site_name": site_name
}
)
client = SSHUtil.get_ssh_client()
SSHUtil.exec_command_async(client, sh, ignore_err=False)
client.close()
engine = DBUtil.get_db_engine(db_type, site_name)
with engine.connect() as connection:
sql = f"""
ALTER TABLE {export_master_tb} RENAME TO {export_master_tb}_old;
ALTER TABLE {export_tb} RENAME TO {export_master_tb};
"""
print("================================执行sql================================")
print(sql)
connection.execute(sql)
print("success")