在学习flink 的过程中,伊始的flink 版本是1.17.2 报题目的错误 ,百思不得其解,尝试更替了1.19.1 然后就成功了 ,期间未做任何的修改 。
ui 默认地址 : http://localhost:8081
pom 文件 如下
XML
<?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>org.example</groupId>
<artifactId>TT</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-streaming-java</artifactId>
<version>1.19.1</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-clients</artifactId>
<version>1.19.1</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-connector-datagen</artifactId>
<version>1.19.1</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-table-api-java-bridge</artifactId>
<version>1.19.1</version>
</dependency>
<!--在本地的集成开发环境里运行Table API和SQL的支持-->
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-table-planner-loader</artifactId>
<version>1.19.1</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-table-runtime</artifactId>
<version>1.19.1</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-runtime-web</artifactId>
<version>1.19.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-java</artifactId>
<version>1.19.1</version>
</dependency>
</dependencies>
</project>
Java 类 ,flink 环境的初始化有很多种,这个只是本地环境的一种 :
java
package com.org.flinkBase;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;
public class FlinkDemoCumulateUI {
public static void main(String[] args) throws Exception{
Configuration conf = new Configuration();
//conf.setString(RestOptions.BIND_PORT,"8081-8089");
StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(conf);
// 产生一个初始数据流,这里我们使用一个简单的集合作为数据源
DataStream<String> initialStream = env.fromElements("Hello", "Flink", "World","Hello", "Flink", "World");
// 定义一个 Map 算子,将流中的每个字符串转换为大写
DataStream<String> transformedStream = initialStream.map(new MapFunction<String, String>() {
@Override
public String map(String value) throws Exception {
return value.toUpperCase();
}
});
// 使用 ProcessFunction 增加处理时间
transformedStream
.keyBy((String value) -> value) // 这里使用每个字符串本身作为 key,只是为了演示
.process(new ProcessFunction<String, String>() {
@Override
public void processElement(String value, Context ctx, Collector<String> out) throws Exception {
// 模拟处理时间,例如,延迟5秒
Thread.sleep(85000);
out.collect(value);
}
})
.print(); // 输出转换后的数据流
env.execute("Java Flink Job with Map Operator");
}
}
Ui 结果:
感悟:
柳暗花明又一村,死磕到底的勇气,不断试错,新时代的爱迪生,努力寻找答案