Sometimes the soul wants something really extraordinaly... for example, to write a unit test for Hive query :)
Let's how it is possible step be step. So, to write unit test for Hive:
First of all, the local hive instance must be run, and for that we need local metastor (I propose Apache Derby) and directories for temporary data, logs, etc. As all configuration will be read from system properties, I didn't find beter way then set up all of them programaticaly...
Be shure to create all mentioned directories before starting Hive, for example with google Guava:
And after then register all of them in system environment:
After that, the local hive executor might be started:
In fact, we are ready in this moment. Now I propose to create a Hive table, load data into it and perform some queries. The best practice in Java world is to put all metadata/data for test in separate file, so I put them under resources directory in this example, and here is reading from resource text files:
Ok, now data in the table and Hive knows about them. Let's perform a query:
Even more, we can register custom function and test it!
And after that we can call fresh function: