【发布时间】:2020-05-08 09:58:45
【问题描述】:
以下内容的正确 PySpark 架构是什么?第一个返回是一个列表,我已经正确编码,但第二个返回是一个元组列表,我不知道如何在一个模式中编写其对应的类型。
df = spark.createDataFrame([("Alive", 4)], ["Name", "Number"])
def example(n):
return Row('Out1', 'Out2')([n + 2, n+4], [(n, n+2), (n, n+4)])
schema = StructType([
StructField("Out1", ArrayType(IntegerType()), False),
StructField("Out2", ArrayType(IntegerType(), False), True)])
example_udf = F.udf(example, schema)
newDF = df.withColumn("Output", F.explode(F.array(example_udf(df["Number"]))))
newDF = newDF.select("Name", "Number", "Output.*")
提前非常感谢!
【问题讨论】: