Skip to content Skip to sidebar Skip to footer

Python Variable Passed As Hive Query

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 in

Solution 1:

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

Solution 2:

You can do it by adding + before and after your variables. This way you treat them as strings and + simply concatenates them. cmd = "hive -S -e 'SELECT * FROM project.full_score WHERE person_name=="+ full_name + " AND city==" + residence_city +";'"

Solution 3:

Found a better solution based on Pander's answer:

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

The \ \ before each " did the trick, now user can input data without having to worry about quotations.

Thank you guys for your help!!!

Post a Comment for "Python Variable Passed As Hive Query"