Python REPL
Flink comes with an integrated interactive Python Shell.It can be used in a local setup as well as in a cluster setup.See the local setup page for more information about how to setup a local Flink.You can also build a local setup from source.
Note The Python Shell will run the command “python”. Please run the following command to confirm that the command “python” in current environment points to Python 3.5+:
$ python --version
# the version printed here must be 3.5+
Note Using Python UDF in Python Shell requires apache-beam 2.15.0. Run the following command to confirm that it meets the requirements before run the Shell in local mode:
$ python -m pip install apache-beam==2.15.0
To use the shell with an integrated Flink cluster just execute:
bin/pyflink-shell.sh local
in the root directory of your binary Flink directory. To run the Shell on acluster, please see the Setup section below.
Usage
The shell only supports Table API currently.The Table Environments are automatically prebound after startup. Use “bt_env” and “st_env” to access BatchTableEnvironment and StreamTableEnvironment respectively.
Table API
The example below is a simple program in the Python shell:
>>> import tempfile
>>> import os
>>> import shutil
>>> sink_path = tempfile.gettempdir() + '/streaming.csv'
>>> if os.path.exists(sink_path):
... if os.path.isfile(sink_path):
... os.remove(sink_path)
... else:
... shutil.rmtree(sink_path)
>>> s_env.set_parallelism(1)
>>> t = st_env.from_elements([(1, 'hi', 'hello'), (2, 'hi', 'hello')], ['a', 'b', 'c'])
>>> st_env.connect(FileSystem().path(sink_path))\
... .with_format(OldCsv()
... .field_delimiter(',')
... .field("a", DataTypes.BIGINT())
... .field("b", DataTypes.STRING())
... .field("c", DataTypes.STRING()))\
... .with_schema(Schema()
... .field("a", DataTypes.BIGINT())
... .field("b", DataTypes.STRING())
... .field("c", DataTypes.STRING()))\
... .register_table_sink("stream_sink")
>>> t.select("a + 1, b, c")\
... .insert_into("stream_sink")
>>> st_env.execute("stream_job")
>>> # If the job runs in local mode, you can exec following code in Python shell to see the result:
>>> with open(sink_path, 'r') as f:
... print(f.read())
>>> import tempfile
>>> import os
>>> import shutil
>>> sink_path = tempfile.gettempdir() + '/batch.csv'
>>> if os.path.exists(sink_path):
... if os.path.isfile(sink_path):
... os.remove(sink_path)
... else:
... shutil.rmtree(sink_path)
>>> b_env.set_parallelism(1)
>>> t = bt_env.from_elements([(1, 'hi', 'hello'), (2, 'hi', 'hello')], ['a', 'b', 'c'])
>>> bt_env.connect(FileSystem().path(sink_path))\
... .with_format(OldCsv()
... .field_delimiter(',')
... .field("a", DataTypes.BIGINT())
... .field("b", DataTypes.STRING())
... .field("c", DataTypes.STRING()))\
... .with_schema(Schema()
... .field("a", DataTypes.BIGINT())
... .field("b", DataTypes.STRING())
... .field("c", DataTypes.STRING()))\
... .register_table_sink("batch_sink")
>>> t.select("a + 1, b, c")\
... .insert_into("batch_sink")
>>> bt_env.execute("batch_job")
>>> # If the job runs in local mode, you can exec following code in Python shell to see the result:
>>> with open(sink_path, 'r') as f:
... print(f.read())
Setup
To get an overview of what options the Python Shell provides, please use
bin/pyflink-shell.sh --help
Local
To use the shell with an integrated Flink cluster just execute:
bin/pyflink-shell.sh local
Remote
To use it with a running cluster, please start the Python shell with the keyword remote
and supply the host and port of the JobManager with:
bin/pyflink-shell.sh remote <hostname> <portnumber>
Yarn Python Shell cluster
The shell can deploy a Flink cluster to YARN, which is used exclusively by theshell.The shell deploys a new Flink cluster on YARN and connects thecluster. You can also specify options for YARN cluster such as memory forJobManager, name of YARN application, etc.
For example, to start a Yarn cluster for the Python Shell with two TaskManagersuse the following:
bin/pyflink-shell.sh yarn -n 2
For all other options, see the full reference at the bottom.
Yarn Session
If you have previously deployed a Flink cluster using the Flink Yarn Session,the Python shell can connect with it using the following command:
bin/pyflink-shell.sh yarn
Full Reference
Flink Python Shell
Usage: pyflink-shell.sh [local|remote|yarn] [options] <args>...
Command: local [options]
Starts Flink Python shell with a local Flink cluster
usage:
-h,--help Show the help message with descriptions of all options.
Command: remote [options] <host> <port>
Starts Flink Python shell connecting to a remote cluster
<host>
Remote host name as string
<port>
Remote port as integer
usage:
-h,--help Show the help message with descriptions of all options.
Command: yarn [options]
Starts Flink Python shell connecting to a yarn cluster
usage:
-h,--help Show the help message with descriptions of
all options.
-jm,--jobManagerMemory <arg> Memory for JobManager Container with
optional unit (default: MB)
-nm,--name <arg> Set a custom name for the application on
YARN
-qu,--queue <arg> Specify YARN queue.
-s,--slots <arg> Number of slots per TaskManager
-tm,--taskManagerMemory <arg> Memory per TaskManager Container with
optional unit (default: MB)
-h | --help
Prints this usage text