I was using Hive via Presto for a project, but then I ran into an issue where Presto cannot support Hive views. So, to be kind to the user, I wanted to present the view definition so they could see how to query the underlying tables.
Unfortunately, you can’t get view definitions from Presto either! So, I had to directly query hive from a Python project.
There are two options that I found for achieving this, and surprisingly neither one was great. You would think this was easy right!?
- Use PyHive – This is the standard connector you would have expected to find, except it does not install and/or work on Windows. So, if you develop on Windows and deploy to Linux, it is painful. Also, you need some other things on the system for it to work which can be painful to find.
- Use JayDeBeApi – This uses the Java JAR to connect to Hive which means it needs Java installed on your machine. DO NOT USE THIS – I quickly ran into a critical bug that happens on both Windows and Linux – if you open one connection, do work, and close it, you cannot open another connection. It happens on Windows and Linux. There is a git story for it and the person had to resort to putting it in another script and calling it as a sub-process for each command which is ridiculous.
So, as I’m deploying on Linux (even though I develop on Windows), PyHive wins.
More on PyHive
So, to install PyHive, you would do the following (but it probably won’t work yet, at least not on Centos7 where I tried it).
pip install pyhive[hive]
In order to get “pyhive[hive]” to install on a server (I tested with Centos7), you have to ensure some other dependencies are available as well.
I was working from Python 3.6 in a virtual environment, and the following worked properly:
sudo yum install gcc-c++ python-devel.x86_64 cyrus-sasl-devel.x86_64 pip install pyhive[hive]
Note that if you do the install without the extra [hive] you will not get all the dependencies. The reason they’re broken out is this technically supports both Hive and Presto, and that means you get to pick which dependencies you need.
This is a mixed blessing; you can install the package on Windows and develop without the extra [hive] but if you try to execute the code it will fail. To run it on Linux you need the full set of dependencies.
I recommend guarding the pyhive import and any related code in your project with if os.name != “nt”: in order to ensure you can run through on Windows without getting errors. Hopefully your project is like mine where this is a side case and I can test plenty without the final calls.
The following is a short example of how to do a query from PyHive assuming you have it all set up properly as we talked about above.
conn = None cursor = None try: query = "describe extended ``.``" conn = hive.Connection(host="host-name", port="10000") cursor = conn.cursor() cursor.execute(query) query_results = cursor.fetchall() column_names = [part for part in cursor.description] df = pd.DataFrame(query_results, columns=column_names) except Exception as ex: logger.info("Error while pulling view details.", ex) raise ex finally: if cursor is not None: cursor.close() if conn is not None: conn.close()