Hive: writing column headers to local file?

前端 未结 7 1489
别跟我提以往
别跟我提以往 2020-12-08 01:17

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?

7条回答
  •  天涯浪人
    2020-12-08 01:40

    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.

    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 at least you can get what you need with a single query.

    Hope this helps!

提交回复
热议问题