I want to automate the whole process to test a scenario where I would like to create a database, perform an operation and then delete the database. Is there any way to do it using Python 3. I tried with PyHive, but it requires the database name to connect to.
Asked
Active
Viewed 226 times
1 Answers
0
If one cannot connect to database and need to run hive commands from terminal, we can use Popen function from subprocess module to use Hive from the Terminal.
if self.is_database_exist():
print("Exists")
self.drop_hive_database()
else:
print("Not Exists")
put = Popen(["hive", "-S", "-e", "create database {0};".format(self.database)], stdin=PIPE, stdout=PIPE, bufsize=-1)
out, exp = put.communicate()
if "failed" in str(out).lower():
self.logger("Failed to create Hive Database %s!" % self.database)
raise Exception("Failed to create database")
Here self.database contains Name of the database, and is database exist function is checking if the database already exists, then we are dropping the database and recreating it.

Raunak Jalan
- 83
- 7