【问题标题】:Unable to Pass python varaible to hive -e无法将 python 变量传递给 hive -e
【发布时间】:2018-04-27 21:38:40
【问题描述】:

我想使用 python 从用户那里获取表名,并使用 select * 获取表的内容。下面的代码告诉我在调用 hive shell 时设置了一个变量。所以,如果是这样,这很好用一个硬编码的值。但如果我给出类似set x= $table_name 的东西,它就行不通了。这意味着它无法将 table_name 的值传递给 hive shell。

import subprocess
table_name=raw_input("enter the name of the table you want to get queried")
subprocess.call(['hive','-e','set x ="mytable";select  * from ${hiveconf:x}'])

链接Python variable passed as hive query 告诉了一些方法,但不确定我将如何使用它。

【问题讨论】:

    标签: python shell hadoop hive


    【解决方案1】:

    这是很久以前的事了,但我今天才看到这个。这就是我在 Hive 中的做法:

    import subprocess
    table_name=raw_input("enter the name of the table you want to get queried")
    
    # formulate your command line statement in a string variable
    completeCmd = "hive -e  " + "'select * from {table} '".format(table = table_name )
    subprocess.call(completeCmd, shell = True)
    

    在 python 3 中,输入语句会改变:

    table_name=input("enter the name of the table you want to get queried")

    如果它不起作用,请告诉我。

    【讨论】:

      猜你喜欢
      • 2019-12-17
      • 2016-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-01
      相关资源
      最近更新 更多