2

I referred to How to use variables in SQL statement in Python?

but couldn't get an answer.

I am trying what you suggested, but I get this error - :(

tbl_nm = 'EMPLOYEE_TABLE'

con.execute('select max(emp_id) from schema.?', tbl_nm)

Getting below error:

Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python2.7/site-packages/impala/hiveserver2.py", line 302,

in execute configuration=configuration) File "/usr/local/lib/python2.7/site-packages/impala/hiveserver2.py", line 343,

in execute_async self._execute_async(op) File "/usr/local/lib/python2.7/site-packages/impala/hiveserver2.py", line 362,

in _execute_async operation_fn() File "/usr/local/lib/python2.7/site-packages/impala/hiveserver2.py", line 334,

in op parameters) File "/usr/local/lib/python2.7/site-packages/impala/interface.py", line 267,

in _bind_parameters

raise ProgrammingError("Query parameters argument should be a "
impala.error.ProgrammingError: Query parameters argument should be a list, tuple, or dict object

>
akshat
  • 1,219
  • 1
  • 8
  • 24
rajb2r
  • 31
  • 2

1 Answers1

1

Instead of passing raw hive query directly to execute method, it is recommended that you store your query as a string into a variable and pass it to execute method.

In your case:

tbl_nm = 'EMPLOYEE_TABLE'
query = "select max(emp_id) from schema.{}".format(tbl_nm)
con.execute(query)

Let me know if this helps.

Batman
  • 50
  • 8
  • Thank you - after posting this question I figured out the answer. How can I execute a hiveql script in python. example - I have a .hql script load_emp_table.hql which contains a series of queries: – rajb2r May 24 '18 at 13:36
  • Thank you - after posting this question I figured out the answer. How can I execute a hiveql script in python. example - I have a .hql script load_emp_table.hql which contains a series of queries: Before the query can to be executed, all the parameters needs to be resolved. tbl_nm1 = emp_stg_tbl tbl_nm = emp_tbl schema_name = emp_schema load_emp_table.hql use ${schema_name}; INSERT OVERWRITE TABLE ${schema_name}.${tbl_nm1} select * from ${schema_name}.${tbl_nm} – rajb2r May 24 '18 at 13:43
  • You can parse the HQL file and using regex, replace the ${schema_name} and ${tbl_nm1} with the variables(which inturn store the actual schema_name and tbl_nm) and then pass that string to execute method. These variables can be passed to python as arguments and you may replace the "$" strings in the HQL file. If the schema_name and tbl_nm come dynamically, find a way to store them into local variables in the python program and use them. – Batman May 24 '18 at 16:02
  • Thank you - i did exactly the same thing. It is working. I used replace to replace ${tbl_nm} with actual table name and re frame the query – rajb2r May 30 '18 at 17:22