我想使用python从用户那里获得表名,并使用select *获得表的内容。下面的代码告诉我在调用 hive 壳时设置了一个变量,因此,如果它是一个硬编码的值就可以了。但是,如果我给出类似set x= $table_name
的信息,它将无法正常工作。这意味着它无法将table_name的值传递给配置单元 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讲述了一些方法,但不确定我将如何使用它。
最佳答案
这是很久以前的事了,但是我今天才看到。这是我在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")
让我知道它是否无效。
关于python - 无法将python变量传递给hive -e,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44994029/