无法使用 Python 从 AWS Glue Database/Tables 读取数据
Unable to read data from AWS Glue Database/Tables using Python
我的要求是使用 python 脚本将数据从 AWS Glue 数据库读取到数据帧中。当我研究时,我与图书馆战斗 - “awswrangler”。我正在使用以下代码连接和读取数据:
import awswrangler as wr
profile_name = 'aws_profile_dev'
REGION = 'us-east-1'
#Retreiving credentials to connect to AWS
ACCESS_KEY_ID, SECRET_ACCESS_KEY,SESSION_TOKEN = get_profile_credentials(profile_name)
session = boto3.session.Session(
aws_access_key_id=ACCESS_KEY_ID,
aws_secret_access_key=SECRET_ACCESS_KEY,
aws_session_token=SESSION_TOKEN
)
my_df= wr.athena.read_sql_table(table= 'mytable_1', database= 'shared_db', boto3_session=session)
但是,当我 运行 执行上述代码时,出现以下错误 - “ValueError:第 0 年超出范围”
或者,我尝试使用另一个库 - “pyathena”。我尝试使用的代码是:
from pyathena import connect
import pandas as pd
conn = connect(aws_access_key_id=ACCESS_KEY_ID,
aws_secret_access_key=SECRET_ACCESS_KEY,
aws_session_token=SESSION_TOKEN,
s3_staging_dir='s3://my-sample-bucket/',
region_name='us-east-1')
df = pd.read_sql("select * from AwsDataCatalog.shared_db.mytable_1 limit 1000", conn)
使用它,我能够检索数据,但它仅在我使用限制时有效。即..,如果我只是 运行ning 查询而没有 limit 即..,“select * 来自 AwsDataCatalog.shared_db.mytable_1” , 它给出错误 - ValueError: year 0 is out of range
奇怪的行为 - 例如,如果我 运行:
df = pd.read_sql("select * from AwsDataCatalog.shared_db.mytable_1 limit 1200", conn)
有时它会给出相同的错误,如果我只是减少限制值和 运行(例如限制 1199),稍后当我再次 运行 它返回限制 1200 时作品。但是如果我试图读取超过 ~1300 行,这就不起作用了。我在 table 中总共有 2002 行。我需要阅读整个 table.
请帮忙!谢谢!
在 python 中使用以下代码获取您要查找的数据。
import boto3
query = "SELECT * from table_name"
s3_resource = boto3.resource("s3")
s3_client = boto3.client('s3')
DATABASE = 'database_name'
output='s3://output-bucket/output-folder'
athena_client = boto3.client('athena')
# Execution
response = athena_client.start_query_execution(
QueryString=query,
QueryExecutionContext={
'Database': DATABASE
},
ResultConfiguration={
'OutputLocation': output,
}
)
queryId = response['QueryExecutionId']
我找到了一种使用 awswrangler 将数据直接从 Athena 查询到本地计算机上的 pandas 数据帧的方法。这不需要我们在 S3 上提供输出位置。
profile_name = 'Dev-AWS'
REGION = 'us-east-1'
#this automatically retrieves credentials from your aws credentials file after you run aws configure on command-line
ACCESS_KEY_ID, SECRET_ACCESS_KEY,SESSION_TOKEN = get_profile_credentials(profile_name)
session = boto3.session.Session(
aws_access_key_id=ACCESS_KEY_ID,
aws_secret_access_key=SECRET_ACCESS_KEY,
aws_session_token=SESSION_TOKEN
)
wr.athena.read_sql_query("select * from table_name", database="db_name", boto3_session=session)
或者,如果您不想查询 Athena,但想阅读整个胶水 table,您可以使用:
my_df = wr.athena.read_sql_table(table= 'my_table', database= 'my_db', boto3_session=session)
我的要求是使用 python 脚本将数据从 AWS Glue 数据库读取到数据帧中。当我研究时,我与图书馆战斗 - “awswrangler”。我正在使用以下代码连接和读取数据:
import awswrangler as wr
profile_name = 'aws_profile_dev'
REGION = 'us-east-1'
#Retreiving credentials to connect to AWS
ACCESS_KEY_ID, SECRET_ACCESS_KEY,SESSION_TOKEN = get_profile_credentials(profile_name)
session = boto3.session.Session(
aws_access_key_id=ACCESS_KEY_ID,
aws_secret_access_key=SECRET_ACCESS_KEY,
aws_session_token=SESSION_TOKEN
)
my_df= wr.athena.read_sql_table(table= 'mytable_1', database= 'shared_db', boto3_session=session)
但是,当我 运行 执行上述代码时,出现以下错误 - “ValueError:第 0 年超出范围”
或者,我尝试使用另一个库 - “pyathena”。我尝试使用的代码是:
from pyathena import connect
import pandas as pd
conn = connect(aws_access_key_id=ACCESS_KEY_ID,
aws_secret_access_key=SECRET_ACCESS_KEY,
aws_session_token=SESSION_TOKEN,
s3_staging_dir='s3://my-sample-bucket/',
region_name='us-east-1')
df = pd.read_sql("select * from AwsDataCatalog.shared_db.mytable_1 limit 1000", conn)
使用它,我能够检索数据,但它仅在我使用限制时有效。即..,如果我只是 运行ning 查询而没有 limit 即..,“select * 来自 AwsDataCatalog.shared_db.mytable_1” , 它给出错误 - ValueError: year 0 is out of range
奇怪的行为 - 例如,如果我 运行:
df = pd.read_sql("select * from AwsDataCatalog.shared_db.mytable_1 limit 1200", conn)
有时它会给出相同的错误,如果我只是减少限制值和 运行(例如限制 1199),稍后当我再次 运行 它返回限制 1200 时作品。但是如果我试图读取超过 ~1300 行,这就不起作用了。我在 table 中总共有 2002 行。我需要阅读整个 table.
请帮忙!谢谢!
在 python 中使用以下代码获取您要查找的数据。
import boto3
query = "SELECT * from table_name"
s3_resource = boto3.resource("s3")
s3_client = boto3.client('s3')
DATABASE = 'database_name'
output='s3://output-bucket/output-folder'
athena_client = boto3.client('athena')
# Execution
response = athena_client.start_query_execution(
QueryString=query,
QueryExecutionContext={
'Database': DATABASE
},
ResultConfiguration={
'OutputLocation': output,
}
)
queryId = response['QueryExecutionId']
我找到了一种使用 awswrangler 将数据直接从 Athena 查询到本地计算机上的 pandas 数据帧的方法。这不需要我们在 S3 上提供输出位置。
profile_name = 'Dev-AWS'
REGION = 'us-east-1'
#this automatically retrieves credentials from your aws credentials file after you run aws configure on command-line
ACCESS_KEY_ID, SECRET_ACCESS_KEY,SESSION_TOKEN = get_profile_credentials(profile_name)
session = boto3.session.Session(
aws_access_key_id=ACCESS_KEY_ID,
aws_secret_access_key=SECRET_ACCESS_KEY,
aws_session_token=SESSION_TOKEN
)
wr.athena.read_sql_query("select * from table_name", database="db_name", boto3_session=session)
或者,如果您不想查询 Athena,但想阅读整个胶水 table,您可以使用:
my_df = wr.athena.read_sql_table(table= 'my_table', database= 'my_db', boto3_session=session)