Updated on 2024-04-02 GMT+08:00

Running the Program

  1. Obtain pyflink-kafka.py and insertData2kafka.sql from the sample project flink-examples/pyflink-example/pyflink-kafka.
  2. Package the prepared Python virtual environment by referring to Preparing for Development and Operating Environment and obtain the venv.zip file.

    zip -q -r venv.zip venv/

  3. Log in to the active management node as the root user and upload venv.zip, pyflink-kafka.py, and insertData2kafka.sql files obtained in 1and 2 to the client environment.

    • Per-job: Upload the preceding files to Client installation directory/Flink/flink.
    • yarn-application: Upload the preceding files and the flink-connector-kafka-Actual version number.jar package to Client installation directory/Flink/flink/yarnship.

  4. Change the specific_jars path in pyflink-kafka.py.

    • per-job: Change the path to the actual path of the SQL file, for example, file:///Client installation directory/Flink/flink/lib/flink-connector-kafka-Actual version number.jar.
    • yarn-application: Change to file://"+os.getcwd()+"/../../../../yarnship/flink-connector-kafka-Actual version number.jar.

  5. Change file_path in pyflink-kafka.py.

    • per-job: Change the path to the actual path of the SQL file. For example: Client installation directory/Flink/flink/insertData2kafka.sql
    • yarn-application: Change the path to os.getcwd () + "/../../../../yarnship/insertData2kafka.sql"

  6. Run the following command to specify the running environment:

    export PYFLINK_CLIENT_EXECUTABLE=venv.zip/venv/bin/python3

  7. Run the following command to run the program:

    • Per-job:
      ./bin/flink run --detached -t yarn-per-job -Dyarn.application.name=py_kafka -pyarch venv.zip -pyexec venv.zip/venv/bin/python3 -py pyflink-kafka.py

      Execution result:

    • yarn-application
      ./bin/flink run-application --detached -t yarn-application -Dyarn.application.name=py_kafka -Dyarn.ship-files=/opt/client/Flink/flink/yarnship/ -pyarch yarnship/venv.zip -pyexec venv.zip/venv/bin/python3 -pyclientexec venv.zip/venv/bin/python3 -pyfs yarnship -pym pyflink-kafka

      Execution result: