Python 连接 Presto
下面是python连接presto的工具类,在连接presto的时候也遇到了点小坑,在执行插入时,遇到了坑。。。。。。
欢迎各位指正,共同学习,共同进步
import prestodb
class prestoDBUtil(object):
def getConnect(self):
prestoConnection = prestodb.dbapi.connect(
host='111.111.111.111',
port=9998,
user='root',
catalog='hive',
schema='default'
)
return prestoConnection
def close(self):
try:
self.cursor.close()
except Exception as ex:
print(ex)
# 执行sql
def executeSelect(self, sql):
conn = self.getConnect()
cursor = conn.cursor()
cursor.execute(sql)
try:
result = cursor.fetchall()
columns_names = [col[0] for col in cursor.description]
except Exception as e:
print(columns_names)
print(e)
return result
def executeInsert(self, sql):
conn = self.getConnect()
cursor = conn.cursor()
try:
n = cursor.execute(sql)
for i in n:
print(i)
except Exception as e:
print(e)
conn.commit()
cursor.close()
conn.close()