1、下载
首先下载Flink :去官网或者国内的镜像网站 (flink.apache.org/zh/download…)
我下载的这个版本:
解压:目录结构如下
看下conf 包下 的配置文件 flink-conf.yaml
#==============================================================================
# Common 【通用配置如下】
#==============================================================================
# The external address of the host on which the JobManager runs and can be
# reached by the TaskManagers and any clients which want to connect. This setting
# is only used in Standalone mode and may be overwritten on the JobManager side
# by specifying the --host <hostname> parameter of the bin/jobmanager.sh executable.
# In high availability mode, if you use the bin/start-cluster.sh script and setup
# the conf/masters file, this will be taken care of automatically. Yarn
# automatically configure the host name based on the hostname of the node where the
# JobManager runs.
jobmanager.rpc.address: localhost
# The RPC port where the JobManager is reachable.
jobmanager.rpc.port: 6123
# The total process memory size for the JobManager.
#
# Note this accounts for all memory usage within the JobManager process, including JVM metaspace and other overhead.
jobmanager.memory.process.size: 1600m
# The total process memory size for the TaskManager.
#
# Note this accounts for all memory usage within the TaskManager process, including JVM metaspace and other overhead.
taskmanager.memory.process.size: 1728m
# To exclude JVM metaspace and overhead, please, use total Flink memory size instead of 'taskmanager.memory.process.size'.
# It is not recommended to set both 'taskmanager.memory.process.size' and Flink memory.
#
# taskmanager.memory.flink.size: 1280m
# The number of task slots that each TaskManager offers. Each slot runs one parallel pipeline.
taskmanager.numberOfTaskSlots: 1
# The parallelism used for programs that did not specify and other parallelism.
parallelism.default: 1
# The default file system scheme and authority.
#
# By default file paths without scheme are interpreted relative to the local
# root file system 'file:///'. Use this to override the default and interpret
# relative paths relative to a different file system,
# for example 'hdfs://mynamenode:12345'
#
# fs.default-scheme
#==============================================================================
# Rest & web frontend 【Rest 接口和网页前端配置】
#==============================================================================
# The port to which the REST client connects to. If rest.bind-port has
# not been specified, then the server will bind to this port as well.
#
#rest.port: 8081
# The address to which the REST client will connect to
#
#rest.address: 0.0.0.0
# Port range for the REST and web server to bind to.
#
#rest.bind-port: 8080-8090
# The address that the REST & web server binds to
#
#rest.bind-address: 0.0.0.0
# Flag to specify whether job submission is enabled from the web-based
# runtime monitor. Uncomment to disable.
#web.submit.enable: false
# Flag to specify whether job cancellation is enabled from the web-based
# runtime monitor. Uncomment to disable.
#web.cancel.enable: false
2.1、启动Flink (Standalone模式)
进入到flink的目录下, ./bin/start-cluster.sh
3、访问Web UI
启动完成后在浏览器输入 【http://localhost:8081/ 】访问。
默认的端口,和网页UI 配置在上面的 conf 的配置文件里面。
4、WEB UI 提交任务
1、选择UI 页面的左侧菜单 submit new job, 然后在点击 add new 就能选择电脑的文件。
2、任务以jar包的方式提交。所以我们打包一下,我们刚才的(上一篇)的word count 项目
3、选择jar 上传。
4、配置运行参数
-
Entry Class (入口类)
程序的入口,指定入口类(类的全限制名)
-
Parallelism (并行度)
设置Job并行度。
-
Paogram Arguments (程序运行参数)
程序启动参数,例如
--host localhost --port 7777 -
savepoint path (保存点路径)
savepoint是通过checkpoint机制为streaming job创建的一致性快照,比如数据源offset,状态等。(savepoint可以理解为手动备份,而checkpoint为自动备份)
Ps:并行度优先级(从上到下优先级递减)
- 代码中算子
setParallelism() 【在代码中每一步计算算子中指定的并行度优先级最高】 ExecutionEnvironment env.setMaxParallelism()。【其次就是代码上整个环境设置的并行度 第二高】- 设置的Job并行度 【第三就是发布job 的配置处指定的】
- 集群conf配置文件中的
parallelism.default。【最后才是集群配置文件中的默认值】
提交一下,失败了 ,是资源不够用。需要用到的solt 数量,比资源提供的大。
(Ps : 资源不够是一个问题,还有其他的。scoket 没开。 nc -lk 7777)
概览这里才一个,Slot ,程序跑按照设置 并行度3 ,需要7个。
5、修改配置文件,设置slot 数量。
先把集群停一下。 ./bin/stop-cluster.sh
修改配置文件 taskmanager.numberOfTaskSlots
taskmanager.numberOfTaskSlots: 4
重新启动集群。 ./bin/start-cluster.sh。 上传jar。跑起来如下。
6、然后利用scoket 模拟发生实时数据。
nc -lk 7777
- ps:socket等特殊的IO操作,本身不能并行处理,并行度只能是1
7、查看打印日志
在task manager 的 stdout 查看日志
可以看出来输出的顺序并不是和输入的字符串严格相同的,因为是多个线程并行处理的。
5、命令行提交JOB
1、查看已提交的所有job
bin/flink list
zhangjiuliang@ZBMac-C02CW08SM flink-1.14.0 % bin/flink list
Waiting for response...
No running jobs.
No scheduled jobs.
2、提交job
-c指定入口类-p指定job的并行度
bin/flink run -c <入口类> -p <并行度> <jar包路径> <启动参数>
$ bin/flink run -c wc.StreamWordCount -p 3 /tmp/Flink_Tutorial-1.0-SNAPSHOT.jar --host localhost --port 7777
Job has been submitted with JobID 33a5d1f00688a362837830f0b85fd75e
3、取消job
bin/flink cancel <Job的ID>
$ bin/flink cancel 30d9dda946a170484d55e41358973942
Cancelling job 30d9dda946a170484d55e41358973942.
Cancelled job 30d9dda946a170484d55e41358973942.
注:Total Task Slots只要不小于Job中Parallelism最大值即可。
eg:这里我配置文件设置taskmanager.numberOfTaskSlots: 4,实际Job运行时总Tasks显示9,但是里面具体4个任务步骤分别需求(1,3,3,2)数量的Tasks,4>3,满足最大的Parallelism即可运行成功。
2.2 Yarn 模式部署
以Yarn模式部署Flink任务时,要求Flink是有 Hadoop 支持的版本,Hadoop 环境需要保证版本在 2.2 以上,并且集群中安装有 HDFS 服务。
1、Flink on Yarn
Flink提供了两种在yarn上运行的模式,分别为Session-Cluster和Per-Job-Cluster模式。
Session-Cluster 模式需要先启动集群,然后再提交作业,接着会向 yarn 申请一块空间后,资源永远保持不变。如果资源满了,下一个作业就无法提交,只能等到 yarn 中的其中一个作业执行完成后,释放了资源,下个作业才会正常提交。所有作业共享 Dispatcher 和 ResourceManager;共享资源;适合规模小执行时间短的作业。
在 yarn 中初始化一个 flink 集群,开辟指定的资源,以后提交任务都向这里提交。这个 flink 集群会常驻在 yarn 集群中,除非手工停止。
一个 Job 会对应一个集群,每提交一个作业会根据自身的情况,都会单独向 yarn 申请资源,直到作业执行完成,一个作业的失败与否并不会影响下一个作业的正常提交和运行。独享 Dispatcher 和 ResourceManager,按需接受资源申请;适合规模大长时间运行的作业。
每次提交都会创建一个新的 flink 集群,任务之间互相独立,互不影响,方便管理。任务执行完成之后创建的集群也会消失。
集群启动步骤可以参考博客:blog.csdn.net/suyebiubiu/…