python - 无法将python变量传递给hive -e

标签 python shell hadoop hive

我想使用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/

相关文章:

linux - .screenrc 中的自定义脚本

hadoop - Hadoop在RHEL Start-dfs.sh上的异常行为

hadoop - HBase 中的检查点

python - 通过python访问放置在多个目录中的文件

python - 如何在 Django 中设置自定义中间件

linux - 如何在 Linux bash shell 中转义 bang (!) 字符?

linux - yarn logs + blk_xxxxxx_xxxxxx 不存在或不在 build 中

python - Django:了解 .values() 和 .values_list() 用例

python - 当空 python 时继续 sock.recv()

linux - 如何反转 shell 字符串中的单词列表?