Python 或 SQL 将列的数据类型 - 映射转换为字符串

Python or SQL to convert the Data Type - Map to String for a column

我在名为 'test' 的 table 中有以下列。

如何获取 'id' 和 'value'(eg.for 'id' = 2,我应该获取值 '24' 和其他两个 ID 的 null)来自给定的 table.

'age' 列的 'data type' 是 'Map',我不确定如何处理它。

Python 或 SQL 中的简单查询或任何线索非常感谢。另外,请告知要导入的包。

您可以在 sql 或 python 中获取。 在Python你可以试试

agecolumn=age.replace("{","").replace("}","").split("=")

if agecolumn[1].strip():
   do domething

explode 函数会将您的映射“分解”为键值对,然后您可以随意使用它们。

from pyspark.sql import functions as F

(df
    .select('id', F.explode('age').alias('k', 'v'))
    .show()
)

+---+---+----+
| id|  k|   v|
+---+---+----+
|  2|age|  24|
|  3|age|null|
+---+---+----+