Hive documentation lacking again:
I'd like to write the results of a query to a local file as well as the names of the columns.
Does Hive support this?
Insert overwrite local directory 'tmp/blah.blah' select * from table_name;
Also, separate question: Is StackOverflow the best place to get Hive Help? @Nija, has been very helpful, but I don't to keep bothering them...
Yes you can. Put the
set hive.cli.print.header=true;
in a.hiverc
file in your main directory or any of the other hive user properties files.Vague Warning: be careful, since this has crashed queries of mine in the past (but I can't remember the reason).
Indeed, @nija's answer is correct - at least as far as I know. There isn't any way to write the column names when doing an
insert overwrite into [local] directory ...
(whether you use local or not).With regards to the crashes described by @user1735861, there is a known bug in hive
0.7.1
(fixed in0.8.0
) that, after doingset hive.cli.print.header=true;
, causes aNullPointerException
for any HQL command/query that produces no output. For example:Whereas this is fine:
Non-HQL commands are fine though (
set
,dfs
!
, etc...)More info here: https://issues.apache.org/jira/browse/HIVE-2334
Here's my take on it. Note, i'm not very well versed in bash, so improvements suggestions welcome :)
I ran into this problem today and was able to get what I needed by doing a UNION ALL between the original query and a new dummy query that creates the header row. I added a sort column on each section and set the header to 0 and the data to a 1 so I could sort by that field and ensure the header row came out on top.
It's a little bulky, but at least you can get what you need with a single query.
Hope this helps!
Hive does support writing to the local directory. You syntax looks right for it as well.
Check out the docs on SELECTS and FILTERS for additional information.
I don't think Hive has a way to write the names of the columns to a file for the query you're running . . . I can't say for sure it doesn't, but I do not know of a way.
I think the only place better than SO for Hive questions would be the mailing list.
Try