与AWS Athena抛出错误的Python连接

时间:2017-04-11 19:12:11

标签: python windows python-2.7 amazon-web-services amazon-athena

我正在尝试通过Python 2.7.13运行AWS Athena SQL查询,并遵循以下两个选项,但在这两种情况下都" python.exe停止工作" 错误。 我是python的新手,非常感谢任何帮助。

选项1:尝试使用Pyathenajdbc

>>> from pyathenajdbc import connect

>>> import pandas as pd

>>> conn = connect(access_key='<acess_key>',
               secret_key='<secret_key>',
               s3_staging_dir='s3://Test/',
               region_name='<region_name>',
               jvm_path='C:\\Program Files (x86)\\Java\\jre6\\bin\\client\\jvm.dll')

>>> df = pd.read_sql("select * from test.test45 LIMIT 1", conn)

选项2:尝试使用jaydebeapi仍然是同样的错误

this question

使用Microsoft Visual Studio进行Python调试中的

错误消息

python.exe中0x00170000处的未处理异常:0xC0000005:访问冲突。

1 个答案:

答案 0 :(得分:1)

使用Athena JDBC调整JayDeBeApi太复杂了,PyAthenajdbc更容易使用。 这就是我使用它的方式,它就像一个魅力!

声明

import os
import configparser
import pyathenajdbc


# Get aws credentials 
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__)))
log_path = BASE_DIR + "/lib/static/queries.log"

class PyAthenaLoader():    
    def connecti(self):
        self.conn = pyathenajdbc.connect(
            s3_staging_dir="s3://athena",
            access_key=access_key_id,
            secret_key=secret_key_id,
            region_name="us-east-1",
            log_path=log_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 query(self, req):
        self.connecti()

        try:
            with self.conn.cursor() as cursor:
                cursor.execute(req)
                res = cursor.fetchall()
        except Exception as X:
            return X
        finally:
            self.conn.close()
        return res

用法

athena = PyAthenaLoader()
res = athena.query('SELECT * from shadow.sales;')
print(res)