使用Boto3与RDS上的亚马逊极光交互
Using Boto3 to interact with amazon Aurora on RDS
我已经使用 Amazon Aurora 在 Amazon RDS 中设置了一个数据库,并希望使用 Python 与数据库进行交互 - 显而易见的选择是使用 Boto。
但是,他们的文档很糟糕,并且没有涵盖我可以与数据库交互的方式:
- 运行 查询 SQL 语句
- 与数据库中的表交互
- 等等
有没有人有一些 examples/tutorials 的链接,或者知道如何完成这些任务?
使用 Amazon RDS 产品(包括 Aurora)时,您不会通过任何 AWS API(包括 Boto)连接到数据库。相反,您将使用所选数据库的本机客户端。对于 Aurora,您将使用 MySQL 命令行客户端进行连接。从那里,您可以像查询任何其他 MySQL 数据库一样查询它。
"Getting Started" 文档中有一个简短的部分讨论了如何连接到您的 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}')
来源Link:
https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/data-api.html#data-api.calling.python
我已经使用 Amazon Aurora 在 Amazon RDS 中设置了一个数据库,并希望使用 Python 与数据库进行交互 - 显而易见的选择是使用 Boto。
但是,他们的文档很糟糕,并且没有涵盖我可以与数据库交互的方式:
- 运行 查询 SQL 语句
- 与数据库中的表交互
- 等等
有没有人有一些 examples/tutorials 的链接,或者知道如何完成这些任务?
使用 Amazon RDS 产品(包括 Aurora)时,您不会通过任何 AWS API(包括 Boto)连接到数据库。相反,您将使用所选数据库的本机客户端。对于 Aurora,您将使用 MySQL 命令行客户端进行连接。从那里,您可以像查询任何其他 MySQL 数据库一样查询它。
"Getting Started" 文档中有一个简短的部分讨论了如何连接到您的 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}')
来源Link: https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/data-api.html#data-api.calling.python