web-dev-qa-db-ja.com

PySpark pandas_udfs Java.lang.IllegalArgumentExceptionエラー

Windowsで実行されているローカルのpysparkセッションで pandas UDFs を使用した経験がある人はいますか?私はそれらをLinuxで使用して良い結果を得ましたが、私のWindowsマシンでは失敗しました。

環境:

python==3.7
pyarrow==0.15
pyspark==2.3.4
pandas==0.24

Java version "1.8.0_74"

サンプルスクリプト:

from pyspark.sql.functions import pandas_udf, PandasUDFType
from pyspark.sql import SparkSession

spark = SparkSession.builder.master("local").getOrCreate()
spark.conf.set("spark.sql.execution.arrow.enabled", "true")
spark.conf.set("spark.sql.execution.arrow.fallback.enabled", "false")

df = spark.createDataFrame(
    [(1, 1.0), (1, 2.0), (2, 3.0), (2, 5.0), (2, 10.0)],
    ("id", "v"))


@pandas_udf("id long, v double", PandasUDFType.GROUPED_MAP)
def subtract_mean(pdf):
    # pdf is a pandas.DataFrame
    v = pdf.v
    return pdf.assign(v=v - v.mean())


out_df = df.groupby("id").apply(subtract_mean).toPandas()
print(out_df.head())

# +---+----+
# | id|   v|
# +---+----+
# |  1|-0.5|
# |  1| 0.5|
# |  2|-3.0|
# |  2|-1.0|
# |  2| 4.0|
# +---+----+

長い時間実行した後(toPandasステージを200秒のタスクに分割し、それぞれ1秒を引き継ぎます)、次のようなエラーを返します。

Traceback (most recent call last):
  File "C:\miniconda3\envs\pandas_udf\lib\site-packages\pyspark\sql\dataframe.py", line 1953, in toPandas
    tables = self._collectAsArrow()
  File "C:\miniconda3\envs\pandas_udf\lib\site-packages\pyspark\sql\dataframe.py", line 2004, in _collectAsArrow
    sock_info = self._jdf.collectAsArrowToPython()
  File "C:\miniconda3\envs\pandas_udf\lib\site-packages\py4j\Java_gateway.py", line 1257, in __call__
    answer, self.gateway_client, self.target_id, self.name)
  File "C:\miniconda3\envs\pandas_udf\lib\site-packages\pyspark\sql\utils.py", line 63, in deco
    return f(*a, **kw)
  File "C:\miniconda3\envs\pandas_udf\lib\site-packages\py4j\protocol.py", line 328, in get_return_value
    format(target_id, ".", name), value)
py4j.protocol.Py4JJavaError: An error occurred while calling o62.collectAsArrowToPython.
: org.Apache.spark.SparkException: Job aborted due to stage failure: Task 69 in stage 3.0 failed 1 times, most recent failure: Lost task 69.0 in stage 3.0 (TID 201, localhost, executor driver): Java.lang.IllegalArgumentException
    at Java.nio.ByteBuffer.allocate(Unknown Source)
    at org.Apache.arrow.vector.ipc.message.MessageChannelReader.readNextMessage(MessageChannelReader.Java:64)
    at org.Apache.arrow.vector.ipc.message.MessageSerializer.deserializeSchema(MessageSerializer.Java:104)
    at org.Apache.arrow.vector.ipc.ArrowStreamReader.readSchema(ArrowStreamReader.Java:128)
    at org.Apache.arrow.vector.ipc.ArrowReader.initialize(ArrowReader.Java:181)
    at org.Apache.arrow.vector.ipc.ArrowReader.ensureInitialized(ArrowReader.Java:172)
    at org.Apache.arrow.vector.ipc.ArrowReader.getVectorSchemaRoot(ArrowReader.Java:65)
    at org.Apache.spark.sql.execution.python.ArrowPythonRunner$$anon$1.read(ArrowPythonRunner.scala:161)
    at org.Apache.spark.sql.execution.python.ArrowPythonRunner$$anon$1.read(ArrowPythonRunner.scala:121)
    at org.Apache.spark.api.python.BasePythonRunner$ReaderIterator.hasNext(PythonRunner.scala:290)
    at org.Apache.spark.InterruptibleIterator.hasNext(InterruptibleIterator.scala:37)
    at scala.collection.Iterator$$anon$12.hasNext(Iterator.scala:439)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)
    at org.Apache.spark.sql.execution.arrow.ArrowConverters$$anon$2.hasNext(ArrowConverters.scala:96)
    at scala.collection.Iterator$class.foreach(Iterator.scala:893)
    at org.Apache.spark.sql.execution.arrow.ArrowConverters$$anon$2.foreach(ArrowConverters.scala:94)
    at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:59)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:104)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:48)
    at scala.collection.TraversableOnce$class.to(TraversableOnce.scala:310)
    at org.Apache.spark.sql.execution.arrow.ArrowConverters$$anon$2.to(ArrowConverters.scala:94)
    at scala.collection.TraversableOnce$class.toBuffer(TraversableOnce.scala:302)
    at org.Apache.spark.sql.execution.arrow.ArrowConverters$$anon$2.toBuffer(ArrowConverters.scala:94)
    at scala.collection.TraversableOnce$class.toArray(TraversableOnce.scala:289)
    at org.Apache.spark.sql.execution.arrow.ArrowConverters$$anon$2.toArray(ArrowConverters.scala:94)
    at org.Apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$12.apply(RDD.scala:945)
    at org.Apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$12.apply(RDD.scala:945)
    at org.Apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:2074)
    at org.Apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:2074)
    at org.Apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:87)
    at org.Apache.spark.scheduler.Task.run(Task.scala:109)
    at org.Apache.spark.executor.Executor$TaskRunner.run(Executor.scala:345)
    at Java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at Java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at Java.lang.Thread.run(Unknown Source)
3
Matt

Java.lang.IllegalArgumentExceptionpandas_udfは、OS環境ではなくpyarrowバージョンに関係しています。詳細はこちら issue をご覧ください。

2つのアクションルーティングがあります。

  1. pyarrowをv.0.14にダウングレードする、または
  2. 環境変数ARROW_PRE_0_15_IPC_FORMAT=1SPARK_HOME/conf/spark-env.sh に追加します
2

Sergeyの回答の補足:pythonで独自のsparkSessionを作成し、設定ファイルを変更しない場合は、spark.yarn.appMasterEnv.ARROW_PRE_0_15_IPC_FORMATと環境変数の両方を設定する必要があります。ローカルエグゼキュータspark.executorEnv.ARROW_PRE_0_15_IPC_FORMAT

spark_session = SparkSession.builder \
            .master("yarn") \
            .config('spark.yarn.appMasterEnv.ARROW_PRE_0_15_IPC_FORMAT',1)\
            .config('spark.executorEnv.ARROW_PRE_0_15_IPC_FORMAT',1)

spark = spark_session.getOrCreate()

お役に立てれば!

0
Andy VY