Pyspark模式树到模式对象的字符串



我在一个txt文件中有printSchema(((一个treeString(的输出。是否有pyspark API来解析字符串并创建模式对象?

示例输入:

root
|-- num: long (nullable = true)
|-- letter: string (nullable = true)

输出示例:火花模式对象

StructType(List(StructField(num,LongType,true),StructField(letter,StringType,true)))

据我所知,没有这样的东西可以将Big输出打印(来自printSchema() that basically returns None.(转换为实际的pyspark对象。为了做到这一点,您必须自己创建模式。

编写一个解析器来为您做这件事,但它非常无用,只需使用DF.schema即可,返回就是您所需要的。

这并不完全相同,但应该可以工作。不要使用treeString,而是使用json字符串。

import json
from pyspark.sql.types import StructType

serialized = df.schema.json()
deserialized = StructType.fromJson(json.loads(serialized))

最新更新