2

It's pretty straightforward what I'm trying to do. I just need to count the records in multiple Hive tables.

I want to create a very simple hql script that takes a file.txt with table names as input and count the total number of records in each of them:

SELECT COUNT(*) from <tablename>

Output should be like:

table1 count1
table2 count2
table3 count3

I'm new to Hive and not very well versed in Unix scripting, and I'm unable to figure out how to create a script to perform this.

Can someone please help me in doing this? Thanks in advance.

1 Answer 1

4

Simple working shell script:

 db=mydb

 for table in $(hive -S -e "use $db; show tables;") 
 do 
 #echo "$table"
 hive -S -e "use $db; select '$table' as table_name, count(*) as cnt from $table;"
 done

You can improve this script and generate file with select commands or even single select with union all, then execute file instead of calling Hive for each table.

If you want to read table names from file, use this:

for table in filename
do 
...
done
Sign up to request clarification or add additional context in comments.

5 Comments

What am I missing? Is there something else that I need to pass to the script?
Are you executing it in a shell? If you are trying to call beeline (not hive cli) from shell script, look at this: community.hortonworks.com/questions/64438/…
I'm executing it in a shell.
After modification, my code is as follows: for table in table_list do echo "$table" eval 'hive -e "select count(*) as cnt from dbname.$table;"' done and I execute it by calling: sh loop.sh < list_of_tables.txt but now it gives me the error: Line 1:28 Table not found 'table_list' Any idea why it's unable to associate the input file with the variable table_list in for loop? Also, I did not know how to get the table name in the select query, because the quotes gets messed up.
@LearneR why do you need eval? Script in my answer works, I have tested it. Look also at this: cyberciti.biz/faq/bash-loop-over-file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.