-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_table_definition.py
107 lines (87 loc) · 3.06 KB
/
make_table_definition.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
import pandas as pd
from google.cloud import bigquery
from openpyxl import load_workbook
# 対象データセットの設定
PROJECT_ID = "bigquery-public-data"
DATASET_ID = "chicago_taxi_trips"
# テンプレートファイルの設定
STRT_TBL = (2, 3) # テーブル情報の開始位置 (行, 列)
STRT_COL = (8, 1) # カラム情報の開始位置 (行, 列)
# BigQueryクライアントのインスタンスを生成
client = bigquery.Client()
query = f"""
SELECT
*
FROM
`{PROJECT_ID}.{DATASET_ID}.INFORMATION_SCHEMA.COLUMNS`;
"""
# クエリを実行してDataFrameとして取得
df = client.query(query).to_dataframe()
df_columns = df[
[
"table_catalog",
"table_schema",
"table_name",
"column_name",
"ordinal_position",
"data_type",
"is_nullable",
"is_partitioning_column",
"clustering_ordinal_position",
]
]
# Excel書き込み時のNA対策でstr型に変換
df_columns = df_columns.astype({"clustering_ordinal_position": str})
query = f"""
SELECT
*
FROM
`{PROJECT_ID}.{DATASET_ID}.INFORMATION_SCHEMA.COLUMN_FIELD_PATHS`;
"""
# クエリを実行してDataFrameとして取得
df = client.query(query).to_dataframe()
df_description = df[["table_name", "column_name", "description"]]
df_table_def = df_columns.merge(df_description, on=["table_name", "column_name"])
# テンプレートファイルを開く
wb = load_workbook("./table_template.xlsx")
# templateシートを取得
ws_template = wb["template"]
# tableごとにテーブル定義書を作成
for table_name, df in df_table_def.groupby("table_name"):
# templeteシートをコピペ
ws = wb.copy_worksheet(ws_template)
print(f"Table Name: {table_name}")
# シート名をテーブル名に変更
ws.title = table_name
# テーブル情報の書き込み
ws.cell(
row=STRT_TBL[0],
column=STRT_TBL[1],
value=df["table_catalog"].iloc[0],
)
ws.cell(
row=STRT_TBL[0] + 1,
column=STRT_TBL[1],
value=df["table_schema"].iloc[0],
)
ws.cell(
row=STRT_TBL[0] + 2,
column=STRT_TBL[1],
value=df["table_name"].iloc[0],
)
# カラム情報の書き込み
for i, (_, sr) in enumerate(df.iterrows()):
row = STRT_COL[0] + i
ws.cell(row=row, column=STRT_COL[1], value=sr["ordinal_position"])
ws.cell(row=row, column=STRT_COL[1] + 1, value=sr["column_name"])
ws.cell(row=row, column=STRT_COL[1] + 2, value=sr["data_type"])
ws.cell(row=row, column=STRT_COL[1] + 3, value=sr["is_nullable"])
ws.cell(row=row, column=STRT_COL[1] + 4, value=sr["is_partitioning_column"])
ws.cell(row=row, column=STRT_COL[1] + 5, value=sr["clustering_ordinal_position"])
ws.cell(row=row, column=STRT_COL[1] + 6, value=sr["description"])
# templeteシートを削除
wb.remove(ws_template)
# テーブル定義書(エクセルファイル)の保存
save_path = f"./table_definition_{PROJECT_ID}.{DATASET_ID}.xlsx"
wb.save(save_path)
print(f"Saved file at {save_path}")