Step1:下载

为了运行Flink,请提前安装好Java8环境,可通过以下命令检查是否安装成功:

  1. > Java -version
  1. FLink官方下载地址:[https://flink.apache.org/zh/downloads.html](https://flink.apache.org/zh/downloads.html),下载之后解压:
  1. > tar -xzf flink-1.14.2-bin-scala_2.11.tgz
  2. > cd flink-1.14.2-bin-scala_2.11

Step2:启动

Flink自带了一个启动脚本,用于启动本地集群:

  1. > ./bin/start-cluster.sh
  2. Starting cluster.
  3. Starting standalonesession daemon on host.
  4. Starting taskexecutor daemon on host.
  1. 启动之后,通过jps -l命令查看进程是否启动成功。<br />![1643076363(1).png](https://cdn.nlark.com/yuque/0/2022/png/22837646/1643076354636-c21413e1-0fe8-484d-a243-626fad346313.png#clientId=ueff08a38-6bf8-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=59&id=ueb7eb912&margin=%5Bobject%20Object%5D&name=1643076363%281%29.png&originHeight=79&originWidth=911&originalType=binary&ratio=1&rotation=0&showTitle=false&size=10258&status=done&style=none&taskId=u814494ca-8c5c-4ad4-bbb7-140cbee5ef2&title=&width=674.8148624854521)<br />访问localhost:8081,确认界面正常启动。<br />注:Flink在1.9版本之前还提供了Windows版本的bat启动脚本,1.9版本之后不再提供,如若搭建Windows环境,可使用下述脚本:<br />在Flink安装目录的bin目录下,新建名为flink的文件,内容如下:
  1. #!/usr/bin/env bash
  2. ################################################################################
  3. # Licensed to the Apache Software Foundation (ASF) under one
  4. # or more contributor license agreements. See the NOTICE file
  5. # distributed with this work for additional information
  6. # regarding copyright ownership. The ASF licenses this file
  7. # to you under the Apache License, Version 2.0 (the
  8. # "License"); you may not use this file except in compliance
  9. # with the License. You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. ################################################################################
  19. target="$0"
  20. # For the case, the executable has been directly symlinked, figure out
  21. # the correct bin path by following its symlink up to an upper bound.
  22. # Note: we can't use the readlink utility here if we want to be POSIX
  23. # compatible.
  24. iteration=0
  25. while [ -L "$target" ]; do
  26. if [ "$iteration" -gt 100 ]; then
  27. echo "Cannot resolve path: You have a cyclic symlink in $target."
  28. break
  29. fi
  30. ls=`ls -ld -- "$target"`
  31. target=`expr "$ls" : '.* -> \(.*\)$'`
  32. iteration=$((iteration + 1))
  33. done
  34. # Convert relative path to absolute path
  35. bin=`dirname "$target"`
  36. # get flink config
  37. . "$bin"/config.sh
  38. if [ "$FLINK_IDENT_STRING" = "" ]; then
  39. FLINK_IDENT_STRING="$USER"
  40. fi
  41. CC_CLASSPATH=`constructFlinkClassPath`
  42. log=$FLINK_LOG_DIR/flink-$FLINK_IDENT_STRING-client-$HOSTNAME.log
  43. log_setting=(-Dlog.file="$log" -Dlog4j.configuration=file:"$FLINK_CONF_DIR"/log4j-cli.properties -Dlogback.configurationFile=file:"$FLINK_CONF_DIR"/logback.xml)
  44. # Add HADOOP_CLASSPATH to allow the usage of Hadoop file systems
  45. exec $JAVA_RUN $JVM_ARGS "${log_setting[@]}" -classpath "`manglePathList "$CC_CLASSPATH:$INTERNAL_HADOOP_CLASSPATHS"`" org.apache.flink.client.cli.CliFrontend "$@"
  1. 新建名为flink.bat文件,内容如下:
  1. ::###############################################################################
  2. :: Licensed to the Apache Software Foundation (ASF) under one
  3. :: or more contributor license agreements. See the NOTICE file
  4. :: distributed with this work for additional information
  5. :: regarding copyright ownership. The ASF licenses this file
  6. :: to you under the Apache License, Version 2.0 (the
  7. :: "License"); you may not use this file except in compliance
  8. :: with the License. You may obtain a copy of the License at
  9. ::
  10. :: http://www.apache.org/licenses/LICENSE-2.0
  11. ::
  12. :: Unless required by applicable law or agreed to in writing, software
  13. :: distributed under the License is distributed on an "AS IS" BASIS,
  14. :: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. :: See the License for the specific language governing permissions and
  16. :: limitations under the License.
  17. ::###############################################################################
  18. @echo off
  19. setlocal
  20. SET bin=%~dp0
  21. SET FLINK_HOME=%bin%..
  22. SET FLINK_LIB_DIR=%FLINK_HOME%\lib
  23. SET FLINK_PLUGINS_DIR=%FLINK_HOME%\plugins
  24. SET JVM_ARGS=-Xmx512m
  25. SET FLINK_JM_CLASSPATH=%FLINK_LIB_DIR%\*
  26. java %JVM_ARGS% -cp "%FLINK_JM_CLASSPATH%"; org.apache.flink.client.cli.CliFrontend %*
  27. endlocal

Step3:测试

Flink自带了许多示例作业,你可以任选一个快速部署到已搭建的集群上。

  1. > ./bin/flink run examples/streaming/WordCount.jar
  2. > tail log/flink-*-taskexecutor-*.out
  3. (nymph,1)
  4. (in,3)
  5. (thy,1)
  6. (orisons,1)
  7. (be,4)
  8. (all,2)
  9. (my,1)
  10. (sins,1)
  11. (remember,1)
  12. (d,4)
  1. 你也可以通过FlinkWeb UI来监视集群的状态和正在运行的作业。

Step4:停止集群

如果你需要停止正在运行的集群,可通过如下脚本停止:

  1. > ./bin/stop-cluster.sh