hive使用小技巧-如何往Hive SQL中传入参数

来源:互联网 发布:新版淘宝摇一摇在哪里 编辑:程序博客网 时间:2024/06/02 06:57
很多SQL需要重复执行,可能是每天执行一次,而需要修改的只是SQL中的日期字段,这种情况下可以使用 参数代入
使用的方式
  1. hive -d key1=value1 -d key2=value -d key3=value3 ...
复制代码

例子-1:

  1. hive -d shell_date_1='20120425' -d shell_date_2='20120426'
  2. hive> set shell_date_1;
  3. shell_date_1=20120425
  4. hive> set shell_date_2;
  5. shell_date_2=20120426
  6. hive> explain select * from dual where user_id = ${shell_date_1};
复制代码

这里面的 shell_date_1 会替换为 20120425
例子-2:
  1. hive -d shell_date_1='20120425' -d shell_date_2='20120426' \ 
  2.        -e "explain select * from dual where user_id = ${shell_date_1}"
复制代码

例子-3:
  1. vim hive.sql
复制代码

内容为:

  1. explain select * from dual where user_id = ${shell_date_1}
  2. hive -d shell_date_1='20120425' -f hive.sql
复制代码

原创粉丝点击