【问题标题】:Lambda external function in Redshift returns 0Redshift 中的 Lambda 外部函数返回 0
【发布时间】:2022-01-22 16:58:49
【问题描述】:

我创建了一个在控制台中工作的 Lambda 函数(调用 SciPy 包)。但不会在 Redshift.i 中返回预期结果

这是 Lambda 函数代码:

import json
from scipy import stats


def binomial_test(event, context):
    t1 = event['arguments']
    # 'len(t1)' represents the number of rows in the request payload.
    # The number of results in the response payload should be the same as the number of rows received.
    resp = [None]*len(t1)

    # By default success is set to 'True'.
    success = True
    # Iterating over all rows in the request payload.
    i = 0
    for x in t1:
        p_value = stats.binomtest(x[0], x[1], x[2], x[3]).pvalue
        resp[i] = p_value
        i = i+1
    ret = dict()
    ret['success'] = success
    if not success:
        ret['error_msg'] = "Invalid values"
    else:
        ret['results'] = resp

    ret_json = json.dumps(ret)
    return ret_json

这是 AWS 控制台中的结果,因此您可以看到 Lambda 可以正常工作:

"{\"success\": true, \"results\": [0.5381831001889082]}"

但在 Redshift 中,该函数只返回值 0

这里是 UDF 定义:

CREATE EXTERNAL FUNCTION stats_binom_test (BIGINT, BIGINT, NUMERIC, VARCHAR)
RETURNS DOUBLE PRECISION
IMMUTABLE
LAMBDA 'stats_binom_test' 
IAM_ROLE 'arn:aws:iam::xxxxxxxxxxxx'

您能告诉我如何正确地返回结果吗? 我尝试了各种数据类型,但没有任何效果。

【问题讨论】:

    标签: amazon-web-services function aws-lambda amazon-redshift external


    【解决方案1】:

    当您从 python 函数返回 p_value 时,将它们转换为字符串而不是数值。 IE。当json.dumps(ret)ret 对象转换为字符串时,它不会用双引号将p_value 值括起来。这会导致 0 导致 Redshift。

    例如:您可以将resp[i] = p_value 行重写为resp[i] = str(p_value)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-19
      • 1970-01-01
      • 2015-11-21
      • 1970-01-01
      • 1970-01-01
      • 2023-02-23
      相关资源
      最近更新 更多