| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- import sys
- import os
- import re
- abspath = os.path.abspath(__file__)
- root_path = re.sub(r"tendata-warehouse.*", "tendata-warehouse", abspath)
- sys.path.append(root_path)
- from pyhive import hive
- class HiveSQL():
- def __init__(self, host, port, username, password, database):
- self.host = host
- self.port = port
- self.username = username
- self.password = password
- self.database = database
- self.conn = self.create_connection()
- def create_connection(self):
- conn = hive.Connection(
- host=self.host,
- port=self.port,
- username=self.username,
- password=self.password,
- database=self.database
- )
- return conn
- def query(self, query):
- print("Executing query:", query)
- try:
- cursor = self.conn.cursor()
- cursor.execute(query)
- result = cursor.fetchall()
- return result
- except Exception as e:
- print("【error】:", e)
- return None
- def execute(self, sql):
- print("Executing sql:", sql)
- try:
- cursor = self.conn.cursor()
- cursor.execute(sql)
- except Exception as e:
- print("【error】:", e)
- return None
- if __name__ == '__main__':
- hive = HiveSQL('192.168.15.3',10000, 'chutianyu', None, 'test')
- print(hive.query('select current_user()'))
- queue_sql = 'SET mapreduce.job.queuename=vip'
- hive.execute(queue_sql)
- print(hive.query('select * from test.test_table1 a cross join test.test_table1 b'))
- print(hive.query('SET mapreduce.job.queuename'))
- hive.execute('insert into table test.test_table1 values (12345)')
|