2

我正在尝试使用 PyFlink 将流放入 csv 格式的文件系统中,但是它不起作用。

# stream_to_csv.py
from pyflink.table import EnvironmentSettings, StreamTableEnvironment

env_settings = EnvironmentSettings.new_instance().in_streaming_mode().use_blink_planner().build()
table_env = StreamTableEnvironment.create(environment_settings=env_settings)

table_env.execute_sql("""
    CREATE TABLE datagen (
        id INT,
        data STRING
    ) WITH (
        'connector' = 'datagen',
        'rows-per-second' = '1'
    )
""")

table_env.execute_sql("""
    CREATE TABLE print (
        id INT,
        data STRING
    ) WITH (
        'connector' = 'filesystem',
        'format' = 'csv',
        'path' = '/tmp/output'
    )
""")

table_env.execute_sql("""
INSERT INTO print
SELECT id, data
FROM datagen
""").wait()

要运行脚本:

$ python stream_to_csv.py

我希望记录转到 /tmp/output 文件夹,但是这不会发生。

$ ~ ls /tmp/output
(nothing shown here)

有什么我想念的吗?

4

1 回答 1

1

我在http://apache-flink-user-mailing-list-archive.2336050.n4.nabble.com/Flink-Not-able-to-sink-a-stream-into-csv-td43105中厚颜无耻地复制了点福的回复.html

您需要为文件系统设置滚动策略。您可以参考滚动策略部分 [1] 了解更多详细信息。

实际上有输出,你可以执行命令ls -la /tmp/output/,然后你会看到几个名为“.part-xxx”的文件。

对于您的工作,您需要在文件系统连接器execution.checkpointing.interval的配置和sink.rolling-policy.rollover-interval属性中进行设置。

作业将如下所示:

from pyflink.table import EnvironmentSettings, StreamTableEnvironment

env_settings = EnvironmentSettings.new_instance().in_streaming_mode().use_blink_planner().build()
table_env = StreamTableEnvironment.create(environment_settings=env_settings)
table_env.get_config().get_configuration().set_string("execution.checkpointing.interval", "10s")

table_env.execute_sql("""
    CREATE TABLE datagen (
        id INT,
        data STRING
    ) WITH (
        'connector' = 'datagen',
        'rows-per-second' = '1'
    )
""")

table_env.execute_sql("""
    CREATE TABLE print (
        id INT,
        data STRING
    ) WITH (
        'connector' = 'filesystem',
        'format' = 'csv',
        'path' = '/tmp/output',
        'sink.rolling-policy.rollover-interval' = '10s'
    )
""")

table_env.execute_sql("""
INSERT INTO print
SELECT id, data
FROM datagen
""").wait()

[1] https://ci.apache.org/projects/flink/flink-docs-release-1.12/dev/table/connectors/filesystem.html#rolling-policy

于 2021-04-21T13:25:34.290 回答