文章目录
- FLINK单机版安装部署
- 高于1.9.3需要修改配置文件flink-conf.yaml(低于1.9.3可以跳过)
- linux启动集群
- windows下启动
- Flink提交任务方式
- 命令方式提交
- 运行WordCount任务
- 运行streaming任务
- web页面提交任务
- 取消Job
- java: Compilation failed: internal java compiler error
- 高版本启动脚本
FLINK单机版安装部署
官网下载Downloads | Apache Flink
下载文件历史版本的1.9.3
https://archive.apache.org/dist/flink/flink-1.9.3/flink-1.9.3-bin-scala_2.11.tgz
# 下载Flink安装包(笔者这里测试使用Flink1.9版本,其它版本下载目录 https://flink.apache.org/downloads.html)
wget https://archive.apache.org/dist/flink/flink-1.9.3/flink-1.9.3-bin-scala_2.11.tgz
# 解压
tar -xzvf flink-1.9.3-bin-scala_2.11.tgz
netCat是linux下自带的支持TCP、UDP、Unix域协议套接字小工具,nc命令允许你创建一个连接、侦听另一个连接和传输数据。在windows环境中也可以使用
使用命令:nc -lk port号
说明: -l listen监听某个端口 k:保持住当前的连接,程序终止的话,当前server不断开
功能:启动了一个可以发送socket文本流的服务器,端口:7777
下载地址 netcat 1.11 for Win32/Win64
https://eternallybored.org/misc/netcat/
注意:
服务启动依赖jdk环境变量(确保已经安装了Java_jdk)
加上环境变量
vi source /etc/profile
export JAVA_HOME=/opt/zulu_jdk11.68.17
export PATH=$JAVA_HOME/bin:$PATH
export PATH=/opt/flink-1.9.3/bin:$PATAT
source /etc/profile
高于1.9.3需要修改配置文件flink-conf.yaml(低于1.9.3可以跳过)
修改端口号(默认端口不影响的可以直接跳过端口配置)
进入conf目录下 flink-conf.yaml 修改rest.port
(8081端口过于常用,容易出现端口冲突,笔者这里改为8001)
#rest.port: 8081
rest.port: 8001
windows下需要加入如下配置,否则执行start-cluster.bat会导致TaskManager进程会退出
taskmanager.cpu.cores: 2
taskmanager.memory.task.heap.size: 512m
taskmanager.memory.managed.size: 512m
taskmanager.memory.network.min: 64m
taskmanager.memory.network.max: 64m
加上后会启动JobManager和TaskManager
linux启动集群
进入bin目录执行下面命令
# 启动Flink集群
./start-cluster.sh
# 停止Flink集群
#./stop-cluster.sh
访问Flink UI
,链接http://host:8778/#/overview,查看dashboard (这里的host为部署flink的机器host)
windows下启动
windows当前能支持的最高版本是1.9.3,直接安装就行;更高的版本已经不支持windos启动了,连启动脚本都已经没有
高版本参考如下:仅供参考但是不保证能成功,基本会失败;
cmd命令行启动
flink-1.9.3\bin>.\start-cluster.bat
启动后可以在管理页面看到Available Task Slots有一个可用的slots,如果没有参考上面的flink-config.yaml缺少windows下配置项
Flink提交任务方式
1 使用Web页面Submit New Job方式提交(生产环境基本以此类方式为主)
2 使用命令行方式提交
命令方式提交
Flink安装包自带了测试样例,
运行WordCount任务
flink-\flink-1.9.3\bin> ./flink run D:\workspace\IDE\flink-1.9.3\examples\batch\WordCount.jar
运行streaming任务
examples\streaming\SocketWindowWordCount.jar
该样例为从一个ip端口获取数据,并进行单词数量统计,用来模拟数据流
运行样例分为本地运行命令行和UI界面提交运行,也就是分为本地模式和服务器模式两种
先启动端口TCP字符流
nc -lp 8888
./flink sun -c org.apache.flink.streaming.examples.socket.SocketWindowWordCount -p 1 D:\workspace\IDE\flink-1.9.3\examples\streaming\SocketWindowWordCount.jar --host localhost --port 8888
-m:指定主机名后面的端口为 JobManager的 REST 通信端口,而不是 RPC的端口,RPC通信端口是 6123(在提交任务时,是通过 REST 端口号(HTTP端口号),将任务上传到 JobManager.)
-c: 执行的主类,指定 main 方法的全类名
-p:运行的slot实例数=最大为当前所有实例数
-jar包路径
-host port :外部参数这里指参数接收的ip和端口号
在管理页面的submit new job中上传 任务jar即可
http://localhost:8001/#/submit
–hostname localhost --port 8888
这里我们依然以example中的WordCount.jar为例提交,点击该明细会出现执行的main文件路径合参数配置,默认即可,点击submit提交
job样例源码
https://gitee.com/jian_yang_lv
flink视频教程
https://www.bilibili.com/video/BV1eg4y1V7AN?p=1&vd_source=439eb7a06dc0a72103551f415ea81556
web页面提交任务
使用Web页面Submit New Job方式提交,这里依然以wordCount为例
填写任务main全路径,以及slot槽数
查看运行情况
WordCount 计数完成后,我们在 TaskManager 配置页面,通过 stdout 可以查看到结果信息
取消Job
因为Flink任务是实时的,启动就不会停止,除非出错或者人为取消Job
JOB->Running job ->明细中点击取消按钮
java: Compilation failed: internal java compiler error
https://blog.csdn.net/weixin_42923363/article/details/126698963
高版本启动脚本
Windows下是没有启动脚本的,这里贴上CMD启动脚本
参考资料:https://blog.csdn.net/xuexijava85/article/details/114803489
flink.bat
::###############################################################################
:: Licensed to the Apache Software Foundation (ASF) under one
:: or more contributor license agreements. See the NOTICE file
:: distributed with this work for additional information
:: regarding copyright ownership. The ASF licenses this file
:: to you under the Apache License, Version 2.0 (the
:: "License"); you may not use this file except in compliance
:: with the License. You may obtain a copy of the License at
::
:: http://www.apache.org/licenses/LICENSE-2.0
::
:: Unless required by applicable law or agreed to in writing, software
:: distributed under the License is distributed on an "AS IS" BASIS,
:: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
:: See the License for the specific language governing permissions and
:: limitations under the License.
::###############################################################################
@echo off
setlocal
SET bin=%~dp0
SET FLINK_HOME=%bin%..
SET FLINK_LIB_DIR=%FLINK_HOME%\lib
SET FLINK_PLUGINS_DIR=%FLINK_HOME%\plugins
SET JVM_ARGS=-Xmx512m
SET FLINK_JM_CLASSPATH=%FLINK_LIB_DIR%\*
java %JVM_ARGS% -cp "%FLINK_JM_CLASSPATH%"; org.apache.flink.client.cli.CliFrontend %*
endlocal
start-cluster.bat
这里,我把start-cluster.bat放在bin文件夹同级目录中,而没有放在bin目录里面
::###############################################################################
:: Licensed to the Apache Software Foundation (ASF) under one
:: or more contributor license agreements. See the NOTICE file
:: distributed with this work for additional information
:: regarding copyright ownership. The ASF licenses this file
:: to you under the Apache License, Version 2.0 (the
:: "License"); you may not use this file except in compliance
:: with the License. You may obtain a copy of the License at
::
:: http://www.apache.org/licenses/LICENSE-2.0
::
:: Unless required by applicable law or agreed to in writing, software
:: distributed under the License is distributed on an "AS IS" BASIS,
:: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
:: See the License for the specific language governing permissions and
:: limitations under the License.
:: author:zjcjava time:2023/05/24
::###############################################################################
@echo off
setlocal EnableDelayedExpansion
::#####这里我直接使用根目录拼接config和bin目录的path路径
SET FLINK_HOME=%cd%
SET bin==%FLINK_HOME%\bin
::### SET FLINK_HOME=%bin%..
echo ###############################################################################
echo FLINK_HOME %FLINK_HOME%
SET FLINK_LIB_DIR=%FLINK_HOME%\lib
SET FLINK_PLUGINS_DIR=%FLINK_HOME%\plugins
SET FLINK_CONF_DIR=%FLINK_HOME%\conf
SET FLINK_LOG_DIR=%FLINK_HOME%\log
echo FLINK_CONF_DIR:%FLINK_CONF_DIR%
echo ###############################################################################
SET JVM_ARGS=-Xms1024m -Xmx1024m
SET FLINK_CLASSPATH=%FLINK_LIB_DIR%\*
SET logname_jm=flink-%username%-jobmanager.log
SET logname_tm=flink-%username%-taskmanager.log
SET log_jm=%FLINK_LOG_DIR%\%logname_jm%
SET log_tm=%FLINK_LOG_DIR%\%logname_tm%
SET outname_jm=flink-%username%-jobmanager.out
SET outname_tm=flink-%username%-taskmanager.out
SET out_jm=%FLINK_LOG_DIR%\%outname_jm%
SET out_tm=%FLINK_LOG_DIR%\%outname_tm%
SET log_setting_jm=-Dlog.file="%log_jm%" -Dlogback.configurationFile=file:"%FLINK_CONF_DIR%/logback.xml" -Dlog4j.configuration=file:"%FLINK_CONF_DIR%/log4j.properties"
SET log_setting_tm=-Dlog.file="%log_tm%" -Dlogback.configurationFile=file:"%FLINK_CONF_DIR%/logback.xml" -Dlog4j.configuration=file:"%FLINK_CONF_DIR%/log4j.properties"
:: Log rotation (quick and dirty)
CD "%FLINK_LOG_DIR%"
for /l %%x in (5, -1, 1) do (
SET /A y = %%x+1
RENAME "%logname_jm%.%%x" "%logname_jm%.!y!" 2> nul
RENAME "%logname_tm%.%%x" "%logname_tm%.!y!" 2> nul
RENAME "%outname_jm%.%%x" "%outname_jm%.!y!" 2> nul
RENAME "%outname_tm%.%%x" "%outname_tm%.!y!" 2> nul
)
RENAME "%logname_jm%" "%logname_jm%.0" 2> nul
RENAME "%logname_tm%" "%logname_tm%.0" 2> nul
RENAME "%outname_jm%" "%outname_jm%.0" 2> nul
RENAME "%outname_tm%" "%outname_tm%.0" 2> nul
DEL "%logname_jm%.6" 2> nul
DEL "%logname_tm%.6" 2> nul
DEL "%outname_jm%.6" 2> nul
DEL "%outname_tm%.6" 2> nul
for %%X in (java.exe) do (set FOUND=%%~$PATH:X)
if not defined FOUND (
echo java.exe was not found in PATH variable
goto :eof
)
echo Starting a local cluster with one JobManager process and one TaskManager process.
echo You can terminate the processes via CTRL-C in the spawned shell windows.
echo Web interface by default on http://localhost:8081/.
start java %JVM_ARGS% %log_setting_jm% -cp "%FLINK_CLASSPATH%"; org.apache.flink.runtime.entrypoint.StandaloneSessionClusterEntrypoint --configDir "%FLINK_CONF_DIR%" > "%out_jm%" 2>&1
start java %JVM_ARGS% %log_setting_tm% -cp "%FLINK_CLASSPATH%"; org.apache.flink.runtime.taskexecutor.TaskManagerRunner --configDir "%FLINK_CONF_DIR%" > "%out_tm%" 2>&1
endlocal