Flink Python REPL(pyflink-shell)实战:本地/远程/YARN 三种启动方式 + Table API 交互开发流程

1、Python Shell 的定位与限制

  • 目前 只支持 Table API(不是 DataStream API 的交互式 REPL)

  • Shell 启动后会自动预绑定 TableEnvironment:

    • bt_env:BatchTableEnvironment
    • st_env:StreamTableEnvironment

Shell 会调用系统里的 python 命令,所以你的 Python 环境必须满足 PyFlink 运行要求(依赖、Python 版本、可能的 Java/类路径等以 Table API 安装指引为准)。

最快的本地体验方式:

bash 复制代码
# 安装 PyFlink
python -m pip install apache-flink

# 启动本地 shell(带集成 Flink 集群)
pyflink-shell.sh local

3、启动方式总览:local / remote / yarn

先记住一句话:
local (本地自带集群)适合学习/验证,remote (连已有集群)适合线上排障/临时跑作业,yarn(拉起独占集群)适合在 YARN 上做交互实验且不影响别的作业。

3.1 查看所有参数

bash 复制代码
pyflink-shell.sh --help

3.2 Local:本地集成集群

bash 复制代码
pyflink-shell.sh local

特点:

  • 自动启动一个本地 Flink 集群
  • 适合快速试 Table API/DDL/connector 配置

3.3 Remote:连接运行中的集群(Standalone/K8s 端口映射等)

bash 复制代码
pyflink-shell.sh remote <hostname> <portnumber>

这里的 <portnumber> 指 JobManager 的端口(对应你集群暴露的 JM RPC/REST 方式,按部署模式而定)。

bash 复制代码
# 例如:起 2 个 TaskManager
pyflink-shell.sh yarn -n 2

你可以额外指定:

  • -jm / --jobManagerMemory:JM 内存
  • -tm / --taskManagerMemory:TM 内存
  • -s / --slots:每个 TM slot 数
  • -nm / --name:YARN 应用名
  • -qu / --queue:YARN 队列

如果你之前已经用 Flink YARN Session 部署过集群,Shell 可以直接连接:

bash 复制代码
pyflink-shell.sh yarn

4、交互开发:Table API 一个最小闭环示例(流式)

下面示例演示:构造表 → 定义 filesystem sink → 写出 CSV → 本地读取结果。

python 复制代码
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.create_temporary_table(
    "stream_sink",
    TableDescriptor.for_connector("filesystem")
        .schema(
            Schema.new_builder()
                .column("a", DataTypes.BIGINT())
                .column("b", DataTypes.STRING())
                .column("c", DataTypes.STRING())
                .build()
        )
        .option("path", sink_path)
        .format(
            FormatDescriptor.for_format("csv")
                .option("field-delimiter", ",")
                .build()
        )
        .build()
)

t.select("a + 1, b, c").execute_insert("stream_sink").wait()

# 仅本地模式下:读取输出文件查看结果
with open(os.path.join(sink_path, os.listdir(sink_path)[0]), 'r') as f:
    print(f.read())

你在 shell 里最常用的操作模式就是这种:

  • from_elements 快速造数据
  • create_temporary_table 快速声明 sink/source
  • execute_insert(...).wait() 等待作业完成并查看结果

5、怎么选模式:一张"实用决策表"

  • 想快速验证 Table API/DDL、写个 demo:用 local
  • 想对接线上真实 connector、看真实数据但不想部署新集群:用 remote
  • 想在 YARN 上做实验但不影响现有 YARN Session:用 yarn -n ... 拉独占集群
  • 已经有 YARN Session 集群:用 pyflink-shell.sh yarn 直接连
相关推荐
明月_清风3 小时前
Python 内存手术刀:sys.getrefcount 与引用计数的生死时速
后端·python
明月_清风3 小时前
Python 消失的内存:为什么 list=[] 是新手最容易踩的“毒苹果”?
后端·python
Flittly18 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(3)TodoWrite (待办写入)
python·agent
千寻girling1 天前
一份不可多得的 《 Django 》 零基础入门教程
后端·python·面试
databook1 天前
探索视觉的边界:用 Manim 重现有趣的知觉错觉
python·动效
明月_清风1 天前
Python 性能微观世界:列表推导式 vs for 循环
后端·python
明月_清风1 天前
Python 性能翻身仗:从 O(n) 到 O(1) 的工程实践
后端·python
helloweilei2 天前
python 抽象基类
python
用户8356290780512 天前
Python 实现 PPT 转 HTML
后端·python
zone77392 天前
004:RAG 入门-LangChain读取PDF
后端·python·面试