我有一个 pyspark UDF,它从源读取并将其存储到 spark 数据帧中的一列中。如何从udf返回没有模式的json
import json
from pyspark.sql import functions as sf
from pyspark.sql import types as st
df.printSchema()
root
|
|- filename: string (nullable = false)
def read_data(filename):
# read json file
return json.loads(output)
read_data_schema = st.StringType()
read_data_udf = sf.udf(read_data, read_data_schema)
df = df.withColumn('output', read_data_udf('filename'))
df.printSchema()
root
|
|- filename: string (nullable = false)
|- output: string (nullable = false)
在这里,UDF 返回的是字符串而不是 JSON。如何从 udf 返回 json(可以在具有不同 json 架构的不同文件上重用)