Hive: writing column headers to local file?

前端 未结 7 1438
别跟我提以往
别跟我提以往 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:22

    Here's my take on it. Note, i'm not very well versed in bash, so improvements suggestions 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/*
    

提交回复
热议问题