返回任何 JSON 的 pyspark UDF 类型

时间:2021-03-30 12:38:31

标签: python json scala pyspark user-defined-functions

我有一个 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 架构的不同文件上重用)

0 个答案:

没有答案