【问题标题】:How to convert this nested json in pyspark? [duplicate]如何在pyspark中转换这个嵌套的json? [复制]
【发布时间】:2019-03-15 05:27:43
【问题描述】:

df.printSchema()

root
|-- country: struct (nullable = true)
|    |-- a: long (nullable = true)
|    |-- b: string (nullable = true)
|    |-- c: string (nullable = true)
|    |-- d: string (nullable = true)

Row(trustset=Row(a=1, b='Melbourne is in Aus', c=None, d='Sydney'))

df.show()

+--------------------+
|            trustset|
+--------------------+
|[1, Melbourne is ...|
+--------------------+

我想要的输出必须是

+------+------------------------+-----+--------+
|   a  |    b                   |  c  |   d    |
+------+------------------------+-----+--------+
|   1  |  Melbourne is in Aus   | None| Sydney |
+------+------------------------+-----+--------+

已经获得信任,因为该列需要以 subs 作为主要列

【问题讨论】:

  • df.select(df.col("trustset.*")) 见stackoverflow.com/questions/38753898/…
  • 用过这个df.select(df.columns("trustset.*"))得到TypeError: 'list' object is not callable用这个df.select(df.col("trustset.*"))得到AttributeError: 'DataFrame' object has no attribute 'col'
  • 没有嵌套 JSON 这样的东西。 JSON 是一些数据结构的文本表示。而且问题中没有 JSON。

标签: apache-spark pyspark apache-spark-sql


【解决方案1】:

DataFrame的select可以帮助你选择struct中的嵌套字段

from pyspark.sql.functions import col
df.select(col("trustset.*")).show()

有一个类似的问题:How to flatten a struct in a Spark dataframe?

【讨论】:

    猜你喜欢
    • 2019-08-26
    • 2020-04-02
    • 2021-03-12
    • 1970-01-01
    • 1970-01-01
    • 2020-09-04
    • 2014-10-23
    • 2021-03-17
    • 2023-01-21
    相关资源
    最近更新 更多