有没有办法在单独的文件中设置查询参数(变量)?

Is there a way to set query parameters (variables) in separate files?

我有许多 SQL 使用一组通用参数(变量)的查询。目前,参数 set 位于 每个 文件的顶部。当任何参数发生变化时,必须在每个文件中进行更改。如果能够将参数放在一个单独的文件中并且只在一个地方更改,那将是有益的。

如何实现?

我意识到我可以使用 --var 选项到 impala-shell,但这意味着这些必须多次输入。

我可以看到这种可能发生的几种方式:

  1. impala-shell 可能支持多个 -f 参数:这会非常优雅,但事实并非如此。

  2. 查询可以 cat 在一起并通过管道传输到 impala-shell。这是有用的但不优雅。

  3. 可以为指定 --var 个参数的 impala-shell 设置别名。这可能很难做到正确。

显然 #1 是最好的解决方案,但还有其他选择或建议吗?

不太相关:Multiple query execution in cloudera impala

3a. 从配置文件自动生成 --var 命令行参数。

试试这个 bash 函数...

function impala-with-conf {
  if [[ -z "" || ! -r "" ]] ; then
    echo "1st argument must be a properties file" >&2
    return 1
  fi
  typeset AllVarArgs=''
  while IFS='' read -r Line || [[ -n "$Line" ]]; do
    KeyValue=$(echo "$Line" | sed -e 's/^[ \t]*//' -e 's/[ \t]*=[ \t]*/=/' -e 's/[ \t]*$//')
    AllVarArgs="$AllVarArgs --var $KeyValue" 
  done < ""
  shift
  impala-shell $AllVarArgs $@
}

(基于Read a file line by line assigning the value to a variable

将使用 "properties" 文件,例如

alpha_beta = some-text-without-spaces-or-quotes
epsilon=12

演示:

cat x.sql
select '${VAR:alpha_beta}' as ab , ${VAR:epsilon} as e

impala-with-conf x.conf -q x.sql
Query: select 'some-text-without-spaces-or-quotes' as ab , 12 as e
+------------------------------------+----+
| ab | e |
+------------------------------------+----+
| some-text-without-spaces-or-quotes | 12 |
+------------------------------------+----+

请检查Impala文档: https://www.cloudera.com/documentation/enterprise/5-15-x/topics/impala_shell_options.html#shell_options

在此处粘贴相关部分:

-f query_file or --query_file=query_file

Passes a SQL query from a file. Multiple statements must be semicolon (;) delimited. In CDH 5.5 / Impala 2.3 and higher, you can specify a filename of - to represent standard input. This feature makes it convenient to use impala-shell as part of a Unix pipeline where SQL statements are generated dynamically by other tools.

如您所见,Impala可以解析更多以分号(;)分隔的查询文件。这样,通过 --var 参数,您可以完成第一个案例。