hive_sql.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import sys
  2. import os
  3. import re
  4. abspath = os.path.abspath(__file__)
  5. root_path = re.sub(r"tendata-warehouse.*", "tendata-warehouse", abspath)
  6. sys.path.append(root_path)
  7. from pyhive import hive
  8. class HiveSQL():
  9. def __init__(self, host, port, username, password, database):
  10. self.host = host
  11. self.port = port
  12. self.username = username
  13. self.password = password
  14. self.database = database
  15. self.conn = self.create_connection()
  16. def create_connection(self):
  17. conn = hive.Connection(
  18. host=self.host,
  19. port=self.port,
  20. username=self.username,
  21. password=self.password,
  22. database=self.database
  23. )
  24. return conn
  25. def query(self, query):
  26. print("Executing query:", query)
  27. try:
  28. cursor = self.conn.cursor()
  29. cursor.execute(query)
  30. result = cursor.fetchall()
  31. return result
  32. except Exception as e:
  33. print("【error】:", e)
  34. return None
  35. def execute(self, sql):
  36. print("Executing sql:", sql)
  37. try:
  38. cursor = self.conn.cursor()
  39. cursor.execute(sql)
  40. except Exception as e:
  41. print("【error】:", e)
  42. return None
  43. if __name__ == '__main__':
  44. hive = HiveSQL('192.168.15.3',10000, 'chutianyu', None, 'test')
  45. print(hive.query('select current_user()'))
  46. queue_sql = 'SET mapreduce.job.queuename=vip'
  47. hive.execute(queue_sql)
  48. print(hive.query('select * from test.test_table1 a cross join test.test_table1 b'))
  49. print(hive.query('SET mapreduce.job.queuename'))
  50. hive.execute('insert into table test.test_table1 values (12345)')