pyflink-shell.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/bin/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. bin=`dirname "$0"`
  20. bin=`cd "$bin"; pwd`
  21. . "$bin"/find-flink-home.sh
  22. _FLINK_HOME_DETERMINED=1
  23. . "$FLINK_HOME"/bin/config.sh
  24. FLINK_CLASSPATH=`constructFlinkClassPath`
  25. PYTHON_JAR_PATH=`echo "$FLINK_OPT_DIR"/flink-python*.jar`
  26. PYFLINK_PYTHON="${PYFLINK_PYTHON:-"python"}"
  27. # So that python can find out Flink's Jars
  28. export FLINK_BIN_DIR=$FLINK_BIN_DIR
  29. export FLINK_HOME
  30. # Add pyflink & py4j & cloudpickle to PYTHONPATH
  31. export PYTHONPATH="$FLINK_OPT_DIR/python/pyflink.zip:$PYTHONPATH"
  32. PY4J_ZIP=`echo "$FLINK_OPT_DIR"/python/py4j-*-src.zip`
  33. CLOUDPICKLE_ZIP=`echo "$FLINK_OPT_DIR"/python/cloudpickle-*-src.zip`
  34. export PYTHONPATH="$PY4J_ZIP:$CLOUDPICKLE_ZIP:$PYTHONPATH"
  35. PARSER="org.apache.flink.client.python.PythonShellParser"
  36. function parse_options() {
  37. "${JAVA_RUN}" ${JVM_ARGS} -cp ${FLINK_CLASSPATH}:${PYTHON_JAR_PATH} ${PARSER} "$@"
  38. printf "%d\0" $?
  39. }
  40. # Turn off posix mode since it does not allow process substitution
  41. set +o posix
  42. # If the command has option --help | -h, the script will directly
  43. # run the PythonShellParser program to stdout the help message.
  44. if [[ "$@" =~ '--help' ]] || [[ "$@" =~ '-h' ]]; then
  45. "${JAVA_RUN}" ${JVM_ARGS} -cp ${FLINK_CLASSPATH}:${PYTHON_JAR_PATH} ${PARSER} "$@"
  46. exit 0
  47. fi
  48. OPTIONS=()
  49. while IFS= read -d '' -r ARG; do
  50. OPTIONS+=("$ARG")
  51. done < <(parse_options "$@")
  52. COUNT=${#OPTIONS[@]}
  53. LAST=$((COUNT - 1))
  54. LAUNCHER_EXIT_CODE=${OPTIONS[$LAST]}
  55. # Certain JVM failures result in errors being printed to stdout (instead of stderr), which causes
  56. # the code that parses the output of the launcher to get confused. In those cases, check if the
  57. # exit code is an integer, and if it's not, handle it as a special error case.
  58. if ! [[ ${LAUNCHER_EXIT_CODE} =~ ^[0-9]+$ ]]; then
  59. echo "${OPTIONS[@]}" | head -n-1 1>&2
  60. exit 1
  61. fi
  62. if [[ ${LAUNCHER_EXIT_CODE} != 0 ]]; then
  63. exit ${LAUNCHER_EXIT_CODE}
  64. fi
  65. OPTIONS=("${OPTIONS[@]:0:$LAST}")
  66. export SUBMIT_ARGS=${OPTIONS[@]}
  67. # -i: interactive
  68. # -m: execute shell.py in the zip package
  69. ${PYFLINK_PYTHON} -i -m pyflink.shell