【发布时间】:2021-08-31 04:41:59
【问题描述】:
我正在尝试降低 PySpark Dataframe 架构的所有列名称的大小写,包括复杂类型列的元素名称。
例子:
original_df
|-- USER_ID: long (nullable = true)
|-- COMPLEX_COL_ARRAY: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- KEY: timestamp (nullable = true)
| | |-- VALUE: integer (nullable = true)
target_df
|-- user_id: long (nullable = true)
|-- complex_col_array: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- key: timestamp (nullable = true)
| | |-- value: integer (nullable = true)
但是,我只能使用以下脚本降低列名的大小写:
from pyspark.sql.types import StructField
schema = df.schema
schema.fields = list(map(lambda field: StructField(field.name.lower(), field.dataType), schema.fields))
我知道我可以使用以下语法访问嵌套元素的字段名称:
for f in schema.fields:
if hasattr(f.dataType, 'elementType') and hasattr(f.dataType.elementType, 'fieldNames'):
print(schema.f.dataType.elementType.fieldNames())
但是如何修改这些字段名的大小写呢?
感谢您的帮助!
【问题讨论】:
-
我想降低所有 Parquet 模式的大小写,因为我在 Hive、Parquet、JSON 和 Spark 之间遇到了区分大小写的问题。