2017-07-17 1 views
0

私は、これらの値に基づいて新しい属性を生成するために、データフレームのインスタンス値を処理するためにPysparkでUseDefinedFunctionを定義しようとしています。データフレーム上でUserDefinedFunctionを使用して「メソッド__getnewargs __([])が存在しません」というエラーをどのように解決できますか?

私はこのようなコードを持っている:私はこの行を実行すると

# I have a global attribute named 'global_dataframe' which is a dataframe containing some interesant instances. 

from pyspark.sql.functions import UserDefinedFunction 


def method1(instance, list_attribute_names): 

    if(instance['Att1'] != '-'): 
     return instance['Att1'] 
    else: 
     i = 0 
     result = "-" 
     query_SQL = "" 
     while(i < len(list_attribute_names)): 
      atr_imp = lista_2[i] 
      query_SQL = query_SQL + atr_imp + " = '" + instance[atr_imp] + "'," 
      i = i + 1 
     query_SQL = query_SQL[:-1] 

     # Here I filter the global_dataframe to get the results which are interesting according to the query generated before with the values of the instance passed to the method as a parameter 
     result_df = global_dataframe.filter(query_SQL) 
     if(result_df.head() != None):# If dataframe is empty 
      result = "None" 
     else: 
      result = query_SQL 
     return result 

def method0(df, important_attributes): 


    udf_func = UserDefinedFunction(lambda instance: method1(instance, important_attributes), StringType()) 
    column = udf_func(df) 


    df = df.withColumnRenamed("Att1", column) 
    return df 

を:

example = method0(dataframe_example, attribute_list_example) 

私は次のエラーを取得:

y4JError: An error occurred while calling o710.__getnewargs__. Trace: 
py4j.Py4JException: Method __getnewargs__([]) does not exist 
at 
py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:318) 
at 
py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:326) 
    at py4j.Gateway.invoke(Gateway.java:272) 
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132) 
    at py4j.commands.CallCommand.execute(CallCommand.java:79) 
    at py4j.GatewayConnection.run(GatewayConnection.java:214) 
    at java.lang.Thread.run(Thread.java:748) 

考え方このコードではデータフレーム上でmethod0を実行し、その属性値に応じて別の列を取得します。

エラーを解決するにはどうすればよいですか?

答えて

0

グローバル変数global_dataframeをシリアル化しようとしていることが問題だと思います。エグゼキュータはDataFrame上で操作を呼び出そうとすべきではありません。これは意味をなさないものです。エグゼキュータの範囲は、のDataFrame(またはRDD)の個々の行でのみ動作します。

global_dataframeがあらかじめ空であるかどうかを評価して、is_emptyという引数をmethod1に渡すことで解決できます。

+0

したがって、データフレームが空であるかどうかを確認するために、引数is_emptyを渡すことをお勧めします。 – jartymcfly

+0

ラムダ関数のデータフレームのインスタンスをUDFに渡し、そのインスタンスの異なる属性の値をその関数にチェックインする可能性はありますか? – jartymcfly

+0

いいえ、 'DataFrame'をUDFに渡す方法はありません。 UDFは、データセット全体ではなく、単一のデータポイントでのみ操作する必要があります。要点は、エグゼキュータによって実行される関数内から、 'DataFrame'または' RDD'上の関数を呼び出すことができないことです。 – timchap

関連する問題