import os
import sys
sys.path.append(os.path.dirname(sys.path[0]))
from utils.secure_db_client import get_remote_engine


def export_data(site_name, date_type, date_info):
    engine = get_remote_engine(
        site_name="us",  # -> database "selection"
        db_type="postgresql_14",  # -> 服务端 alias "mysql"
        # user="fangxingjun",         # -> 服务端 alias "mysql"
        # user_token="5f1b2e9c3a4d7f60"   # 可不传，走默认
    )
    partitions = {
        'site_name': site_name,
        'date_type': date_type,
        'date_info': date_info,
    }
    cols_list = ['asin', 'asin_is_variation', 'volume', 'weight_str', 'date_info']

    import_table = f'{site_name}_all_syn_st_month_{date_info.replace("-", "_")}'
    hive_table = 'dwt_asin_sync'
    print(f"import_table: {import_table}, hive_table: {hive_table}")
    print(f"partitions: {partitions}")

    engine.sqoop_raw_export(
        hive_table=hive_table,
        import_table=import_table,
        partitions=partitions,
        m=1,
        cols=','.join(cols_list)
    )


if __name__ == '__main__':
    # site_name = 'us'
    # date_type = 'month'
    # date_info = '2026-01'
    site_name = sys.argv[1]  # 参数1：站点
    date_type = sys.argv[2]  # 参数2：类型：week/4_week/month/quarter/day
    date_info = sys.argv[3]  # 参数3：年-周/年-月/年-季/年-月-日, 比如: 2022-1
    export_data(site_name, date_type, date_info)
