package com.lanyuanxiaoyao.service.executor.task;

import cn.hutool.core.util.StrUtil;
import com.lanyuanxiaoyao.service.common.Constants;
import com.lanyuanxiaoyao.service.executor.core.TaskContext;
import com.lanyuanxiaoyao.service.executor.task.helper.ArgumentsHelper;
import com.lanyuanxiaoyao.service.executor.task.helper.HdfsHelper;
import org.apache.flink.types.Row;
import org.apache.flink.util.CloseableIterator;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hudi.common.table.HoodieTableMetaClient;
import org.apache.hudi.configuration.FlinkOptions;
import org.eclipse.collections.api.factory.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 统计表总数
 *
 * @author lanyuanxiaoyao
 * @date 2024-05-17
 */
public class TableSummary extends SQLExecutor {
    private static final Logger logger = LoggerFactory.getLogger(TableSummary.class);

    public static void main(String[] args) throws Exception {
        TaskContext taskContext = ArgumentsHelper.getContext(args);
        logger.info("Context: {}", taskContext);

        ArgumentsHelper.checkMetadata(taskContext, "hdfs");
        String hdfs = (String) taskContext.getMetadata().get("hdfs");

        Configuration configuration = new Configuration();
        configuration.setStrings(FlinkOptions.PATH.key(), hdfs);
        configuration.setInt(FlinkOptions.READ_TASKS.key(), 50);

        HoodieTableMetaClient metaClient = HoodieTableMetaClient.builder()
                .setConf(configuration)
                .setBasePath(hdfs)
                .build();
        try (CloseableIterator<Row> iterator = executeBatch(
                metaClient,
                tableName -> StrUtil.format("select count({}) as `count`, max({}) as latest_op_ts from `{}`", Constants.UNION_KEY_NAME, Constants.LATEST_OPERATION_TIMESTAMP_KEY_NAME, tableName)
        )) {
            HdfsHelper.createResult(FileSystem.get(metaClient.getHadoopConf()), taskContext, generateResult(iterator, Lists.immutable.of("count", "latest_op_ts")));
        }
    }
}
