1
+ """
2
+ starrocks 批量修改物化视图副本数,可配置修改前和修改后的副本数。(删除重建)
3
+ SR (v3.2.15)当前物化视图无法修改副本数,已反馈官方:
4
+ MySQL [information_schema]> ALTER MATERIALIZED VIEW dsj_dwd.mv_zt_hhy_staff_user_company_info SET ("replication_num" = "1");
5
+ ERROR 1064 (HY000): Unexpected exception: Getting analyzing error. Detail message: Modify failed because unknown properties: {replication_num=1}, please add `session.` prefix if you want add session variables for mv(eg, "session.query_timeout"="30000000")..
6
+ """
7
+ import pymysql
8
+ import time
9
+
10
+ # 副本数配置
11
+ REPLICATION_NUM_BEFORE = "3" # 修改前的副本数
12
+ REPLICATION_NUM_AFTER = "1" # 修改后的副本数
13
+
14
+ # 数据库连接配置
15
+ DB_CONFIG = {
16
+ "host" : "127.0.0.1" ,
17
+ "user" : "root" ,
18
+ "password" : "root.COM2025*" ,
19
+ "database" : "information_schema" ,
20
+ "port" : 9030 ,
21
+ }
22
+
23
+
24
+ def fetch_materialized_views ():
25
+ """查询所有指定 replication_num 的物化视图"""
26
+ query = f"""
27
+ SELECT TABLE_SCHEMA, TABLE_NAME
28
+ FROM information_schema.tables_config
29
+ WHERE TABLE_ENGINE = 'MATERIALIZED_VIEW'
30
+ AND PROPERTIES LIKE '%"replication_num":"{ REPLICATION_NUM_BEFORE } "%';
31
+ """
32
+ connection = pymysql .connect (** DB_CONFIG )
33
+ try :
34
+ with connection .cursor () as cursor :
35
+ cursor .execute (query )
36
+ return cursor .fetchall ()
37
+ finally :
38
+ connection .close ()
39
+
40
+
41
+ def get_create_view_statement (schema , table ):
42
+ """获取物化视图的创建语句"""
43
+ query = f"SHOW CREATE TABLE `{ schema } `.`{ table } `;"
44
+ connection = pymysql .connect (** DB_CONFIG )
45
+ try :
46
+ with connection .cursor () as cursor :
47
+ cursor .execute (query )
48
+ result = cursor .fetchone ()
49
+ return result [1 ] # 返回创建语句
50
+ finally :
51
+ connection .close ()
52
+
53
+
54
+ def drop_view (schema , table ):
55
+ """删除物化视图"""
56
+ query = f"DROP MATERIALIZED VIEW `{ schema } `.`{ table } `;"
57
+ connection = pymysql .connect (** DB_CONFIG )
58
+ try :
59
+ with connection .cursor () as cursor :
60
+ cursor .execute (query )
61
+ connection .commit ()
62
+ finally :
63
+ connection .close ()
64
+
65
+
66
+ def create_view (create_statement ):
67
+ """创建物化视图"""
68
+ print (create_statement )
69
+ connection = pymysql .connect (** DB_CONFIG )
70
+ try :
71
+ with connection .cursor () as cursor :
72
+ cursor .execute (create_statement )
73
+ connection .commit ()
74
+ finally :
75
+ connection .close ()
76
+
77
+
78
+ def update_replication_num (schema , create_statement ):
79
+ """修改创建语句中的 replication_num"""
80
+ return create_statement .replace (
81
+ f'"replication_num" = "{ REPLICATION_NUM_BEFORE } "' ,
82
+ f'"replication_num" = "{ REPLICATION_NUM_AFTER } "'
83
+ ).replace ("CREATE MATERIALIZED VIEW " , f"CREATE MATERIALIZED VIEW `{ schema } `." )
84
+
85
+
86
+ def main ():
87
+ # 获取所有指定 replication_num 的物化视图
88
+ views = fetch_materialized_views ()
89
+ for schema , table in views :
90
+ print (f"Processing view: { schema } .{ table } " )
91
+
92
+ # 获取创建语句
93
+ create_statement = get_create_view_statement (schema , table )
94
+ print ("Original create statement fetched." )
95
+
96
+ # 修改 replication_num
97
+ updated_statement = update_replication_num (schema , create_statement )
98
+ print (f"Create statement updated with replication_num={ REPLICATION_NUM_AFTER } ." )
99
+ time .sleep (0.5 )
100
+
101
+ # 删除原视图
102
+ drop_view (schema , table )
103
+ print (f"Original view dropped: { schema } .{ table } " )
104
+ time .sleep (0.5 )
105
+
106
+ # 重新创建视图
107
+ create_view (updated_statement )
108
+ print (f"View recreated with updated replication_num={ REPLICATION_NUM_AFTER } .----------" )
109
+
110
+
111
+ if __name__ == "__main__" :
112
+ main ()
0 commit comments