-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateHiveFuncution.py
33 lines (28 loc) · 1.09 KB
/
createHiveFuncution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# coding=UTF-8
from pyhive import hive # 用于创建hive tabel
def run_mysql_query(conn, sql):
with conn.cursor() as cursor:
cursor.execute(sql)
# print(type(cursor.fetchall()))
# print(type(cursor.fetchall()))
return cursor.fetchall()
def create_table(host="localhost", database="", newTableName="", attributeList=''' '''):
"""
:param host:
:param database:
:param newTableName:
:param attributeList: exp: `province` varchar(255) , `goods_type` varchar(255)
:return:
"""
print(attributeList)
conn = hive.connect(host=host, database=database)
if (newTableName,) not in list(run_mysql_query(conn, "show tables")):
sql = '''
create table `{:s}`.`{:s}` ({:s})
'''.format(database, newTableName, attributeList)
cursor = conn.cursor()
cursor.execute(sql)
conn.close()
return
if __name__=="__main__":
create_table(host="localhost", database="Metro_Hangzhou", newTableName="test_inputstation", attributeList=''' `stationID` varchar(255), `inputNums` varchar(255) ''')