19

I have few tables in hive that has same prefix like below..

temp_table_name
temp_table_add
temp_table_area

There are few hundreds of tables like this in my database along with many other tables. I want to delete tables that starts with "temp_table". Do any of you know any query that can do this work in Hive?

Alex Raj Kaliamoorthy
  • 2,035
  • 3
  • 29
  • 46

8 Answers8

22

There is no such thing as regular expressions for drop query in hive (or i didn't find them). But there are multipe ways to do it, for example :

  • With a shell script :

    hive -e "show tables 'temp_*'" | xargs -I '{}' hive -e 'drop table {}'
    
  • Or by putting your tables in a specific database and dropping the whole database.

    Create table temp.table_name;
    
    Drop database temp cascade;
    
Nicolás Ozimica
  • 9,481
  • 5
  • 38
  • 51
Tremo
  • 589
  • 6
  • 19
  • 1
    hive -e ‘show tables 'temp_*’ | xargs -I ‘{}’ hive -e ‘drop table {}’ is not working for me – MysticForce Jun 26 '16 at 17:02
  • 2
    The answer is perfect however there is a small change that is needed which actually worked for me. I am modifying @Louxou 's code a bit by adding double quotes. Here is the updated code. `hive -e "show tables 'temp_*'" | xargs -I '{}' hive -e 'drop table {}'` – Alex Raj Kaliamoorthy Sep 26 '17 at 06:34
  • i want to add something to this. The problem now is that hive ( or beeline ) tends to show the results in "graphical" format. Also if you have multiple DB in hive you need to specify the one you need `beeline --showHeader=false --outputformat=csv2 -e "use ; show tables 'temp_*'" | xargs -I '{}' beeline -e 'use; drop table {}'` – habarnam Jan 13 '22 at 07:00
9

Above solutions are good. But if you have more tables to delete, then running 'hive -e drop table' is slow. So, I used this:

hive -e 'use db;show tables' | grep pattern > file.hql

use vim editor to open file.hql and run below commands

:%s!^!drop table  
:%s!$!;

then run

hive -f file.hql

This approach will be much faster.

Community
  • 1
  • 1
Chandra
  • 526
  • 2
  • 9
  • 26
5

My solution has been to use bash script with the following cmd:

hive -e "SHOW TABLES IN db LIKE 'schema*';" | grep "schema" | sed -e 's/^/hive -e \"DROP TABLE db\./1' | sed -e 's/$/\"/1' > script.sh
chmod +x script.sh
./script.sh
HorusH
  • 231
  • 1
  • 5
  • 14
4

I was able to delete all tables using following steps in Apache Spark with Scala:

val df = sql("SHOW TABLES IN default LIke 'invoice*'").select("tableName") // to  drop only selected column
val df = sql("SHOW TABLES IN default").select("tableName")
val tableNameList: List[String] = df.as[String].collect().toList
val df2 = tableNameList.map(tableName => sql(s"drop table ${tableName}"))
Oleg Svechkarenko
  • 2,508
  • 25
  • 30
2

As I had a lot of tables do drop I used the following command, inspired in the @HorusH answer

hive -e "show tables 'table_prefix*'" | sed -e 's/^/ \DROP TABLE db_name\./1' | sed -e 's/$/;/1' > script.sh
hive -f script.sh
dirceusemighini
  • 1,344
  • 2
  • 16
  • 35
0

Try this:

hive -e 'use sample_db;show tables' | xargs -I '{}' hive -e 'use sample_db;drop table {}'

0

Below command will also work.

 hive -e 'show tables' | grep table_prefix |  while read line; do hive -e "drop table $line"; done
kamoor
  • 2,909
  • 2
  • 19
  • 34
0

fastest solution through one shell script:

drop_tables.sh pattern

Shell script content:

hive -e 'use db;show tables' | grep $1 | sed 's/^/drop table db./' | sed 's/$/;/' > temp.hql
hive -f temp.hql
rm temp.hql
user2359902
  • 121
  • 1
  • 7