2

我正在使用名为 PyAthenaJDBC 的 python 模块,以便使用提供的 JDBC 驱动程序查询 Athena。这是链接:https ://pypi.python.org/pypi/PyAthenaJDBC/

我一直面临一些持久的问题。每当我连续两次使用 Athena 连接时,我都会收到此 java 错误。

事实上,我能够连接到 Athena、显示数据库、创建新表甚至查询内容。我正在使用 Django 构建应用程序并运行其服务器以使用 Athena 但是,我必须重新运行服务器才能使 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 视图有关,一旦其中一个视图执行与 athena 的连接,下一个连接就不可能了,除非我重新启动服务器,否则会引发错误

有什么帮助吗?如果缺少其他详细信息,我会立即提供。

4

1 回答 1

1

更新:在github上发布问题后,作者解决了这个问题并发布了一个完美运行的新版本。这是 JPype 的多线程问题。

问题已回答!

参考:https ://github.com/laughingman7743/PyAthenaJDBC/pull/8

于 2017-03-21T14:43:22.247 回答