-
Notifications
You must be signed in to change notification settings - Fork 0
/
connPG.py
99 lines (79 loc) · 3.4 KB
/
connPG.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
import psycopg2
# PostgreSQL数据库连接参数
host = "192.168.1.232"
database = "imdb"
user = "gpadmin"
password = "gpadmin"
tpcdstables = ["catalog_sales", "catalog_returns", "store_sales", "store_returns", "web_sales", "web_returns", "inventory","call_center",
"catalog_page","customer","customer_address","customer_demographics","date_dim","household_demographics","income_band",
"item","promotion","reason","ship_mode","store","time_dim","warehouse","web_page","web_site","dbgen_version"]
imdbtables = ["aka_name","aka_title","cast_info","char_name","comp_cast_type","company_name","company_type","complete_cast","info_type",
"keyword","kind_type","link_type","movie_companies","movie_info","movie_info_idx","movie_keyword","movie_link",
"name","person_info","role_type","title"]
def getParatitionRecords():
try:
# 将数组保存到文件中
with open("./data/query_result.txt", "w") as file:
for table in imdbtables:
connection = psycopg2.connect(
host=host,
database=database,
user=user,
password=password,
port=5435
)
# 创建游标对象
cursor = connection.cursor()
# 执行SQL查询
sql_query = "select count(*) from "+table+" group by gp_segment_id order by gp_segment_id;"
cursor.execute(sql_query)
# 从游标中获取所有结果
rows = cursor.fetchall()
# 关闭游标和数据库连接
cursor.close()
connection.close()
# 将查询结果保存到数组中
result_array = []
for row in rows:
result_array.append(row[0])
file.write(str(result_array) + "\n")
print("Query result saved to query_result.txt")
except (Exception, psycopg2.Error) as error:
print("Error while connecting to PostgreSQL:", error)
finally:
# 关闭数据库连接
if connection:
connection.close()
def getParameterValues(segmentID):
try:
connection = psycopg2.connect(
host=host,
database=database,
user=user,
password=password,
port=5435
)
# 创建游标对象
cursor = connection.cursor()
# 执行SQL查询
sql_query = "SELECT content AS segment_id, name AS parameter_name, setting AS parameter_value " \
"FROM gp_configuration " \
"WHERE content = "+ segmentID +" AND name " \
"IN ('shared_buffers', 'work_mem', 'gp_max_packet_size', 'max_connections', 'random_page_cost', 'seq_page_cost');"
cursor.execute(sql_query)
# 从游标中获取所有结果
rows = cursor.fetchall()
# 关闭游标和数据库连接
cursor.close()
connection.close()
# 将查询结果保存到数组中
result_array = []
for row in rows:
result_array.append(row[0])
return result_array
except (Exception, psycopg2.Error) as error:
print("Error while connecting to PostgreSQL:", error)
finally:
# 关闭数据库连接
if connection:
connection.close()