1

I have a sample.hql file which contains below lines.

desc db.table1;
desc db.table2;
desc db.table3;

I am trying to run it from shell command I want to find out if a particular column is present in the table or not For eg-If col_1 is present in table1 the output should say col1_1 is found in db.table1

I am not sure how to find it. I am executing below command

hive -f sample.hql | grep -q "<column_name>"

But I am not sure how to get the db and table name from each executing line.

Lauren Yim
  • 12,700
  • 2
  • 32
  • 59
Hannah
  • 163
  • 2
  • 12
  • @Ben It will be too time consuming beause for every call I have to access hive shell – Hannah Dec 10 '21 at 12:26
  • 1
    New(er) versions of Hive define ANSI-compliant `information_schema` database that can be queried (from beeline) directly instead of running multiple `describe table`. https://issues.apache.org/jira/browse/HIVE-1010. So if you've been diligent and kept up with upgrades... :) – mazaneicha Dec 10 '21 at 14:31

1 Answers1

0

You can make grep show you before -B and after -A. The below command would show you 10 lines before. This likely will get the job done quick and dirty.

 hive -f sample.hql | grep -B 10 -q "<column_name>"

If you wanted to be a little more careful you might try a for loop instead that feeds the lines to hive one at a time. If it finds the column it will echo the table it found the column in. ( the '&&' only executes code if the previous command was successful)

#!/bin/bash

for i in $(cat sample.hql); do
    
    hive -e "$i" | grep -q "<column_name>" && echo $i; 
done
Matt Andruff
  • 4,974
  • 1
  • 5
  • 21