run_sql.py 832 B

12345678910111213141516171819202122232425262728
  1. #!/usr/bin/env python3
  2. """Execute an explicit SQL file with the bundled environment-based ODPS client."""
  3. from __future__ import annotations
  4. import argparse
  5. from pathlib import Path
  6. from odps_module import ODPSClient
  7. def main() -> None:
  8. parser = argparse.ArgumentParser(description="Run an ODPS SQL file and save UTF-8 CSV")
  9. parser.add_argument("sql_file", type=Path)
  10. parser.add_argument("output_file", type=Path)
  11. parser.add_argument("--project", help="Override ODPS_PROJECT for this query")
  12. args = parser.parse_args()
  13. sql = args.sql_file.expanduser().read_text(encoding="utf-8")
  14. output_path = ODPSClient(project=args.project).execute_sql_result_save_file(
  15. sql,
  16. args.output_file,
  17. )
  18. print(f"[CSV] {output_path.resolve()}", flush=True)
  19. if __name__ == "__main__":
  20. main()