【问题标题】:passing argument from shell script to hive script将参数从 shell 脚本传递到 hive 脚本
【发布时间】:2015-06-02 09:18:26
【问题描述】:

我有一个问题,可以分为两种方式: 我的要求是将参数从 shell 脚本传递给 hive 脚本。 或者 在一个 shell 脚本中,我应该在 hive 语句中包含变量的值。

我会用两个例子来解释:

1) 将参数从 shell 脚本传递到 hiveQL->

My test Hive QL:
select count(*) from demodb.demo_table limit ${hiveconf:num}

我的测试shell脚本:

cnt=1
sh -c 'hive -hiveconf num=$cnt -f countTable.hql'

所以基本上我想在 HQL 中包含“cnt”的值,在这种情况下不会发生。我得到的错误是:

FAILED: ParseException line 2:0 mismatched input '<EOF>' expecting Number near 'limit' in limit clause

我确定错误意味着变量的值没有被传递。

2) 直接在 shell 脚本中传递参数->

cnt=1
hive -e 'select count(*) from demodb.demo_table limit $cnt'

在上述两种情况下,我都无法传递参数值。有什么想法吗??

PS:我知道在计数中包含“限制”的查询似乎很荒谬,但我已经重新表述了我实际遇到的问题。传递参数的要求保持不变。

任何想法,任何人?

提前致谢。

【问题讨论】:

  • 你能在第二种情况下做 echo 看看你得到了什么: echo 'select count(*) from demodb.demo_table limit $cnt'
  • 使用单引号抑制扩展。

标签: bash hadoop hive


【解决方案1】:

这样设置变量:

#!/bin/bash
cnt=3
echo "Executing the hive query - starts"
hive -hiveconf num=$cnt -e ' set num; select * from demodb.demo_table limit ${hiveconf:num}'
echo "Executing the hive query - ends"

【讨论】:

  • num="$cnt",如果您想让答案更通用(能够传递的不仅仅是数字,还可能包含空格、全局字符等)。
  • 感谢 sras,这是完美的。我从来没有从命令行使用多个选项来配置配置单元。完全同意查尔斯;单引号抑制信息。
  • 更准确地说,我们可以使用hivevar命名空间而不是hiveconf命名空间。但是,两者都适用于您的查询。
【解决方案2】:

如果放入名为hivetest.sh 的文件中,则此方法有效,然后使用sh hivetest.sh 调用:

cnt=2
hive -e "select * from demodb.demo_table limit $cnt"

您使用的是单引号而不是双引号。 对 OPTION #1 使用双引号也可以。

【讨论】:

    【解决方案3】:

    hadoop@osboxes:~$ export val=2;

    hadoop@osboxes:~$ hive -e "select * from bms.bms1 where max_seq=$val";

    vi test.sh
    #########
    export val=2
    hive -e "select * from bms.bms1 where max_seq=$val";
    #####################
    

    【讨论】:

      【解决方案4】:

      试试这个
      cnt=1

      hive -hiveconf number=$cnt select * from demodb.demo_table limit ${hiveconf:number}
      

      【讨论】:

        猜你喜欢
        • 2016-12-01
        • 1970-01-01
        • 1970-01-01
        • 2022-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-31
        • 1970-01-01
        相关资源
        最近更新 更多