chore: sync warehouse/etl/collect_hive_meta.py

This commit is contained in:
admin committed 2026-11-24 17:06:04 +00:00
1 parent 38a98175af
commit 2a71508c9c
1 file changed
+80
+80
View File
@@ -0,0 +1,80 @@
#!/usr/bin/env python3
"""采集 Hive Metastore 表结构 -> 提交 Gitea 05_sync/hive_tables.json(供 Dify 标注工作流读取)。
幂等: 内容与远端一致则不提交。用法: python3 collect_hive_meta.py
"""
import base64
import json
import os
import pymysql
import requests
MS = dict(host=os.getenv("HIVE_MS_HOST", "127.0.0.1"), port=3306, user="hive", password="hive123",
database="hive_metastore", charset="utf8mb4")
GITEA = os.getenv("GITEA_URL", "http://192.168.50.176:3001")
REPO = "admin/mini-erp-fabric-demo"
GH = {"Authorization": "token 77c6bd2fdab535b2720821d36b4cfb74aa3bf87c", "Content-Type": "application/json"}
PATH = "05_sync/hive_tables.json"
SQL = """
SELECT d.NAME, t.TBL_NAME, t.TBL_TYPE, s.LOCATION,
c.COLUMN_NAME, c.TYPE_NAME, c.COMMENT,
p.PKEY_NAME, p.PKEY_TYPE, p.PKEY_COMMENT
FROM TBLS t
JOIN DBS d ON d.DB_ID = t.DB_ID
LEFT JOIN SDS s ON s.SD_ID = t.SD_ID
LEFT JOIN COLUMNS_V2 c ON c.CD_ID = s.CD_ID
LEFT JOIN PARTITION_KEYS p ON p.TBL_ID = t.TBL_ID
WHERE d.NAME IN ('ods','dwd','dws','ads')
"""
def main():
conn = pymysql.connect(**MS)
cur = conn.cursor()
cur.execute(SQL)
tables = {}
for db, name, ttype, loc, cn, ct, cc, pn, pt, pc in cur.fetchall():
t = tables.setdefault((db, name), {"db": db, "table": name, "tbl_type": ttype,
"location": loc or "", "columns": {}, "partition_keys": {}})
if cn:
t["columns"][cn] = {"name": cn, "type": ct, "comment": cc or ""}
if pn:
t["partition_keys"][pn] = {"name": pn, "type": pt, "comment": pc or ""}
cur.execute("SELECT d.NAME, t.TBL_NAME, p.PARAM_VALUE FROM TBLS t JOIN DBS d ON d.DB_ID=t.DB_ID "
"JOIN TABLE_PARAMS p ON p.TBL_ID=t.TBL_ID AND p.PARAM_KEY='comment'")
cmap = {(r[0], r[1]): r[2] for r in cur.fetchall()}
conn.close()
out = []
for (db, name), t in sorted(tables.items()):
out.append({"db": db, "table": name, "tbl_type": t["tbl_type"], "location": t["location"],
"comment": cmap.get((db, name), ""),
"columns": sorted(t["columns"].values(), key=lambda c: c["name"]),
"partition_keys": sorted(t["partition_keys"].values(), key=lambda c: c["name"])})
payload = {"tables": out}
text = json.dumps(payload, ensure_ascii=False, indent=2)
url = f"{GITEA}/api/v1/repos/{REPO}/contents/{PATH}"
r = requests.get(url, headers=GH, timeout=20)
sha = ""
if r.status_code == 200:
jj = r.json()
sha = jj.get("sha", "")
if json.loads(base64.b64decode(jj["content"]).decode("utf-8")) == payload:
print(json.dumps({"committed": False, "tables": len(out), "reason": "unchanged"}))
return 0
body = {"content": base64.b64encode(text.encode("utf-8")).decode(), "branch": "main",
"message": "chore(sync): 更新 Hive 表结构快照"}
if sha:
body["sha"] = sha
rr = requests.put(url, headers=GH, json=body, timeout=20)
else:
rr = requests.post(url, headers=GH, json=body, timeout=20)
ok = rr.status_code in (200, 201)
print(json.dumps({"committed": ok, "tables": len(out), "http": rr.status_code,
"resp": "" if ok else rr.text[:200]}, ensure_ascii=False))
return 0 if ok else 1
if __name__ == "__main__":
raise SystemExit(main())