[ACCEPTED]-Hive: writing column headers to local file?-hive

Accepted answer
Score: 64

Try

set hive.cli.print.header=true;

0

Score: 15

Yes you can. Put the set hive.cli.print.header=true; in a .hiverc file in your 5 main directory or any of the other hive 4 user properties files.

Vague Warning: be 3 careful, since this has crashed queries 2 of mine in the past (but I can't remember 1 the reason).

Score: 9

Indeed, @nija's answer is correct - at least 9 as far as I know. There isn't any way to 8 write the column names when doing an insert overwrite into [local] directory ... (whether 7 you use local or not).

With regards to the 6 crashes described by @user1735861, there 5 is a known bug in hive 0.7.1 (fixed in 0.8.0) that, after 4 doing set hive.cli.print.header=true;, causes a NullPointerException for any HQL command/query 3 that produces no output. For example:

$ hive -S
hive> use default; 
hive> set hive.cli.print.header=true;
hive> use default;
Exception in thread "main" java.lang.NullPointerException
    at org.apache.hadoop.hive.cli.CliDriver.processCmd(CliDriver.java:222)
    at org.apache.hadoop.hive.cli.CliDriver.processLine(CliDriver.java:287)
    at org.apache.hadoop.hive.cli.CliDriver.main(CliDriver.java:517)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at org.apache.hadoop.util.RunJar.main(RunJar.java:197)

Whereas 2 this is fine:

$ hive -S
hive> set hive.cli.print.header=true;
hive> select * from dual;
c
c
hive> 

Non-HQL commands are fine though 1 (set,dfs !, etc...)

More info here: https://issues.apache.org/jira/browse/HIVE-2334

Score: 7

Hive does support writing to the local directory. You 8 syntax looks right for it as well.
Check 7 out the docs on SELECTS and FILTERS for additional information.

I don't 6 think Hive has a way to write the names 5 of the columns to a file for the query you're 4 running . . . I can't say for sure it doesn't, but 3 I do not know of a way.

I think the only 2 place better than SO for Hive questions 1 would be the mailing list.

Score: 4

I ran into this problem today and was able 9 to get what I needed by doing a UNION ALL 8 between the original query and a new dummy 7 query that creates the header row. I added 6 a sort column on each section and set the 5 header to 0 and the data to a 1 so I could 4 sort by that field and ensure the header 3 row came out on top.

create table new_table as
select 
  field1,
  field2,
  field3
from
(
  select
    0 as sort_col,  --header row gets lowest number
    'field1_name' as field1,
    'field2_name' as field2,
    'field3_name' as field3
  from
    some_small_table  --table needs at least 1 row
  limit 1  --only need 1 header row
  union all
  select
    1 as sort_col,  --original query goes here
    field1,
    field2,
    field3
  from
    main_table
) a
order by 
  sort_col  --make sure header row is first

It's a little bulky, but 2 at least you can get what you need with 1 a single query.

Hope this helps!

Score: 3

Not a great solution, but here is what I 1 do:

create table test_dat
ROW FORMAT DELIMITED FIELDS TERMINATED BY "\t" STORED AS 
INPUTFORMAT "com.hadoop.mapred.DeprecatedLzoTextInputFormat" 
OUTPUTFORMAT "org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat" 
LOCATION '/tmp/test_dat' as select * from YOUR_TABLE;

hive -e 'set hive.cli.print.header=true;select * from YOUR_TABLE limit 0' > /tmp/test_dat/header.txt

cat header.txt 000* > all.dat
Score: 2

Here's my take on it. Note, i'm not very 2 well versed in bash, so improvements suggestions 1 welcome :)

#!/usr/bin/env bash

# works like this:
# ./get_data.sh database.table > data.csv

INPUT=$1
TABLE=${INPUT##*.}
DB=${INPUT%.*}

HEADER=`hive -e "
  set hive.cli.print.header=true;
  use $DB;
  INSERT OVERWRITE LOCAL DIRECTORY '$TABLE'
  row format delimited
  fields terminated  by ','
  SELECT * FROM $TABLE;"`

HEADER_WITHOUT_TABLE_NAME=${HEADER//$TABLE./}
echo ${HEADER_WITHOUT_TABLE_NAME//[[:space:]]/,}
cat $TABLE/*

More Related questions