Python 连接到 AWS Athena 抛出错误

标签 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)

方案二:尝试使用jaydebeapi还是一样的错误

Screenshot of Error msg

使用 Microsoft Visual Studio 进行 Python 调试时的错误消息

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

最佳答案

JayDeBeApi 太复杂,无法使用 Athena JDBC 进行调整,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)

关于Python 连接到 AWS Athena 抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43354388/

相关文章:

python - TensorFlow:在不同输出形状的数据集之间交替

c++ - Windbg:ChildEBP和RetAddr的含义

python-2.7 - 从 SQLAlchemy 子查询中选择整个实体

google-app-engine - Google App Engine 中通过实体进行全文搜索或字符串搜索以显示即时结果

java - 使用 Python 将 json 作为字节数组发送到 kafka

Python - 用列表提供对象

python - MYSQL CONNECTOR加载数据本地文件: file could not be read

c++ - 为什么按下的键的 dwControlKeyState 与常量不匹配?

java - 如何在 Java 中检查 Windows 版本?

python - 让 max(dict_name.values()) 忽略字符串