pyflink过滤kafka数据

py 复制代码
from pyflink.table import (TableEnvironment, EnvironmentSettings)

# 输入、输出、过滤条件
columns_in = [
...
]

columns_out = [
...
]
filter_condition = "name = '蒋介石' and sex = '男'"


# 创建执行环境

t_env = TableEnvironment.create(EnvironmentSettings.in_streaming_mode())
t_env.get_config().get_configuration().set_string("pipeline.jars", "file:///work/flink-sql-connector-kafka-3.2.0-1.19.jar")

source_topic = "foo"
sink_topic = "baa"
kafka_servers = "kafka:9092"
kafka_consumer_group_id = "flink consumer"

columnstr = ','.join([f"`{col}` VARCHAR"  for col in columns_in])
source_ddl = f"""
CREATE TABLE kafka_source({columnstr}) WITH (
              'connector' = 'kafka',
              'topic' = '{source_topic}',
              'properties.bootstrap.servers' = '{kafka_servers}',
              'properties.group.id' = '{kafka_consumer_group_id}',
              'scan.startup.mode' = 'latest-offset',
              'format' = 'json'
            )
"""

columnstr2 = ','.join([f"`{col}` VARCHAR"  for col in columns_out])
sink_ddl = f"""
CREATE TABLE kafka_sink ({columnstr2}
    ) with (
      'connector' = 'kafka',
      'topic' = '{sink_topic}',
      'properties.bootstrap.servers' = '{kafka_servers}',
      'properties.group.id' = '{kafka_consumer_group_id}',
      'scan.startup.mode' = 'latest-offset',
      'format' = 'json'
    )
"""
# 过滤字段
filtersql = f"""
insert into kafka_sink
select {
','.join([f"`{col}`"  for col in columns_out])
}
from kafka_source
where {filter_condition}
"""
t_env.execute_sql(filtersql)
t_env.execute_sql(source_ddl)
t_env.execute_sql(sink_ddl)
相关推荐
武子康12 分钟前
Java-167 Neo4j CQL 实战:CREATE/MATCH 与关系建模速通 案例实测
java·开发语言·数据库·python·sql·nosql·neo4j
upward_tomato19 分钟前
python中模拟浏览器操作之playwright使用说明以及打包浏览器驱动问题
开发语言·python
为你写首诗ge23 分钟前
【python】python安装使用pytorch库环境配置
pytorch·python
信创天地39 分钟前
RISC-V 2025年在国内的发展趋势
python·网络安全·系统架构·系统安全·运维开发
Danceful_YJ1 小时前
30.注意力汇聚:Nadaraya-Watson 核回归
pytorch·python·深度学习
FreeCode1 小时前
LangChain1.0智能体开发:人机协作
python·langchain·agent
2501_930412271 小时前
如何添加清华源到Conda?
开发语言·python·conda
2501_930412271 小时前
如何删除Conda中的清华源配置?
开发语言·python·conda
程序猿20232 小时前
Python每日一练---第十二天:验证回文串
开发语言·python
沿着路走到底2 小时前
python 判断与循环
java·前端·python