如何定义 Lambda 变量以通过 API 网关查询 Redshift
How to define Lambda variables to query Redshift over API Gateway
我遇到了如何为 API 网关设置参数以使用 Lambda 函数查询 Amazon Redshift 的问题。
我的连接工作正常,但我一直都满 table 响应。
我需要定义变量,用户可以查询特定的参数、值和模式
谁能推荐一个如何设置的例子
我的配置是:
#!/usr/bin/env python
import psycopg2
import logging
import traceback
import json
from os import environ
query="SELECT * from public"
logger=logging.getLogger()
logger.setLevel(logging.INFO)
def make_connection():
conn=psycopg2.connect(dbname= 'database', host='redshift-cluster.amazonaws.com',
port= '5439', user= 'user', password= 'password')
conn.autocommit=True
return conn
def log_err(errmsg):
logger.error(errmsg)
return {"body": errmsg , "headers": {}, "statusCode": 400,
"isBase64Encoded":"false"}
logger.info("Cold start complete.")
print('Loading Function')
def handler(event,context):
try:
cnx = make_connection()
cursor=cnx.cursor()
try:
cursor.execute(query)
except:
return log_err ("ERROR: Cannot execute cursor.\n{}".format(
traceback.format_exc()) )
try:
results_list=[]
for result in cursor: results_list.append(result)
print(results_list)
cursor.close()
except:
return log_err ("ERROR: Cannot retrieve query data.\n{}".format(
traceback.format_exc()))
return {"body": str(results_list), "headers": {}, "statusCode": 200,
"isBase64Encoded":"false"}
except:
return log_err("ERROR: Cannot connect to database from handler.\n{}".format(
traceback.format_exc()))
finally:
try:
cnx.close()
except:
pass
if __name__== "__main__":
handler(None,None)
与其使用 psycopg2
,我更推荐 Using the Amazon Redshift Data API。它提供了一种更简单的方法来 运行 在 Amazon Redshift 上进行查询。
首先,使用 execute_statement()
, then retrieve the results with get_statement_result()
发送查询。
我遇到了如何为 API 网关设置参数以使用 Lambda 函数查询 Amazon Redshift 的问题。
我的连接工作正常,但我一直都满 table 响应。 我需要定义变量,用户可以查询特定的参数、值和模式
谁能推荐一个如何设置的例子
我的配置是:
#!/usr/bin/env python
import psycopg2
import logging
import traceback
import json
from os import environ
query="SELECT * from public"
logger=logging.getLogger()
logger.setLevel(logging.INFO)
def make_connection():
conn=psycopg2.connect(dbname= 'database', host='redshift-cluster.amazonaws.com',
port= '5439', user= 'user', password= 'password')
conn.autocommit=True
return conn
def log_err(errmsg):
logger.error(errmsg)
return {"body": errmsg , "headers": {}, "statusCode": 400,
"isBase64Encoded":"false"}
logger.info("Cold start complete.")
print('Loading Function')
def handler(event,context):
try:
cnx = make_connection()
cursor=cnx.cursor()
try:
cursor.execute(query)
except:
return log_err ("ERROR: Cannot execute cursor.\n{}".format(
traceback.format_exc()) )
try:
results_list=[]
for result in cursor: results_list.append(result)
print(results_list)
cursor.close()
except:
return log_err ("ERROR: Cannot retrieve query data.\n{}".format(
traceback.format_exc()))
return {"body": str(results_list), "headers": {}, "statusCode": 200,
"isBase64Encoded":"false"}
except:
return log_err("ERROR: Cannot connect to database from handler.\n{}".format(
traceback.format_exc()))
finally:
try:
cnx.close()
except:
pass
if __name__== "__main__":
handler(None,None)
与其使用 psycopg2
,我更推荐 Using the Amazon Redshift Data API。它提供了一种更简单的方法来 运行 在 Amazon Redshift 上进行查询。
首先,使用 execute_statement()
, then retrieve the results with get_statement_result()
发送查询。