我對Pig/Python相當陌生,需要幫助。試圖編寫一個協調財務數據的豬腳本。使用的參數遵循像(grand_tot,x1,x2,... xn)這樣的語法,這意味着第一個值應該等於剩餘值的總和。Python:總結一個包含浮點值的豬元組
我不知道使用Pig單獨完成此操作的方法,所以我一直在嘗試編寫一個Python UDF。豬把元組傳遞給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
我收到錯誤消息:爲+ 不支持的操作數類型(S):「詮釋」和「array.array」
我試過很多東西試圖數組值轉換爲數字,並轉換爲浮動等等我可以總結,但沒有成功。
任何想法???感謝您的期待!
這個工程,有一些調整。沒有Python,性能也有所提高(獎金)。謝謝。 – JaneQDoe
@JaneQDoe Cool.Please標記它的答案,如果它回答你的問題 –