Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

how to remove tables starting with a specific string

avatar

i have 100 tables Test_1,Test_2......Test80 test90 out of which some 70 tables start with string Test_ need to be removed.

I need to use only hive queries, nothing from unix just pure hive commands, I am looking for something like this

drop table where tablename like Test_*

2 REPLIES 2

avatar

Hive does not support like semantics for drop table, see official DDL semantics here. You will need multiple calls to accomplish this and handle them in an external driver.

avatar
Rising Star

Just to elaborate the answer from @Deepesh, here is an example, how you can accomplish your goal.

1) save a list of tables you want to drop into a file

echo "show tables like 'test_*';" > tables.sql
beeline -u 'jdbc:hive2://localhost:2181/;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2' -f tables.sql  > tables.txt 

2) from this list, generate a list of drop tables commands; for example, you can use the following python script, generate_droptables.py:

f = 'tables.txt' 
f = open(f)
lines = f.readlines()
def process_line(line):
  if line.startswith('|') and 'test_' in line:
    return line[1:].strip()[:-1].strip()
names = list(map(process_line, lines))
names = list(filter(lambda x : x, names))
out = open('droptables.sql', 'w')
for item in names:
  out.write("drop table %s;\n" % item)

3) run the list drop tables command

python generate_droptables.py
beeline -u 'jdbc:hive2://localhost:2181/;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2' -f droptables.sql