我正在使用python模塊,名爲PyAthenaJDBC以便使用提供的JDBC驅動程序查詢Athena。 這裏是鏈接:https://pypi.python.org/pypi/PyAthenaJDBC/java.sql.SQLExceptionPyRaisable使用Django連接到Athena的第二次嘗試
我一直面臨一些持續性的問題。每當我連續兩次使用雅典娜連接時,我總是收到這個java錯誤。
事實上,我能夠連接到雅典娜,顯示數據庫,創建新表格,甚至查詢內容。我正在使用Django構建一個應用程序並運行其服務器以使用Athena 但是,爲了雅典娜連接再次運行,我必須重新運行服務器,
以下是我擁有的類的一瞥使用內置
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})
作品就好了! 但是刷新頁面將導致該錯誤:
請注意,我用的是本地.jar文件:我認爲這會解決這個問題,但我錯了 即使我刪除的路徑JDBC驅動程序,讓模塊從S3下載,錯誤仍然存在:
文件「/home/tewfikghariani/.virtualenvs/venv/lib/python3.4/site-packages/pyathenajdbc/connection.py」 69行,init ATHENA_CONNECTION_STRING .format(region = self.region_name,schema = schema_name),props) jpype._jexception.java.sql.SQLExceptionPyRaisable: java.sql.SQLException:找不到合適的驅動程序 jdbc:awsathena://athena.us- west-2.amazonaws.com:443/hive/default/
此外,當我自己運行模塊時,它工作得很好。 當我在渲染模板之前在我的視圖中設置多個連接時,該工作也很好。
我想這個問題是關係到Django的觀點,一旦其中一個視圖正在執行與雅典娜的連接,下一個連接是不可能的了,除非我重新啓動服務器
任何幫助引發的錯誤?如果其他細節丟失,我會立即提供。
PS:我不過開跑一些進一步的測試後PyAthenaJDBC github上的問題,我相信這個問題主要涉及到的Django 參考值= https://github.com/laughingman7743/PyAthenaJDBC/問題/ 7 –