Kettle table input使用变量替换in中的条件

来源:互联网 发布:遛99遛弯淘宝区怎么样 编辑:程序博客网 时间:2024/05/21 15:55

1. 问题描述

今天在使用kettle从PostgreSQL数据库表中导出数据到文本文件时,用到了in查询,原始的sql是下面这样的:
select to_char(current_timestamp, 'YYYY-MM-DD HH24:MI:SS') as checkin_time, '${personType}' as bd_type, 'AA.AA.AA.AA' as checkin_window_ip from compare_result where from_station in ('QTP','CTH','CLP','BDP','BEP','SHD') or to_station in ('QTP','CTH','CLP','BDP','BEP','SHD')
在Kettle的table input中使用变量替换的方式传入from_station和to_station的值,一开始的写法是这样的:
select to_char(current_timestamp, 'YYYY-MM-DD HH24:MI:SS') as checkin_time, '${personType}' as bd_type, 'AA.AA.AA.AA' as checkin_window_ip from compare_result where board_station in (${fromStationCode}) or to_station in (${toStationCode})
执行后发现有语法错误,意识到变量引用应该放到单引号里面,于是,改成了下面这样:
select to_char(current_timestamp, 'YYYY-MM-DD HH24:MI:SS') as checkin_time, '${personType}' as bd_type, 'AA.AA.AA.AA' as checkin_window_ip from compare_result where board_station in ('${fromStationCode}') or to_station in ('${toStationCode}')
这样解析出来的SQL是有问题的,in里面的值最外面多了一层单引号:
select to_char(current_timestamp, 'YYYY-MM-DD HH24:MI:SS') as checkin_time, '${personType}' as bd_type, 'AA.AA.AA.AA' as checkin_window_ip from compare_result where from_station in (''QTP','CTH','CLP','BDP','BEP','SHD'') or to_station in (''QTP','CTH','CLP','BDP','BEP','SHD'')
于是,想到了各种转义,包括在table input步骤的sql中对单引号转义和对参数里面的单引号进行转义,结果都无效。

2. 解决方法

查看Kettle的使用说明,发现变量的引用方式有两种,一种是我们上面使用的${}的方式,这种方式使用的时候外面一般都要套上一层单引号;另外一种是在变量名两边都加上另个%%的方式,这种方式不需要再加单引号,于是,把我们的查询改成了下面这样的:
select to_char(current_timestamp, 'YYYY-MM-DD HH24:MI:SS') as checkin_time, '${personType}' as bd_type, 'AA.AA.AA.AA' as checkin_window_ip from compare_result where board_station in (%%fromStationCode%%) or to_station in (%%toStationCode%%)
再次执行Kettle作业,发现作业被成功执行了,问题解决。
原创粉丝点击