如何在配置单元的选择查询中使用变量值

xzlaal3s  于 2021-06-28  发布在  Hive
关注(0)|答案(1)|浏览(239)

我发现一个月的天数如下。

  1. set ndays=datediff(CONCAT(y, '-', (m + 1), '-', '01'), CONCAT(y, '-', m, '-', '01')) FROM (SELECT month(current_date) as m, year(current_date) as y, day(current_date)) tabl1;

我正在检查数值。

  1. select ${hiveconf:ndays}; --O/P 31

我在一个查询中使用这个变量,得到一个错误。

  1. select
  2. sum(price)/ ${hiveconf:ndays}
  3. from sales_aly
  4. GROUP BY sales_month;

失败:parseexception行3:0在“tabl1”附近的“from”处缺少eof
请帮帮我。
提前谢谢。

kcwpcxri

kcwpcxri1#

set 命令将不计算此表达式的值: datediff(CONCAT(y, '-', (m + 1), '-', '01'), CONCAT(y, '-', m, '-', '01')) FROM (SELECT month(current_date) as m, year(current_date) as y, day(current_date)) tabl1 但将表达式本身指定为值。然后,当您在代码中使用变量时,将替换表达式而不是变量。
您可以在shell中计算表达式并将其传递给配置单元,如下所示:

  1. var=$(hive -e "set hive.cli.print.header=false; select datediff(CONCAT(y, '-', (m + 1), '-', '01'), CONCAT(y, '-', m, '-', '01')) FROM (SELECT month(current_date) as m, year(current_date) as y, from tab1 limit 1) tabl1;")
  2. # Then call your script:
  3. hive -hiveconf ndays=$var -f your_script_file_name

脚本中的地址变量: ${hiveconf:ndays} 也可以使用宏。看这个:http://svn.apache.org/repos/asf/hive/trunk/ql/src/test/queries/clientpositive/macro.q

相关问题