#!/usr/bin/env python
# coding=utf-8

from odps import ODPS


class ODPSClient(object):
    def __init__(self, project="loghubods"):
        self.accessId = "LTAIWYUujJAm7CbH"
        self.accessSecret = "RfSjdiWwED1sGFlsjXv0DlfTnZTG1P"
        self.endpoint = "http://service.odps.aliyun.com/api"
        self.tunnelUrl = "http://dt.cn-hangzhou.maxcompute.aliyun-inc.com"

        self.odps = ODPS(
            self.accessId,
            self.accessSecret,
            project,
            self.endpoint
        )

    def execute_sql(self, sql: str):
        hints = {
            'odps.sql.submit.mode': 'script'
        }
        with self.odps.execute_sql(sql, hints=hints).open_reader(tunnel=True) as reader:
            pd_df = reader.to_pandas()
        return pd_df

    def execute_sql_result_save_file(self, sql: str, output_file: str):
        data_df = self.execute_sql(sql)
        data_df.to_csv(output_file, index=False)
