Home > Net >  Loop through table name in Hive
Loop through table name in Hive

Time:02-24

I'm trying loop through table name and export the table schema as .csv file. Here is my shell script:

#!/bin/bash
## declare an array variable
declare -a array=("table1"
                  "table2")

# get length of an array
arraylength=${#array[@]}

# use for loop to read all values and indexes
for (( i=0; i<${arraylength}; i   ));
do
  echo "index: $i, value: ${array[$i]}"
  hive -e $('DESCRIBE FORMATTED ${array[$i]') | sed 's/[\t]/,/g' > /tmp/${array[$i]}.csv
done

But I got this error:

test.sh: line 14: DESCRIBE FORMATTED ${array[$i]: command not found
Missing argument for option: e

Any suggestions?

CodePudding user response:

The issue is here:

hive -e $('DESCRIBE FORMATTED ${array[$i]') | sed 's/[\t]/,/g' > /tmp/${array[$i]}.csv

Command substitution $() executes shell command but DESCRIBE is not a shell command. Also curly brace } is missing

Fixed:

hive -e "DESCRIBE FORMATTED ${array[$i]};" | sed 's/[\t]/,/g' > /tmp/${array[$i]}.csv

See example of similar command: https://stackoverflow.com/a/52541046/2700344

You may need to use $(command) in case if you want to get command result into variable, for example:

count=$(hive -e "select count(*) from mytable")

In your case you do not need $() command substitution.

  • Related