【发布时间】:2016-04-07 18:41:03
【问题描述】:
我对 Pig/Python 还很陌生,需要帮助。尝试编写一个协调财务数据的 Pig 脚本。使用的参数遵循类似 (grand_tot, x1, x2,... xn) 的语法,这意味着第一个值应该等于剩余值的总和。
我不知道如何单独使用 Pig 来完成此任务,因此我一直在尝试编写 Python UDF。 Pig 将一个元组传递给 Python;如果 x1:xn 之和等于 grand_tot,则 Python 应向 Pig 返回“1”以表明数字匹配,否则返回“0”。
这是我目前所拥有的:
register 'myudf.py' using jython as myfuncs;
A = LOAD '$file_nm' USING PigStorage(',') AS (grand_tot,west_region,east_region,prod_line_a,prod_line_b, prod_line_c, prod_line_d);
A1 = GROUP A ALL;
B = FOREACH A1 GENERATE TOTUPLE($recon1) as flds;
C = FOREACH B GENERATE myfuncs.isReconciled(flds) AS res;
DUMP C;
$recon1作为参数传递,定义为:
grand_tot, west_region, east_region
我稍后会将 $recon2 传递为:
grand_tot, prod_line_a, prod_line_b, prod_line_c, prod_line_d
示例数据行(在 $file_nm 中)如下所示:
grand_tot,west_region,east_region,prod_line_a,prod_line_b, prod_line_c, prod_line_d
10000,4500,5500,900,2200,450,3700,2750
12500,7500,5000,3180,2770,300,3950,2300
9900,7425,2475,1320,460,3070,4630,1740
最后...这是我正在尝试使用 Python UDF 代码执行的操作:
@outputSchema("result")
def isReconciled(arrTuple):
arrTemp = []
arrNew = []
string1 = ""
result = 0
## the first element of the Tuple should be the sum of remaining values
varGrandTot = arrTuple[0]
## create a new array with the remaining Tuple values
arrTemp = arrTuple[1:]
for item in arrTuple:
arrNew.append(item)
## sum the second to the nth values
varSum = sum(arrNew)
## if the first value in the tuple equals the sum of all remaining values
if varGrandTot = varSum then:
#reconciled to the penny
result = 1
else:
result = 0
return result
我收到的错误信息是: + 不支持的操作数类型:'int' 和 'array.array'
我尝试了许多尝试将数组值转换为数字并转换为浮点数以便求和的方法,但没有成功。
有什么想法吗???感谢收看!
【问题讨论】:
标签: python tuples apache-pig