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)
相关推荐
坚持就完事了8 分钟前
数据结构之链表
数据结构·python·算法·链表
木头左16 分钟前
自动化超参搜索框架在PCA参数调优中的应用
python
apihz17 分钟前
域名注册状态查询免费API接口详细教程
android·服务器·网络·python·tcp/ip
The_Second_Coming24 分钟前
Python 自动化运维学习笔记
运维·python·自动化
牙牙要健康1 小时前
【open3d】示例:自动计算点人脸点云模型面部朝向算法
人工智能·python·算法
lalala_lulu1 小时前
Lambda表达式是什么
开发语言·python
r***86981 小时前
Python中的简单爬虫
爬虫·python·信息可视化
atregret1 小时前
OSError: [WinError 1114] 动态链接库(DLL)初始化例程失败。Error loading ... c10.dll
人工智能·python
zhongtianhulian1 小时前
江苏设备管理平台哪家好
大数据·运维·python
love530love1 小时前
【ComfyUI/SD环境管理指南(二)】:如何避免插件安装导致的环境崩溃与“外科手术式”修复
人工智能·windows·python·stable diffusion·github·aigc·comfyui