flink任务通过读取配置的方式来运行程序,写好一个模板每次只需要通过修改配置文件重新压缩成jar之后即可。
public class mysqlCDC {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
ParameterTool parameter = null;
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
try {
inputStream = mysqlCDC.class.getResourceAsStream("/application.properties");
inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
properties.load(inputStreamReader);
parameter = ParameterTool.fromMap((Map) properties);
inputStream.close();
inputStreamReader.close();
} catch (IOException e) {
e.printStackTrace();
}finally {
assert inputStream != null;
inputStream.close();
assert inputStreamReader != null;
inputStreamReader.close();
}
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
// 本地环境提交
assert parameter != null;
env.setParallelism(Integer.parseInt(parameter.get("pipeline.parallelism","1")));
env.setRestartStrategy(RestartStrategies.failureRateRestart(
300,
Time.of(5, TimeUnit.MINUTES),
Time.of(10, TimeUnit.SECONDS)
));
env.enableCheckpointing(1000);
env.getCheckpointConfig().setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE);
Checkpoint 必须在一分钟内完成,否则就会被抛弃
env.getCheckpointConfig().setCheckpointTimeout(60000);
同一时间只允许一个 checkpoint 进行
env.getCheckpointConfig().setMaxConcurrentCheckpoints(1);
//设置检查点保存位置
env.getCheckpointConfig().setCheckpointStorage("file:///usr/local/savaPoint");
// 开启实验性的 unaligned checkpoints
env.getCheckpointConfig().enableUnalignedCheckpoints();
StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);
Configuration configuration = tableEnv.getConfig().getConfiguration();
configuration.setString("pipeline.name",parameter.get("pipeline.name"));
String sql = parameter.get("pipeline.sqlScript");
sql = sql.replace("\n","");//去掉回车
sql = sql.trim();//去掉首尾空格
StringBuilder stringBuilder = new StringBuilder("");
char[] chars = sql.toCharArray();
for(char ch : chars){
if(ch==';'){
if(StringUtils.isNotEmpty(stringBuilder.toString())){
tableEnv.executeSql(stringBuilder.toString());
System.out.println(stringBuilder.toString());
}
stringBuilder = new StringBuilder();
}else{
stringBuilder.append(ch);
}
}
}
}
pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xw</groupId>
<artifactId>formwork</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<flink.version>1.14.4</flink.version>
<scala.version>2.12</scala.version>
<!-- <scope>compile</scope>-->
<scope>provided</scope>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
<scope>${scope}</scope>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-connector-kafka_${scala.version}</artifactId>
<version>${flink.version}</version>
<scope>${scope}</scope>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-csv</artifactId>
<version>${flink.version}</version>
<scope>${scope}</scope>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-connector-jdbc_${scala.version}</artifactId>
<version>${flink.version}</version>
<scope>${scope}</scope>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-streaming-java_${scala.version}</artifactId>
<version>${flink.version}</version>
<scope>${scope}</scope>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-table-common</artifactId>
<version>${flink.version}</version>
<scope>${scope}</scope>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-table-api-java-bridge_${scala.version}</artifactId>
<version>${flink.version}</version>
<scope>${scope}</scope>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-table-planner_${scala.version}</artifactId>
<version>${flink.version}</version>
<scope>${scope}</scope>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-java</artifactId>
<version>${flink.version}</version>
<scope>${scope}</scope>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-clients_${scala.version}</artifactId>
<version>${flink.version}</version>
<scope>${scope}</scope>
</dependency>
<dependency>
<groupId>com.ververica</groupId>
<artifactId>flink-sql-connector-mysql-cdc</artifactId>
<version>2.2.1</version>
<scope>${scope}</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<artifactSet>
<excludes>
<!-- 参考flink官网也把这个排除 -->
<exclude>com.google.code.findbugs:jsr305</exclude>
<exclude>ch.qos.logback:*</exclude>
</excludes>
</artifactSet>
</configuration>
</plugin>
</plugins>
</build>
</project>
注意:一定要配置resources目录,不然的话程序在flink中找不到properties文件。