chore: sync warehouse/etl/run_sql.py
This commit is contained in:
1 parent
3bfc73b43c
commit
38a98175af
1 file changed
+42
@@ -0,0 +1,42 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""经 beeline -e 执行 Hive SQL 文件(本环境 beeline -f 会吞语句,故绕过)。
|
||||||
|
|
||||||
|
用法: python3 run_sql.py <sqlfile> [k=v ...] 替换 SQL 中的 ${k}
|
||||||
|
幂等: SQL 本身 INSERT OVERWRITE / IF NOT EXISTS,重跑安全。
|
||||||
|
"""
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
|
||||||
|
BEELINE = "/opt/hive/bin/beeline"
|
||||||
|
URL = "jdbc:hive2://127.0.0.1:10000"
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
path = sys.argv[1]
|
||||||
|
kv = dict(a.split("=", 1) for a in sys.argv[2:])
|
||||||
|
with open(path, encoding="utf-8") as f:
|
||||||
|
lines = [l for l in f.read().splitlines() if not l.strip().startswith("--")]
|
||||||
|
sql = "\n".join(lines)
|
||||||
|
for k, v in kv.items():
|
||||||
|
sql = sql.replace("${%s}" % k, v)
|
||||||
|
sql = sql.strip().rstrip(";")
|
||||||
|
if not sql:
|
||||||
|
print("SQL_EMPTY", path)
|
||||||
|
return 0
|
||||||
|
env = dict(os.environ, TERM="dumb", LANG="C.UTF-8",
|
||||||
|
HADOOP_HOME="/opt/hadoop", JAVA_HOME="/usr/lib/jvm/java-21-openjdk-amd64")
|
||||||
|
r = subprocess.run([BEELINE, "-u", URL, "-n", "czk", "--silent=true", "-e", sql],
|
||||||
|
capture_output=True, text=True, env=env)
|
||||||
|
out = (r.stdout or "") + (r.stderr or "")
|
||||||
|
bad = [l for l in out.splitlines() if l.startswith("Error:")]
|
||||||
|
if r.returncode != 0 or bad:
|
||||||
|
print("SQL_FAIL", path)
|
||||||
|
print("\n".join(bad) or out[-2000:])
|
||||||
|
return 1
|
||||||
|
print("SQL_OK", path)
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
sys.exit(main())
|
||||||
Reference in new issue
Block a user