【问题标题】:declare variable in sql (hive)在 sql (hive) 中声明变量
【发布时间】:2016-05-20 17:23:24
【问题描述】:

我在互联网上进行了深入研究,但找不到任何合适的答案。

在 hive 中,是否可以声明一个变量,比如说:

test = 1

并在查询中更改此变量的值?

select
   case
      when field > 1 then test = test+1
      else test = 1
   end as test
from my table

【问题讨论】:

  • 您可以使用会话变量,例如:@test :=1
  • 不,无法完成的两个原因——(1)Hive 编译查询以创建适当的执行计划,以及(2) 这些参数在客户端(胖 CLI 或瘦 Beeline 客户端)而不是在服务器端进行管理。想象一下在编译最终文本之前进行文本替换的预处理器。

标签: sql variables hive


【解决方案1】:

有可能。请找到以下代码在 Hive 中创建变量。

hive> SET cust_id = 1234567890;

创建变量后,您可以在查询中使用它,如下所示。

hive> select * from cust_table where customer_id = '${hiveconf:cust_id}';

希望这会对您有所帮助。 现在您可以将它应用到您的场景中。

【讨论】:

  • 感谢您的回答!这也有效: set ids = 1,2,3; select * from users where id in (${hiveconf:ids});
  • 此外,它可以用作列名而不是列中的值,如下所示:SET col_name = order_id; SELECT customer_id, ${hiveconf:col_name} from database.table_name; 这将从表中返回 customer_id、order_id 列,假设表具有 order_id 作为列。请注意,在分配变量时,分配值没有被引用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-13
  • 2015-05-04
  • 2011-05-12
  • 2012-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多