Redshift中的Lambda外部函数返回0



我创建了一个在控制台中工作的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'

你能告诉我如何才能正确地返回结果吗?我尝试了各种数据类型,但都不起作用。

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

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

最新更新