odps_module.py 937 B

12345678910111213141516171819202122232425262728293031
  1. #!/usr/bin/env python
  2. # coding=utf-8
  3. from odps import ODPS
  4. class ODPSClient(object):
  5. def __init__(self, project="loghubods"):
  6. self.accessId = "LTAIWYUujJAm7CbH"
  7. self.accessSecret = "RfSjdiWwED1sGFlsjXv0DlfTnZTG1P"
  8. self.endpoint = "http://service.odps.aliyun.com/api"
  9. self.tunnelUrl = "http://dt.cn-hangzhou.maxcompute.aliyun-inc.com"
  10. self.odps = ODPS(
  11. self.accessId,
  12. self.accessSecret,
  13. project,
  14. self.endpoint
  15. )
  16. def execute_sql(self, sql: str):
  17. hints = {
  18. 'odps.sql.submit.mode': 'script'
  19. }
  20. with self.odps.execute_sql(sql, hints=hints).open_reader(tunnel=True) as reader:
  21. pd_df = reader.to_pandas()
  22. return pd_df
  23. def execute_sql_result_save_file(self, sql: str, output_file: str):
  24. data_df = self.execute_sql(sql)
  25. data_df.to_csv(output_file, index=False)