java.sql.SQLExceptionPyRaisable 第二次尝试使用 Django 连接到 Athena
java.sql.SQLExceptionPyRaisable on the second attempt connecting to Athena using Django
我正在使用名为 PyAthenaJDBC 的 python 模块,以便使用提供的 JDBC 驱动程序查询 Athena。
这是 link :https://pypi.python.org/pypi/PyAthenaJDBC/
我一直面临一些顽固的问题。每当我连续两次使用 Athena 连接时,我都会收到此 java 错误。
事实上,我能够连接到 Athena、显示数据库、创建新表甚至查询内容。我正在使用 Django 构建一个应用程序并 运行 将其服务器设置为使用 Athena
但是,我不得不重新运行 服务器以使 Athena 连接再次工作,
这是我构建的 class
的一瞥
import os
import configparser
import pyathenajdbc
#Get aws credentials for the moment
aws_config_file = '~/.aws/config'
Config = configparser.ConfigParser()
Config.read(os.path.expanduser(aws_config_file))
access_key_id = Config['default']['aws_access_key_id']
secret_key_id = Config['default']['aws_secret_access_key']
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
athena_jdbc_driver_path = BASE_DIR + "/lib/static/AthenaJDBC.jar"
log_path = BASE_DIR + "/lib/static/queries.log"
class PyAthenaLoader():
def __init__(self):
pyathenajdbc.ATHENA_JAR = athena_jdbc_driver_path
def connecti(self):
self.conn = pyathenajdbc.connect(
s3_staging_dir="s3://aws-athena-query-results--us-west-2",
access_key=access_key_id,
secret_key=secret_key_id,
#profile_name = "default",
#credential_file = aws_config_file,
region_name="us-west-2",
log_path=log_path,
driver_path=athena_jdbc_driver_path
)
def databases(self):
dbs = self.query("show databases;")
return dbs
def tables(self, database):
tables = self.query("show tables in {0};".format(database))
return tables
def create(self):
self.connecti()
try:
with self.conn.cursor() as cursor:
cursor.execute(
"""CREATE EXTERNAL TABLE IF NOT EXISTS sales4 (
Day_ID date,
Product_Id string,
Store_Id string,
Sales_Units int,
Sales_Cost float,
Currency string
)
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe'
WITH SERDEPROPERTIES (
'serialization.format' = '|',
'field.delim' = '|',
'collection.delimm' = 'undefined',
'mapkey.delim' = 'undefined'
) LOCATION 's3://athena-internship/';
""")
res = cursor.description
finally:
self.conn.close()
return res
def query(self, req):
self.connecti()
try:
with self.conn.cursor() as cursor:
cursor.execute(req)
print(cursor.description)
res = cursor.fetchall()
finally:
self.conn.close()
return res
def info(self):
res = []
for i in dir(pyathenajdbc):
temp = i + ' = ' + str(dic[i])
#print(temp)
res.append(temp)
return res
用法示例:
def test(request):
athena = jdbc.PyAthenaLoader()
res = athena.query('Select * from sales;')
return render(request, 'test.html', {'data': res})
工作得很好!
但是刷新页面会导致此错误:
Error
请注意,我使用的是本地 .jar 文件:我认为这可以解决问题,但我错了
即使我去掉JDBC驱动的路径,让模块从s3下载,错误依然存在:
File "/home/tewfikghariani/.virtualenvs/venv/lib/python3.4/site-packages/pyathenajdbc/connection.py", line 69, in init
ATHENA_CONNECTION_STRING.format(region=self.region_name, schema=schema_name), props)
jpype._jexception.java.sql.SQLExceptionPyRaisable:
java.sql.SQLException: No suitable driver found for
jdbc:awsathena://athena.us-west-2.amazonaws.com:443/hive/default/
此外,当我 运行 模块本身时,它工作得很好。
当我在渲染模板之前在我的视图中设置多个连接时,效果也很好。
我想这个问题与 django 视图有关,一旦其中一个视图正在执行与 athena 的连接,下一个连接就不可能了,除非我重新启动服务器,否则会出现错误
有什么帮助吗?如果缺少其他详细信息,我会立即提供。
更新:
在 github 发布问题后,作者解决了这个问题并发布了一个完美运行的新版本。
这是 JPype 的多线程问题。
问题已回答!
我正在使用名为 PyAthenaJDBC 的 python 模块,以便使用提供的 JDBC 驱动程序查询 Athena。 这是 link :https://pypi.python.org/pypi/PyAthenaJDBC/
我一直面临一些顽固的问题。每当我连续两次使用 Athena 连接时,我都会收到此 java 错误。
事实上,我能够连接到 Athena、显示数据库、创建新表甚至查询内容。我正在使用 Django 构建一个应用程序并 运行 将其服务器设置为使用 Athena 但是,我不得不重新运行 服务器以使 Athena 连接再次工作,
这是我构建的 class
的一瞥 import os
import configparser
import pyathenajdbc
#Get aws credentials for the moment
aws_config_file = '~/.aws/config'
Config = configparser.ConfigParser()
Config.read(os.path.expanduser(aws_config_file))
access_key_id = Config['default']['aws_access_key_id']
secret_key_id = Config['default']['aws_secret_access_key']
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
athena_jdbc_driver_path = BASE_DIR + "/lib/static/AthenaJDBC.jar"
log_path = BASE_DIR + "/lib/static/queries.log"
class PyAthenaLoader():
def __init__(self):
pyathenajdbc.ATHENA_JAR = athena_jdbc_driver_path
def connecti(self):
self.conn = pyathenajdbc.connect(
s3_staging_dir="s3://aws-athena-query-results--us-west-2",
access_key=access_key_id,
secret_key=secret_key_id,
#profile_name = "default",
#credential_file = aws_config_file,
region_name="us-west-2",
log_path=log_path,
driver_path=athena_jdbc_driver_path
)
def databases(self):
dbs = self.query("show databases;")
return dbs
def tables(self, database):
tables = self.query("show tables in {0};".format(database))
return tables
def create(self):
self.connecti()
try:
with self.conn.cursor() as cursor:
cursor.execute(
"""CREATE EXTERNAL TABLE IF NOT EXISTS sales4 (
Day_ID date,
Product_Id string,
Store_Id string,
Sales_Units int,
Sales_Cost float,
Currency string
)
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe'
WITH SERDEPROPERTIES (
'serialization.format' = '|',
'field.delim' = '|',
'collection.delimm' = 'undefined',
'mapkey.delim' = 'undefined'
) LOCATION 's3://athena-internship/';
""")
res = cursor.description
finally:
self.conn.close()
return res
def query(self, req):
self.connecti()
try:
with self.conn.cursor() as cursor:
cursor.execute(req)
print(cursor.description)
res = cursor.fetchall()
finally:
self.conn.close()
return res
def info(self):
res = []
for i in dir(pyathenajdbc):
temp = i + ' = ' + str(dic[i])
#print(temp)
res.append(temp)
return res
用法示例:
def test(request):
athena = jdbc.PyAthenaLoader()
res = athena.query('Select * from sales;')
return render(request, 'test.html', {'data': res})
工作得很好! 但是刷新页面会导致此错误:
Error
请注意,我使用的是本地 .jar 文件:我认为这可以解决问题,但我错了 即使我去掉JDBC驱动的路径,让模块从s3下载,错误依然存在:
File "/home/tewfikghariani/.virtualenvs/venv/lib/python3.4/site-packages/pyathenajdbc/connection.py", line 69, in init ATHENA_CONNECTION_STRING.format(region=self.region_name, schema=schema_name), props) jpype._jexception.java.sql.SQLExceptionPyRaisable: java.sql.SQLException: No suitable driver found for jdbc:awsathena://athena.us-west-2.amazonaws.com:443/hive/default/
此外,当我 运行 模块本身时,它工作得很好。 当我在渲染模板之前在我的视图中设置多个连接时,效果也很好。
我想这个问题与 django 视图有关,一旦其中一个视图正在执行与 athena 的连接,下一个连接就不可能了,除非我重新启动服务器,否则会出现错误
有什么帮助吗?如果缺少其他详细信息,我会立即提供。
更新: 在 github 发布问题后,作者解决了这个问题并发布了一个完美运行的新版本。 这是 JPype 的多线程问题。
问题已回答!