3.阿里云flink&selectdb-py作业

1.概述

Python API中文文档

本文介绍在阿里云实时计算flink中使用python作业,把oss中的数据同步数据到阿里云selectdb的过程。python简单的语法特性更适合flink作业的开发;

先说结论:

在实际开发中遇到了很多问题,导致python作业基本基本无法运行。最后放弃了;

  • python作业中的标量函数的错误没有日志,永远是报这个错误:ExceptionInChainedOperatorException: Could not forward element to next operator,定位不到具体问题;
  • python作业中的用户定义的标量函数基本无法运行。本地测试没有问题的函数,提交到flink中就报错。怀疑是环境中没有flink-python.jar,自己上传此jar和flink中的包不兼容(阿里云flink和开源版本flink有些jar包不一样);
  • 如果各位遇到些问题并且有解决方案,麻烦也告知我,非常感谢;

2.目标

把阿里云sls日志中的数据准实时同步到云服务selectdb;

源表 flink 结果表
阿里云sls 实时计算flink 云服务selectdb

3.步骤

3.1.搭建环境

shell 复制代码
#**创建虚拟环境essa-flink,pyhton版本为3.11.9
conda create -n essa-flink python=3.11.9

#**安装apache-flink-1.20版本。安装的依赖比较大,指定国内的pip源
pip install apache-flink==1.20.0 -i https://pypi.tuna.tsinghua.edu.cn/simple

3.2.创建作业

作业代码本身很简单,逐行读取sls的日志,进行转换后保存到selectdb中。转换函数为do_active_log,在本地测试过程中遇到了第一个问题后,很轻松愉快就通过了。部署在flink中出现了其它问题;

  • 首先是阿里云提供sls连接器(ververica-connector-sls-1.17-vvr-8.0.8.jar)不可用,报错缺少com/alibaba/ververica/connectors/sls/source/SLSRecordReader。查看源码,确实没有定义此类。提工单后,建设使用低版本解决;
  • 然后报错缺少flink-python,不能执行python函数。于是把flink-python上传,并在作业中引用依赖;
  • 最后报错ExceptionInChainedOperatorException: Could not forward element to next,无法执行。把作业中函数调用do_active_log删除后正常。提工单后还是没有解决。最后放弃,改用jar作业;
python 复制代码
def do_active_log(row: Row) -> Row:
    '''用户登录日志处理'''
    logging.info('执行do_active_log函数...')
    params = json.loads(row[2])

    occurred = datetime.fromtimestamp(float(row[1]))
    user_id = params['userId']
    platform = params['platform']
    last_active_time = occurred
    create_time = occurred
    id = occurred.strftime("%Y%m%d") + str(user_id)

    return Row(str(id), int(user_id), platform, last_active_time, create_time)

def create_active_log_sink_table(table_env: StreamTableEnvironment, sink_table: str):
    '''创建用户登录日志结果表'''
    sql = '''
        create temporary table {}(
            id string,
            user_id int,
            platform string,
            last_active_time timestamp,
            create_time timestamp,
            primary key(id) not enforced
        ) with (
            'connector' = 'doris',
            'fenodes' = '{}',
            'table.identifier' = '{}',
            'username' = '{}',
            'password' = '{}',
            'sink.properties.format' = 'json'
        )
        '''.format(sink_table, sink_config['fenodes'], sink_config['table.identifier'], 
                   sink_config['username'], sink_config['password'])
    table_env.execute_sql(sql)

def get_soruce_datastream(table_env: StreamTableEnvironment):
    '''创建datastream'''
    times = {'start_time': '', 'stop_time': ''}
    sql = '''
        create temporary table essa_ubc(
            ip string,
            `time` string,
            content string,
            __topic__ string metadata virtual,
            __source__ string metadata virtual,
            __timestamp__ string metadata virtual
        ) with (
            'connector' = 'sls',
            'endpoint' = '{}',
            'accessId' = '{}',
            'accessKey' = '{}',
            'project' = '{}',
            'logstore' ='essa-ubc',
            'startTime' = '{}',
            'stopTime' = '{}',
            'exitAfterFinish' = 'true'
        )
        '''.format(source_config['sls_endpoint'], source_config['access_id'], source_config['access_secret'],
                   source_config['sls_project'], times['start_time'], times['stop_time'])
    table_env.execute_sql(sql)
    source_table = table_env.from_path('essa_ubc')
    return table_env.to_append_stream(source_table, Types.ROW([Types.STRING(), Types.STRING(), Types.STRING(), Types.STRING(),
                                                           Types.STRING(), Types.STRING()]))

if __name__ == '__main__':
    env = StreamExecutionEnvironment.get_execution_environment()
    t_env = StreamTableEnvironment.create(stream_execution_environment=env)
    #**加载依赖的jar包
    t_env.get_config().set("pipeline.jars", "依赖包.jar")
    
    #**创建sls源
    ds = get_soruce_datastream(t_env)

    #**用户登录日志处理
    #**读取sls日志数据,然后使用自定义标量函数处理数据
    ds = ds.filter(lambda d: d[3] == 'activeLog').map(do_active_log, Types.ROW([Types.STRING(), Types.INT(), Types.STRING(),
                                                                                Types.SQL_TIMESTAMP(), Types.SQL_TIMESTAMP()]))
    table = t_env.from_data_stream(ds)
    active_log_sink_table = 'user_active_log'
    create_active_log_sink_table(t_env, active_log_sink_table)
    table.execute_insert(active_log_sink_table).wait()
相关推荐
肖永威4 小时前
macOS环境安装/卸载python实践笔记
笔记·python·macos
TechWJ5 小时前
PyPTO编程范式深度解读:让NPU开发像写Python一样简单
开发语言·python·cann·pypto
枷锁—sha5 小时前
【SRC】SQL注入WAF 绕过应对策略(二)
网络·数据库·python·sql·安全·网络安全
abluckyboy5 小时前
Java 实现求 n 的 n^n 次方的最后一位数字
java·python·算法
喵手5 小时前
Python爬虫实战:构建各地统计局数据发布板块的自动化索引爬虫(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集数据csv导出·采集各地统计局数据发布数据·统计局数据采集
天天爱吃肉82186 小时前
跟着创意天才周杰伦学新能源汽车研发测试!3年从工程师到领域专家的成长秘籍!
数据库·python·算法·分类·汽车
m0_715575346 小时前
使用PyTorch构建你的第一个神经网络
jvm·数据库·python
甄心爱学习6 小时前
【leetcode】判断平衡二叉树
python·算法·leetcode
深蓝电商API6 小时前
滑块验证码破解思路与常见绕过方法
爬虫·python
Ulyanov6 小时前
Pymunk物理引擎深度解析:从入门到实战的2D物理模拟全攻略
python·游戏开发·pygame·物理引擎·pymunk