Flink集群环境搭建
Flink简介
官网下载Flink
解压文件
将下载好的压缩文件上传至服务器
scp -p 端口号 文件名称(flink-1.9.2-bin-scala_2.11.tgz) 登录名@IP地址: 服务器中文件上传位置
tar -zvf flink-1.9.2-bin-scala_2.11.tgz
修改配置文件
vim flink-conf.yaml
配置集群从节点
需要地址映射配置完成后才可以启动成功(名字自己取)
配置完成后使用 scp -r 将解压好的文件夹放入其他三台机器
设置环境变量
vim ~/.bashrc
配置完成后执行 source ~/.bashrc 重新加载资源
启动测试
1.主节点启动
2.从节点
其他两个从节点均启动成功则证明搭建完成
完成后登陆验证
Linux相关操作
关闭防火墙及SELinux
CentOS7关闭防火墙相关操作
CentOS7关闭SELinux相关操作
配置host地址映射
vim /etc/hosts (四台机器均要配置)
systemctl restart network 重启网卡
设置免密登录
免密登录原理:将本机公钥放入需免密登录的服务器的.ssh文件中 并将 id_dsa.pub(公钥文件)输出到 authorized_keys 文件中
生成.ssh 文件夹
ssh localhost 命令生成
安装所需相关文件
yum install -y nc
编写测试代码
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>org.example</groupId>
<artifactId>flink</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<flink-version>1.9.2</flink-version>
<scala-version>2.11.8</scala-version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-scala_2.11</artifactId>
<version>${flink-version}</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-streaming-scala_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.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala-version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 在maven项目中既有java又有scala代码时配置 maven-scala-plugin 插件打包时可以将两类代码一起打包 -->
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<version>2.15.2</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- maven 打jar包需要插件 -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<!-- 设置false后是去掉 MySpark-1.0-SNAPSHOT-jar-with-dependencies.jar 后的 “-jar-with-dependencies” -->
<!--<appendAssemblyId>false</appendAssemblyId>-->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
测试代码
package com.zqy
import org.apache.flink.api.scala._
import org.apache.flink.streaming.api.scala.{DataStream, StreamExecutionEnvironment}
/**
* @author zqy
* @date 2021/2/1 4:27 下午
*/
object WordCount {
def main(args: Array[String]): Unit = {
// 准备换件
var env = StreamExecutionEnvironment.getExecutionEnvironment
env.setParallelism(1)
val initStream: DataStream[String] = env.socketTextStream("node01", 8888)
val wordStream = initStream.flatMap(_.split(""))
val pairStream = wordStream.map((_, 1))
val keyByStream = pairStream.keyBy(fields = 0)
val restStream = keyByStream.sum(position = 1)
restStream.print()
env.execute("first flink job")
}
}
将测试代码部署到服务器
开启被监听端口
nc -lk 8888
第一种部署方式
第二种部署方式 也可以通过web端上传
测试结果
输入
结果