|
| 1 | +package org.apache.flink.table.test.utils; |
| 2 | + |
| 3 | +import org.apache.commons.io.IOUtils; |
| 4 | +import org.apache.commons.lang3.StringUtils; |
| 5 | +import org.apache.flink.streaming.api.CheckpointingMode; |
| 6 | +import org.apache.flink.streaming.api.environment.CheckpointConfig; |
| 7 | +import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; |
| 8 | +import org.apache.flink.table.api.bridge.java.StreamTableEnvironment; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.io.InputStream; |
| 12 | +import java.nio.charset.StandardCharsets; |
| 13 | +import java.util.concurrent.CountDownLatch; |
| 14 | + |
| 15 | +/** |
| 16 | + * Author: lwjhn |
| 17 | + * Date: 2024/5/30 10:11 |
| 18 | + * Description: |
| 19 | + */ |
| 20 | +public class TestUtil { |
| 21 | + public static StreamExecutionEnvironment getLocalExecutionEnvironment(){ |
| 22 | + // StreamTableEnvironment tEnv = StreamTableEnvironment.create(StreamExecutionEnvironment.createLocalEnvironment()); |
| 23 | + StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); |
| 24 | +// "decimal.handling.mode", "string" |
| 25 | + env.enableCheckpointing(10000); |
| 26 | + // 设置模式为exactly-once (这是默认值) |
| 27 | + env.getCheckpointConfig().setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE); |
| 28 | + // 确保检查点之间有至少500 ms的间隔【checkpoint最小间隔】 |
| 29 | + env.getCheckpointConfig().setMinPauseBetweenCheckpoints(500); |
| 30 | + // 检查点必须在10分钟内完成,或者被丢弃【checkpoint的超时时间】 |
| 31 | + env.getCheckpointConfig().setCheckpointTimeout(600000); |
| 32 | + // 同一时间只允许进行一个检查点 |
| 33 | + env.getCheckpointConfig().setMaxConcurrentCheckpoints(1); |
| 34 | + // cancel后,保留Checkpoint数据,以便根据实际需要恢复到指定的Checkpoint |
| 35 | + env.getCheckpointConfig().setExternalizedCheckpointCleanup(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION); |
| 36 | + // scan.incremental.snapshot.enabled |
| 37 | + env.getCheckpointConfig().setCheckpointStorage("file:///dist/test/checkpoint"); |
| 38 | + return env; |
| 39 | + } |
| 40 | + |
| 41 | + public static StreamTableEnvironment getLocalStreamTableEnvironment(){ |
| 42 | + return StreamTableEnvironment.create(getLocalExecutionEnvironment()); |
| 43 | + } |
| 44 | + |
| 45 | + public static String loadFile(String path){ |
| 46 | + try (InputStream inputStream = |
| 47 | + Thread.currentThread().getContextClassLoader().getResourceAsStream(path)){ |
| 48 | + assert inputStream != null; |
| 49 | + return IOUtils.toString(inputStream, StandardCharsets.UTF_8); |
| 50 | + /* |
| 51 | + String text = new BufferedReader( |
| 52 | + new InputStreamReader(Objects.requireNonNull(TestUtil.class.getResourceAsStream(path)), StandardCharsets.UTF_8)) |
| 53 | + .lines() |
| 54 | + .collect(Collectors.joining("\n")); |
| 55 | + */ |
| 56 | + } catch (IOException e) { |
| 57 | + throw new RuntimeException(e); |
| 58 | + } |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + public static String[] getStatements(String path){ |
| 63 | + return SqlUtil.preparedStatement(loadFile(path)); |
| 64 | + } |
| 65 | + |
| 66 | + public static void executeSql(String path) { |
| 67 | + StreamTableEnvironment environment = getLocalStreamTableEnvironment(); |
| 68 | + String[] statements = TestUtil.getStatements(path); |
| 69 | + for(String statement : statements){ |
| 70 | + if(StringUtils.isBlank(statement=statement.trim())) |
| 71 | + continue; |
| 72 | + System.out.printf("%nFlink SQL [%d]> %s;%n", System.currentTimeMillis(), statement); |
| 73 | + environment.executeSql(statement).print(); |
| 74 | + } |
| 75 | + System.out.printf("[%d] END...%n", System.currentTimeMillis()); |
| 76 | + try { |
| 77 | + new CountDownLatch(1).await(); |
| 78 | + } catch (InterruptedException e) { |
| 79 | + throw new RuntimeException(e); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments