如何将printSchema的结果保存到PySpark中的文件

ito人

df.printSchema()在pyspark中使用过,它为我提供了具有树状结构的架构。现在,我需要将其保存在变量或文本文件中。

我尝试了以下保存方法,但是它们没有用。

v = str(df.printSchema())  
print(v) 
#and
df.printSchema().saveAsTextFile(<path>)

我需要以下格式的保存模式

|-- COVERSHEET: struct (nullable = true)                              
 |    |-- ADDRESSES: struct (nullable = true)
 |    |    |-- ADDRESS: struct (nullable = true)
 |    |    |    |-- _VALUE: string (nullable = true)
 |    |    |    |-- _city: string (nullable = true)
 |    |    |    |-- _primary: long (nullable = true)
 |    |    |    |-- _state: string (nullable = true)
 |    |    |    |-- _street: string (nullable = true)
 |    |    |    |-- _type: string (nullable = true)
 |    |    |    |-- _zip: long (nullable = true)
 |    |-- CONTACTS: struct (nullable = true)
 |    |    |-- CONTACT: array (nullable = true)
 |    |    |    |-- element: struct (containsNull = true)
 |    |    |    |    |-- _VALUE: string (nullable = true)
 |    |    |    |    |-- _name: string (nullable = true)
 |    |    |    |    |-- _type: string (nullable = true)
vert

您需要treeString(由于某种原因,我在python API中找不到)

#v will be a string
v = df._jdf.schema().treeString()

您可以将其转换为RDD并使用 saveAsTextFile

sc.parallelize([v]).saveAsTextFile(...)

或使用特定于Python的API将字符串写入文件。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章