1

Options in my mind:

  1. Fire DESCRIBE TABLE;

  2. Fire SELECT 1 FROM TABLE;

These queries will fail if table does not exist.

Which is better way? Or is there any other good approach?

Dev
  • 13,492
  • 19
  • 81
  • 174

1 Answers1

0

In many scenarios, we might not be sure in which database the table exists, so generally we keep looping through all the databases. To save time, you can use the below script. replace the $BEELINE_CONN_STRING with your beeline strings, you can save this as a script and pass table_name as parameter. To make your searching specific to some databases you can also change the show databases to show database like <your preferred database string> in the below code or pass that also as a parameter to the script.

table_name=${1}
for database in `$BEELINE_CONN_STRING --showHeader=false --outputformat=tsv2 --silent=true -e "show databases;" 2> /dev/null `
do
result=`$BEELINE_CONN_STRING --silent=true --showHeader=false --outputformat=tsv2 -e "use $database;show tables like '*$table_name*';"  2> /dev/null `
if [[ "test_append"$result"" != "test_append" ]];
then
    echo "Table $result exists in $database"
else
    echo "Table not found in $database"
fi
done

Sample output looks like

Table not found in yourdb
Table www_table exists in mydb
venkata
  • 447
  • 3
  • 15