package com.lanyuanxiaoyao.service.executor.task.helper;

import com.lanyuanxiaoyao.service.executor.core.TaskContext;
import java.util.Arrays;
import java.util.Iterator;
import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.common.serialization.SimpleStringEncoder;
import org.apache.flink.api.java.functions.FlatMapIterator;
import org.apache.flink.connector.file.sink.FileSink;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.filesystem.OutputFileConfig;
import org.apache.flink.streaming.api.functions.sink.filesystem.bucketassigners.BasePathBucketAssigner;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hudi.common.fs.FSUtils;

/**
 * @author lanyuanxiaoyao
 * @date 2024-01-08
 */
public class FlinkHelper {
    public static StreamExecutionEnvironment getSteamEnvironment() {
        return StreamExecutionEnvironment.getExecutionEnvironment();
    }

    public static StreamExecutionEnvironment getBatchEnvironment() {
        StreamExecutionEnvironment environment = getSteamEnvironment();
        environment.setRuntimeMode(RuntimeExecutionMode.BATCH);
        return environment;
    }

    public static FileSink<String> createFileSink(TaskContext context) {
        return createFileSink(context.getTaskId(), context.getResultPath());
    }

    public static FileSink<String> createFileSink(String taskId, String resultPath) {
        return FileSink
                .<String>forRowFormat(new Path(resultPath + "/" + taskId), new SimpleStringEncoder<>("UTF-8"))
                .withBucketAssigner(new BasePathBucketAssigner<>())
                .withOutputFileConfig(new OutputFileConfig("task", ""))
                .build();
    }
}
