Python变量作为配置单元查询传递 [英] Python variable passed as hive query
问题描述
我正在尝试创建一个将要求用户输入的python脚本。该输入将存储在两个单独的变量中,然后这些变量将用于查询配置单元的信息。下面的代码:
I am trying to create a python script that will ask the user for input. This input will be stored in two separate variables and these variables will then used to query hive for information. The code below:
person_database = []
full_name = raw_input('Enter Your Full Name: ')
residence_city = raw_input('Enter City of Interest: ')
def check(): #Define function
` cmd = "hive -S -e 'SELECT * FROM project.full_score WHERE` person_name=="full_name" AND city=="recidence_city";'"
person_database = commands.getoutput(cmd)
print person_database
现在的问题是,如何将变量传递给cmd,以便该Hive可以理解使用它们包含的内容,而不是变量名称?例如,如果full_name包含John Smith并且residence_city包含Vancouver,那么john Smith和Vancouver应构成查询的一部分。
Now the question is, how to pass the variables to "cmd" in a for that Hive can understand to use what they contain, not the variable name? For example if "full_name" contains "John Smith" and "residence_city" contains "Vancouver" then "john Smith" and "Vancouver" should form part of the query.
推荐答案
您只需将cmd视为一个字符串并构建您想要的文本:
you simply treat cmd as a string and build the text you want:
.......
cmd = "hive -S -e 'SELECT * FROM project.full_score WHERE` person_name=="%s" AND city=="%s";'"%(full_name,residence_city)
person_database = commands.getoutput(cmd)
print person_database
这篇关于Python变量作为配置单元查询传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!