Apache spark Pyspark UDF-复杂返回类型的性能命中

Apache spark Pyspark UDF-复杂返回类型的性能命中,apache-spark,pyspark,Apache Spark,Pyspark,我有一个PySpark UDF,它返回字符串的元组,我将其编码为结构。这里有一个玩具的例子 def my_func(x): return "1", x, "3" spark.udf.register("my_func", lambda x: my_func(x), StructType([StructField("one", StringType(), Struc

我有一个PySpark UDF,它返回字符串的元组,我将其编码为结构。这里有一个玩具的例子

def my_func(x):
  return "1", x, "3"

spark.udf.register("my_func", lambda x: my_func(x), StructType([StructField("one", StringType(),
                                                                StructField("two", StringType(), 
                                                                StructField("three", StringType()])
我称之为

spark.sql("select col1, my_func(col1) from sdf").show()
与返回元组中的一个元素相比,我看到返回整个元组的性能提高了10到20倍,例如

spark.udf.register("my_func", lambda x: my_func(x)[1], StringType())

这是一个已知的问题,有没有避免转换速度减慢的方法?

这就是我如何让它工作的方法-如果有更有效的方法,请使用lmk。为了解决性能问题

1) Transform the DataFrame to an RDD[Row]

2) Apply the function to transform into a Row of the final output

3) Convert back to a DataFrame
代码:

def map_to_new_row(row):
  NewRow = Row("one", "two", "three")
  return NewRow("1", row.col1, "3")

rdd1 = df1.rdd.map(map_to_new_row)
df2 = spark.createDataFrame(rdd1, StructType([StructField("one", StringType(), 
                                              StructField("two", StringType(), 
                                              StructField("three", StringType()]))
这是给我更好的表现