赞
踩
本内容为后续系例示例的演示基础,所有相关示例、数据源、依赖均在此文内的工程与数据之上进行演示。后续如有变动,会在本文中变更。如有错误之处,敬请指出。
示例环境
- java.version: 1.8.x
- flink.version: 1.11.1
创建工程
idea中创建项目,项目名称:flink-examples
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>com.flink.examples</groupId>
- <artifactId>flink-examples</artifactId>
- <packaging>pom</packaging>
- <version>1.0-SNAPSHOT</version>
- <modules>
- <module>stream</module>
- <module>connectors</module>
- </modules>
-
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- <java.version>1.8</java.version>
- <flink.version>1.11.1</flink.version>
- <compiler.version>8</compiler.version>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>org.apache.flink</groupId>
- <artifactId>flink-streaming-java_2.11</artifactId>
- <version>${flink.version}</version>
- </dependency>
- <dependency>
- <groupId>org.apache.flink</groupId>
- <artifactId>flink-clients_2.11</artifactId>
- <version>${flink.version}</version>
- </dependency>
- <dependency>
- <groupId>org.apache.flink</groupId>
- <artifactId>flink-table-api-java-bridge_2.11</artifactId>
- <version>${flink.version}</version>
- </dependency>
- <dependency>
- <groupId>org.apache.flink</groupId>
- <artifactId>flink-table-planner-blink_2.11</artifactId>
- <version>${flink.version}</version>
- </dependency>
- <dependency>
- <groupId>org.apache.flink</groupId>
- <artifactId>flink-table-planner_2.11</artifactId>
- <version>${flink.version}</version>
- <!--<scope>provided</scope>-->
- </dependency>
- <dependency>
- <groupId>org.apache.flink</groupId>
- <artifactId>flink-json</artifactId>
- <version>${flink.version}</version>
- <!--<scope>test</scope>-->
- </dependency>
- <dependency>
- <groupId>com.google.code.gson</groupId>
- <artifactId>gson</artifactId>
- <version>2.8.6</version>
- </dependency>
- <dependency>
- <groupId>org.apache.logging.log4j</groupId>
- <artifactId>log4j-core</artifactId>
- <version>2.11.1</version>
- </dependency>
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <encoding>UTF-8</encoding>
- <source>${compiler.version}</source>
- <target>${compiler.version}</target>
- </configuration>
- </plugin>
-
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-shade-plugin</artifactId>
- <executions>
- <execution>
- <phase>package</phase>
- <goals>
- <goal>shade</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
-
- </project>

项目分两个模块
- flink-examples
- |----connectors(中件间连接器示例模块)
- |----examples(模拟电商订单数据并推送到kafka中,以及flink核心数据流处理客户端)
- |----stream(数据流与算子、方法、窗口等示例模块)
- |----tableapi(table&sql与中件间的使用示例模块)
- |----web(获取flink算子计算后的存储结果,提供给前端展示模块)
connectors模块
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">
- <parent>
- <groupId>com.flink.examples</groupId>
- <artifactId>flink-examples</artifactId>
- <version>1.0-SNAPSHOT</version>
- </parent>
- <modelVersion>4.0.0</modelVersion>
- <artifactId>connectors</artifactId>
-
- <dependencies>
- <!-- Flink jdbc依赖 -->
- <dependency>
- <groupId>org.apache.flink</groupId>
- <artifactId>flink-jdbc_2.11</artifactId>
- <version>1.10.1</version>
- </dependency>
-
- <!-- mysql驱动包 -->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.47</version>
- </dependency>
- <!-- kafka依赖 -->
- <dependency>
- <groupId>org.apache.flink</groupId>
- <artifactId>flink-connector-kafka_2.11</artifactId>
- <version>${flink.version}</version>
- </dependency>
- <!-- redis依赖 -->
- <dependency>
- <groupId>org.apache.bahir</groupId>
- <artifactId>flink-connector-redis_2.11</artifactId>
- <version>1.0</version>
- </dependency>
- <!-- rabbitMq依赖 -->
- <dependency>
- <groupId>org.apache.flink</groupId>
- <artifactId>flink-connector-rabbitmq_2.11</artifactId>
- <version>${flink.version}</version>
- </dependency>
- <!-- elasticsearch6依赖 -->
- <dependency>
- <groupId>org.apache.flink</groupId>
- <artifactId>flink-connector-elasticsearch6_2.11</artifactId>
- <version>${flink.version}</version>
- </dependency>
- <dependency>
- <groupId>org.apache.flink</groupId>
- <artifactId>flink-sql-connector-elasticsearch6_2.11</artifactId>
- <version>${flink.version}</version>
- </dependency>
- </dependencies>
-
- </project>

stream模块
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">
- <parent>
- <artifactId>flink-examples</artifactId>
- <groupId>com.flink.examples</groupId>
- <version>1.0-SNAPSHOT</version>
- </parent>
- <modelVersion>4.0.0</modelVersion>
- <artifactId>stream</artifactId>
- <dependencies>
- <dependency>
- <groupId>org.apache.flink</groupId>
- <artifactId>flink-table-common</artifactId>
- <version>${flink.version}</version>
- <!--<scope>provided</scope>-->
- </dependency>
- </dependencies>
-
- </project>

项目全局示例图
示例数据类
后续所有算子演示的数据,均来自此类来提供公共数据。
- import org.apache.flink.api.java.tuple.Tuple3;
- import java.util.Arrays;
- import java.util.List;
-
- /**
- * @Description 公共示例数据
- */
- public class DataSource {
-
- /**
- * 示例数据集合
- * Tuple3 是一个固定3个属性变量的实体类,分别用f0,f1,f2表示三个构造传参与变量
- * @return
- */
- public static List<Tuple3<String,String,Integer>> getTuple3ToList(){
- //Tuple3<f0,f1,f2> = Tuple3<姓名,性别(man男,girl女),年龄>
- return Arrays.asList(
- new Tuple3<>("张三", "man", 20),
- new Tuple3<>("李四", "girl", 24),
- new Tuple3<>("王五", "man", 29),
- new Tuple3<>("刘六", "girl", 32),
- new Tuple3<>("伍七", "girl", 18),
- new Tuple3<>("吴八", "man", 30)
- );
- }
-
- }

源码下载
Gitee:flink-examples: 基于flink.1.11.1版本的工程示例,此示例包含大部份算子、窗口、中间件连接器、tables&sql的用法,适合新人学习使用;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。