UDF函数返回dictionary,但返回schema为structyp

2024-04-25 17:21:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个下面的测试片段。在

import pyspark
from pyspark.conf import SparkConf
from pyspark.sql import SQLContext
from pyspark.sql.functions import udf
from pyspark.sql.types import *


conf = SparkConf()
sc = pyspark.SparkContext(conf=conf)
spark = SQLContext(sc)

schema = StructType([
    StructField("foo", FloatType(), False),
    StructField("bar", FloatType(), False)
])

def udf_test(n):
    return {
        'foo': n / 2, 
        'bar': n * 2,
    }

test_udf = udf(udf_test, schema)
df = spark.createDataFrame([(1, 2.0), (2, 3.0)], ["x", "y"])

base_columns = df.columns

df.withColumn('foobar', test_udf("y")).select(*base_columns, 'foobar.*').show()

据我所知,我的自定义项应该只返回值列表。 但我试着把字典还给我,结果奏效了。它工作正常吗?在

我怀疑它能得到字典,把它的值转换成列表,然后再把它们解包。但是当您得到dict的值时,它可以以不同的顺序返回,这样这个脚本就可以不正确地工作了。 我说得对吗?在


Tags: columnsfromtestimportdfsqlschemaconf