Our Community is getting an upgrade! To get everything ready for the relaunch, we’ll be placing the site in read-only mode starting September 21st.
We really appreciate your understanding while we get things set up behind the scenes. Catch up on all the exciting details about the move here.
Need help or have questions? Drop us a line at [email protected]

Archives of Support Questions (Read Only)

This is an archived board for historical reference. Information and links may no longer be available or relevant
Announcements
This board is archived and read-only for historical reference. To ask a new question, please post a new topic on the appropriate active board.

How to select the last table from a list of hive tables?

avatar
Expert Contributor

I have a list of hive tables and want to select the last table for performing some query. Here is what I use to get the list of similar hive tables.

show tables 'test_temp_table*';

It displays the below result

test_temp_table_1
test_temp_table_2
test_temp_table_3
test_temp_table_4
test_temp_table_5
test_temp_table_6

I need to run some query on test_temp_table_6. I can do this using shell script by writing the output to a temp file and reading the last value from it but is there a simple way using hive query to get the last table that has the maximum number at the end?

1 ACCEPTED SOLUTION

avatar
Expert Contributor

Finally, I got the answer from Stackoverflow and wanted to propagate here. There isn't seems to be a straight way to get the last table name however the answer works just using a single line of shell script including the hive query. Here is it.

last_table=$(hive -e "show tables 'test_temp_table*';" | sort -r | head -n1)

View solution in original post

2 REPLIES 2

avatar
Expert Contributor

Finally, I got the answer from Stackoverflow and wanted to propagate here. There isn't seems to be a straight way to get the last table name however the answer works just using a single line of shell script including the hive query. Here is it.

last_table=$(hive -e "show tables 'test_temp_table*';" | sort -r | head -n1)

avatar
Super Collaborator

@Alex Raj You can also create a script using 2 commands

  1. Execute "show tables; " to get all tables
  2. For each table execute "desc formatted < database >.< table_name >" . The output contains a field CreateTime.
  3. use CreateTime field to find the latest table.