Python 使用Boto3在RDS上与amazon Aurora交互
我已经使用AmazonAurora在AmazonRDS中建立了一个数据库,并希望使用Python与该数据库进行交互——显而易见的选择是使用Boto 但是,他们的文档很糟糕,没有涵盖我可以与数据库交互的方式,以便:Python 使用Boto3在RDS上与amazon Aurora交互,python,mysql,amazon-web-services,boto3,Python,Mysql,Amazon Web Services,Boto3,我已经使用AmazonAurora在AmazonRDS中建立了一个数据库,并希望使用Python与该数据库进行交互——显而易见的选择是使用Boto 但是,他们的文档很糟糕,没有涵盖我可以与数据库交互的方式,以便: 使用SQL语句运行查询 与数据库中的表交互 等 是否有人有一些示例/教程的链接,或者知道如何执行这些任务?当使用Amazon RDS产品(包括Aurora)时,您不会通过任何AWS API(包括Boto)连接到数据库。相反,您将使用所选数据库的本机客户端。对于Aurora,您可以使
- 使用SQL语句运行查询
- 与数据库中的表交互
- 等
是否有人有一些示例/教程的链接,或者知道如何执行这些任务?当使用Amazon RDS产品(包括Aurora)时,您不会通过任何AWS API(包括Boto)连接到数据库。相反,您将使用所选数据库的本机客户端。对于Aurora,您可以使用MySQL命令行客户端进行连接。从那里,您可以像查询任何其他MySQL数据库一样查询它 “入门”文档中有一个简短的部分介绍了如何连接到Aurora数据库:
这里有几个例子: 插入示例:
import boto3
sql = """
INSERT INTO YOUR_TABLE_NAME_HERE
(
your_column_name_1
,your_column_name_2
,your_column_name_3)
VALUES(
:your_param_1_name
,:your_param_2_name)
,:your_param_3_name
"""
param1 = {'name':'your_param_1_name', 'value':{'longValue': 5}}
param2 = {'name':'your_param_2_name', 'value':{'longValue': 63}}
param3 = {'name':'your_param_3_name', 'value':{'stringValue': 'para bailar la bamba'}}
param_set = [param1, param2, param3]
db_clust_arn = 'your_db_cluster_arn_here'
db_secret_arn = 'your_db_secret_arn_here'
rds_data = boto3.client('rds-data')
response = rds_data.execute_statement(
resourceArn = db_clust_arn,
secretArn = db_secret_arn,
database = 'your_database_name_here',
sql = sql,
parameters = param_set)
print(str(response))
import boto3
rds_data = boto3.client('rds-data')
db_clust_arn = 'your_db_cluster_arn_here'
db_secret_arn = 'your_db_secret_arn_here'
employee_id = 35853
get_vacation_days_sql = f"""
select vacation_days_remaining
from employees_tbl
where employee_id = {employee_id}
"""
response1 = rds_data.execute_statement(
resourceArn = db_clust_arn,
secretArn = db_secret_arn,
database = 'your_database_name_here',
sql = get_vacation_days_sql)
#recs is a list (of rows returned from Db)
recs = response1['records']
print(f"recs === {recs}")
#recs === [[{'longValue': 57}]]
#single_row is a list of dictionaries, where each dictionary represents a
#column from that single row
for single_row in recs:
print(f"single_row === {single_row}")
#single_row === [{'longValue': 57}]
#one_dict is a dictionary with one key value pair
#where the key is the data type of the column and the
#value is the value of the column
#each additional column is another dictionary
for single_column_dict in single_row:
print(f"one_dict === {single_column_dict}")
# one_dict === {'longValue': 57}
vacation_days_remaining = single_column_dict['longValue']
print(f'vacation days remaining === {vacation_days_remaining}')
阅读示例:
import boto3
sql = """
INSERT INTO YOUR_TABLE_NAME_HERE
(
your_column_name_1
,your_column_name_2
,your_column_name_3)
VALUES(
:your_param_1_name
,:your_param_2_name)
,:your_param_3_name
"""
param1 = {'name':'your_param_1_name', 'value':{'longValue': 5}}
param2 = {'name':'your_param_2_name', 'value':{'longValue': 63}}
param3 = {'name':'your_param_3_name', 'value':{'stringValue': 'para bailar la bamba'}}
param_set = [param1, param2, param3]
db_clust_arn = 'your_db_cluster_arn_here'
db_secret_arn = 'your_db_secret_arn_here'
rds_data = boto3.client('rds-data')
response = rds_data.execute_statement(
resourceArn = db_clust_arn,
secretArn = db_secret_arn,
database = 'your_database_name_here',
sql = sql,
parameters = param_set)
print(str(response))
import boto3
rds_data = boto3.client('rds-data')
db_clust_arn = 'your_db_cluster_arn_here'
db_secret_arn = 'your_db_secret_arn_here'
employee_id = 35853
get_vacation_days_sql = f"""
select vacation_days_remaining
from employees_tbl
where employee_id = {employee_id}
"""
response1 = rds_data.execute_statement(
resourceArn = db_clust_arn,
secretArn = db_secret_arn,
database = 'your_database_name_here',
sql = get_vacation_days_sql)
#recs is a list (of rows returned from Db)
recs = response1['records']
print(f"recs === {recs}")
#recs === [[{'longValue': 57}]]
#single_row is a list of dictionaries, where each dictionary represents a
#column from that single row
for single_row in recs:
print(f"single_row === {single_row}")
#single_row === [{'longValue': 57}]
#one_dict is a dictionary with one key value pair
#where the key is the data type of the column and the
#value is the value of the column
#each additional column is another dictionary
for single_column_dict in single_row:
print(f"one_dict === {single_column_dict}")
# one_dict === {'longValue': 57}
vacation_days_remaining = single_column_dict['longValue']
print(f'vacation days remaining === {vacation_days_remaining}')
源链接:
谢谢。请原谅我的幼稚,但我该如何创建和使用API来通过查询获取数据,以便在web/移动应用程序中使用呢?例如,是否有一组服务或API?@user3024827-Nope。没有API。您只需像连接本地数据库一样连接到数据库。RDS只不过是一个托管数据库解决方案。那么这是否意味着它不能用作应用程序的数据库?再次道歉,如果这是一个愚蠢的问题,我是非常新的@user3024827-它绝对可以用作应用程序的数据库。我只是说,您对待它的方式与在本地运行数据库服务器的方式没有任何区别。我觉得这个答案可能会产生更多的歧义,并指导人们寻找使用Boto3连接到Aurora的方法。有一些API可以使用boto3连接到“rds数据”,然后运行查询。这在尝试使用secret manager而不是直接使用用户名密码进行连接时特别有用。在Glue上运行时,Boto3中没有rds数据客户端