我对pyspark很陌生,这个问题令我感到困惑。基本上,我正在寻找一种通过structType或ArrayType进行类型转换的可缩放方法。
我的数据模式示例:
root
|-- _id: string (nullable = true)
|-- created: timestamp (nullable = true)
|-- card_rates: struct (nullable = true)
| |-- rate_1: integer (nullable = true)
| |-- rate_2: integer (nullable = true)
| |-- rate_3: integer (nullable = true)
| |-- card_fee: integer (nullable = true)
| |-- payment_method: string (nullable = true)
|-- online_rates: array (nullable = true)
| |-- element: struct (containsnull = true)
| | |-- rate_1: integer (nullable = true)
| | |-- rate_2: integer (nullable = true)
| | |-- online_fee: double (nullable = true)
|-- updated: timestamp (nullable = true)
如您在此处看到的,card_rates
是结构体,而online_rates
是结构体数组。我正在寻找遍历以上所有字段并有条件地进行类型转换的方法。理想情况下,如果应该是数字,则应将其转换为双精度型;如果应该是字符串,则应将其转换为字符串型。我需要循环,因为这些rate_*
字段可能会随着时间增长。
但是现在,我对能够循环播放它们并将它们全部转换为字符串表示满意,因为我对pyspark非常陌生,并且仍在尝试中。
我想要的输出模式:
root
|-- _id: string (nullable = true)
|-- created: timestamp (nullable = true)
|-- card_rates: struct (nullable = true)
| |-- rate_1: double (nullable = true)
| |-- rate_2: double (nullable = true)
| |-- rate_3: double (nullable = true)
| |-- card_fee: double (nullable = true)
| |-- payment_method: string (nullable = true)
|-- online_rates: array (nullable = true)
| |-- element: struct (containsnull = true)
| | |-- rate_1: double (nullable = true)
| | |-- rate_2: double (nullable = true)
| | |-- online_fee: double (nullable = true)
|-- updated: timestamp (nullable = true)
我正在提出如何执行此操作的想法。
我从这里获得了参考:PySpark convert struct field inside array to string
但是此解决方案对字段进行硬编码,并且不会真正在字段上循环。
请帮助。