Scala Spark:如何从Parquet文件中提取嵌套的列名并在其上添加前缀

发布于 2025-02-08 03:54:18 字数 889 浏览 0 评论 0原文

这个想法是在数据框架中读取一个镶木quet文件。然后,从架构中提取所有列名和类型。如果我们有一个嵌套的列,我想在列名之前添加一个“前缀”。

考虑到我们可以具有正确命名的子列的嵌套列,并且我们还可以拥有一个嵌套的列,只有一个数组,没有列名,而是“元素”。

val dfSource: DataFrame = spark.read.parquet("path.parquet")
val dfSourceSchema: StructType = dfSource.schema

dfsourceschema的示例(输入):

 |-- exCar: array (nullable = true)
 |    |-- element: array (containsNull = true)
 |    |    |-- element: binary (nullable = true)
 |-- exProduct: string (nullable = true)
 |-- exName: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- exNameOne: string (nullable = true)
 |    |    |-- exNameTwo: string (nullable = true)

所需的输出:

((exCar.prefix.prefix,binary)),(exProduct, String), (exName.prefix.exNameOne, String), (exName.prefix.exNameTwo, String) )

The idea is to read a parquet file into dataFrame. Then, extract all column name's and type's from it's schema. If we have a nested columns, i would like to add a "prefix" before the column name.

Considering that we can have a nested column with sub column named properly, and we can have also a nested column with just an array of array without column name but "element".

val dfSource: DataFrame = spark.read.parquet("path.parquet")
val dfSourceSchema: StructType = dfSource.schema

Example of dfSourceSchema (Input):

 |-- exCar: array (nullable = true)
 |    |-- element: array (containsNull = true)
 |    |    |-- element: binary (nullable = true)
 |-- exProduct: string (nullable = true)
 |-- exName: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- exNameOne: string (nullable = true)
 |    |    |-- exNameTwo: string (nullable = true)

Desired output :

((exCar.prefix.prefix,binary)),(exProduct, String), (exName.prefix.exNameOne, String), (exName.prefix.exNameTwo, String) )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文