Hive export to CSV
Bash function to export Hive table data to local CSV file
Usage: hive_export_csv <db.table> <output.csv> [queue]
Recommendation: Add to .bash_profile
hive_export_csv () {
if [ -z "$2" ]; then
echo "Bad arguments. Usage: ${FUNCNAME[0]} <db.table> <output.csv> [queue]"
else
uuid=$(uuidgen)
output=$2
[ -z $3 ] && queue="default" || queue="$3"
query="hive --hiveconf tez.queue.name=$queue -e \"insert overwrite directory '/tmp/"$uuid"'
row format delimited fields terminated by ',' lines terminated by '\n'
select * from $1\""
echo "Executing: $query"
eval "$query"
retval=$?
if [ "$retval" -ne 0 ]; then
echo -e "\nCommand failed with return code: $retval \n"
return $retval
fi
get_merge="hdfs dfs -getmerge '/tmp/"$uuid"/*' $output"
echo -e "\nExecuting: $get_merge"
eval "$get_merge"
delete_tmp="hdfs dfs -rm -r -f –skipTrash '/tmp/"$uuid"'"
echo "Executing: $delete_tmp"
eval "$delete_tmp"
fi
}
HTH