0%

如何在impala-shell之外使用特定用户set变量

版本信息

  • impalad 2.5.0-cdh5.7.*

问题

通过查阅官网可以知道,用户特定变量无法在除impala-shell外使用,这对使用odbc、jdbc或thrift的客户端来说,SQL维护会造成很大困扰 —— 在长SQL中改变一个或多个条件的值

在impala-shell中,可以通过set语句或--var参数设置会话上下文中的变量,如下

使用set

1
2
3
4
[localhost:21000] > set var:table_name=production_table;
[localhost:21000] > set var:cutoff=3;
[localhost:21000] > select s from ${var:table_name} order by s limit ${var:cutoff};
Query: select s from production_table order by s limit 3

使用–var

1
2
3
$ impala-shell --var=table_name=staging_table --var=cutoff=2
[localhost:21000] > select s from ${var:table_name} order by s limit ${var:cutoff};
Query: select s from staging_table order by s limit 2

模拟impala-shell:通过程序替换

1
2
3
set var:table_name=production_table;
set var:cutoff=3;
select s from ${var:table_name} order by s limit ${var:cutoff};

有以上sql,

  1. 通过正则表达式抽出set语句
  2. 替换到由${}包裹的变量名
  3. 使用odbc/jdbc/thrift执行替换变量后的sql
不为赚钱,只为交个朋友!干杯~

Welcome to my other publishing channels