flink脚本系列 SQL Gateway脚本
脚本主要功能
该脚本用于启动和停止SQL Gateway。以下是脚本的主要功能:
- 提供了脚本使用说明的usage函数。
- 解析命令行参数,包括启动或停止命令、帮助参数等。
- 根据不同的命令执行相应的操作,如以后台或前台方式启动SQL Gateway,停止SQL Gateway等。
- 引入其他依赖脚本和配置文件,获取相关配置信息。
脚本加上中文注释
#!/bin/bash
# 定义脚本使用说明函数
function usage() {
echo "Usage: sql-gateway.sh [start|start-foreground|stop|stop-all] [args]"
echo " commands:"
echo " start - Run a SQL Gateway as a daemon"
echo " start-foreground - Run a SQL Gateway as a console application"
echo " stop - Stop the SQL Gateway daemon"
echo " stop-all - Stop all the SQL Gateway daemons"
echo " -h | --help - Show this help message"
}
################################################################################
# 从 "flink" bash 脚本中采用的部分
################################################################################
target="$0"
# 如果可执行文件直接进行了符号链接,通过迭代解析符号链接的正确路径
iteration=0
while [ -L "$target" ]; do
if [ "$iteration" -gt 100 ]; then
echo "Cannot resolve path: You have a cyclic symlink in $target."
break
fi
ls=`ls -ld -- "$target"`
target=`expr "$ls" : '.* -> \(.*\)$'`
iteration=$((iteration + 1))
done
# 转换相对路径为绝对路径
bin=`dirname "$target"`
# 导入flink配置
. "$bin"/config.sh
if [ "$FLINK_IDENT_STRING" = "" ]; then
FLINK_IDENT_STRING="$USER"
fi
################################################################################
# SQL gateway 特定逻辑
################################################################################
ENTRYPOINT=sql-gateway
# 如果命令行参数包含--help或-h,则打印使用说明并退出
if [[ "$1" = *--help ]] || [[ "$1" = *-h ]]; then
usage
exit 0
fi
STARTSTOP=$1
# 如果未提供启动或停止命令,则默认为"start"
if [ -z "$STARTSTOP" ]; then
STARTSTOP="start"
fi
# 如果提供的命令不是有效的启动或停止命令,则打印使用说明并退出
if [[ $STARTSTOP != "start" ]] && [[ $STARTSTOP != "start-foreground" ]] && [[ $STARTSTOP != "stop" ]] && [[ $STARTSTOP != "stop-all" ]]; then
usage
exit 1
fi
# 如果命令是"start*",并且包含--help或-h参数,则以前台方式打印帮助信息到控制台并退出
if [[ "$STARTSTOP" = start* ]] && ( [[ "$*" = *--help* ]] || [[ "$*" = *-h* ]] ); then
FLINK_TM_CLASSPATH=`constructFlinkClassPath`
SQL_GATEWAY_CLASSPATH=`findSqlGatewayJar`
"$JAVA_RUN" -classpath "`manglePathList "$FLINK_TM_CLASSPATH:$SQL_GATEWAY_CLASSPATH:$INTERNAL_HADOOP_CLASSPATHS"`" org.apache.flink.table.gateway.SqlGateway "${@:2}"
exit 0
fi
# 如果命令是"start-foreground",则以前台方式执行flink-console.sh脚本启动SQL Gateway
if [[ $STARTSTOP == "start-foreground" ]]; then
exec "${FLINK_BIN_DIR}"/flink-console.sh $ENTRYPOINT "${@:2}"
else
# 否则,使用flink-daemon.sh脚本以后台方式启动或停止SQL Gateway
"${FLINK_BIN_DIR}"/flink-daemon.sh $STARTSTOP $ENTRYPOINT "${@:2}"
fi
该脚本多种用法及其代码示例
该脚本支持以下多种用法和相应的代码示例:
-
启动SQL Gateway作为守护进程:
./sql-gateway.sh start
-
启动SQL Gateway作为控制台应用程序:
./sql-gateway.sh start-foreground
-
停止SQL Gateway守护进程:
./sql-gateway.sh stop
-
停止所有SQL Gateway守护进程:
./sql-gateway.sh stop-all
-
打印帮助信息:
./sql-gateway.sh --help
官方链接
您可以在以下链接中找到有关该脚本的更多信息: