0

我一直在使用带有 python lambda 函数的 AWS aurora 来为我们的应用程序执行查询。lambda 函数效果很好,但只返回前 1000 个结果而不是全部。我尝试使用分页器将限制增加到 5000,但找不到合适的解决方案:

import boto3
def lambda_handler(event, context):

   client = boto3.client('rds-data')
   readParam = event['query'] # readParam = 'select * from table;'
   database1 = event['database'] # Database name

   response = client.execute_sql(
      awsSecretStoreArn='arn:aws:secretsmanager:us-east-1:xxxxx:secret:abc/read-XXXX',
      database=database1,
      dbClusterOrInstanceArn='arn:aws:rds:us-east-1:xxxxx:cluster:abcd-abc',
      sqlStatements=readParam
   )

   return {
      'statusCode': 200,
      'headers': {
          'Content-Type': 'application/json',
          "Access-Control-Allow-Origin": "*",
          "Access-Control-Allow-Headers": "Content-Type",
          "Access-Control-Allow-Methods": "OPTIONS,POST"
       },
      'body': response
  }

`

我曾尝试使用 SQLAlchemy 和 pydataapi 并将 AWS 开发包部署到 lambda,但这不起作用。lambda 函数不会读取具有 lambda_handler 的相应 python 文件。代码如下:

import pymysql.cursors
from sqlalchemy.engine import create_engine
  def lambda_handler(event, context):
    readParam = event['query']
    database1 = event['database']

    engine = create_engine(
       'mysql+pydataapi://',
        connect_args={
           'resource_arn': 'arn:aws:rds:us-east-1:xxxx:cluster:abcd-abc',
           'secret_arn': 'arn:aws:secretsmanager:us-east-1:xxxx:secret:abc/read-XXXX',
        'database': 'mimic_dev'
    }
)

result: ResultProxy = engine.execute(readParam)

return {
    'statusCode': 200,
    'headers': {
        'Content-Type': 'application/json',
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Headers": "Content-Type",
        "Access-Control-Allow-Methods": "OPTIONS,POST"
    },
    'body': result.fetchall
}

对于我一直在尝试的方法,有没有更好的替代解决方案?任何帮助表示赞赏。谢谢你

4

1 回答 1

0

此问题已得到解决。资源:Python 中的 AWS lambda 部署包

以及以下代码:

import pymysql.cursors
import json

from sqlalchemy.engine import create_engine

def lambda_handler(event,context):
   engine = create_engine(
       'mysql+pydataapi://',
        connect_args={
        'resource_arn': 'arn:aws:rds:us-east-1:xxxxx:cluster:xxxx',
        'secret_arn': 'arn:aws:secretsmanager:us-east-1:xxxx:secret:decima/abcd-abc',
        'database': 'mimic_dev'
    }
)
   result: ResultProxy = engine.execute("select * from person limit 10000")
   resultValue = result.fetchall()
   return json.dumps([dict(r) for r in resultValue])
于 2020-06-15T19:43:44.460 回答