由于阿里云的odps SQL不能进行清空分区表的操作,可以使用下面的py3odps代码来代替实现这个功能

def truncate_partition_table(odps_table_name,partition_feild_name):
    for odps_table in odps_table_name:
        print("Processing odps table is "+odps_table)
        table_name_list=[]
        with o.execute_sql(f'SELECT DISTINCT  {partition_feild_name} FROM {odps_table};').open_reader(tunnel=True) as reader:
            for record in reader:
                table_name_list = table_name_list + record.values
        # print(table_name_list)
        
        t = odps.get_table(f'{odps_table}')
        for table_name in table_name_list:
            date = '{partition_feild_name} = ' + table_name
            print(date)
            t.delete_partition(date, if_exists=True) # 存在的时候才删除


odps_table_name = ['stg_oms_var_trade_money']
truncate_partition_table(odps_table_name=odps_table_name,partition_feild_name='tablename')
Last modification:September 8th, 2021 at 11:13 am