2016-04-07 12 views
0

私は、Pig/Pythonとか、助けが必要なのはかなり新しいです。財務データを調整する豚スクリプトを作成しようとしています。使用されるパラメータは、(grand_tot、x1、x2、... xn)のような構文に従います。つまり、最初の値は残りの値の合計と等しくなければなりません。Python:浮動小数点値を含むブタのタプルを合計する

私は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がに)のように見えるデータの:

@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 

私はエラーメッセージがある:最後

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コードでやろうとしているものです:+用 サポートされていないオペランドのタイプ(S):「INT」と「array.array」

私が数値に配列の値を変換するので、float型に変換しようと、多くの事を試してみました私は合計することができますが、成功しません。

見てくれてありがとう!

答えて

1

これはPIG自体で行うことができます。

まず、スキーマにデータ型を指定します。 PigStorageはbytearrayをデフォルトのデータ型として使用します。あなたのpythonスクリプトがエラーをスローしています。あなたのサンプルデータがintを持っているように見えますが、あなたの質問ではfloatについて言及しました。

第2に、2番目のフィールドまたは選択したフィールドから開始するフィールドを追加します。

第3に、bincond演算子を使用して、最初のフィールド値を合計で確認します。

A = LOAD '$file_nm' USING PigStorage(',') AS (grand_tot:float,west_region:float,east_region:float,prod_line_a:float,prod_line_b:float, prod_line_c:float, prod_line_d:float); 
A1 = FOREACH A GENERATE grand_tot,SUM(TOBAG(prod_line_a,prod_line_b,prod_line_c,prod_line_d)) as SUM_ALL; 
B = FOREACH A1 GENERATE (grand_tot == SUM_ALL ? 1 : 0); 
DUMP B; 
+0

これは、いくつかの調整を加えて動作します。パフォーマンスもPythonなしで向上しました(ボーナス)。ありがとう。 – JaneQDoe

+0

@JaneQDoe Cool.Pleaseあなたの質問に答えた場合はそれをマークしてください –

0

arrTupleは数字の配列ではありませんが、配列の一部である可能性があります。

、それをチェックするいくつかのチェックを追加して、コードを変更するには:

@outputSchema("result") 
def isReconciled(arrTuple): 
    # some checks 
    tmpl = "Item # {i} shall be a number (has value {itm} of type {tp})" 
    for i, num in enumerate(arrTuple): 
     msg = templ.format(i=i, itm=itm, tp=type(itm)) 
     assert isinstance(arrTuple[0], (int, long, float)), msg 
    # end of checks 

    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 

それは、項目の一つにAssertionFailed例外がスローされますことを、非常に可能性があります。 アサーションメッセージを読んで、どの項目が問題を起こしているかを確認します。あなたが最初の数は、配列の残りの部分の合計に等しい場合は0または1を返すようにしたい場合は

とにかく、 に従うこと、あまりにも動作します:

@outputSchema("result") 
def isReconciled(arrTuple): 
    if arrTuple[0] == sum(arrTuple[1:]): 
     return 1 
    else: 
     return 0 

と場合には、あなたがTrueを得ることに満足して生きるでしょうが1の代わりに1とFalseの代わりに0:

@outputSchema("result") 
def isReconciled(arrTuple): 
    return arrTuple[0] == sum(arrTuple[1:]) 
関連する問題