【发布时间】:2022-01-13 01:32:35
【问题描述】:
我有深度级别为 6 的嵌套架构。我在遍历架构中的每个元素以修改列时遇到问题。我有一个列表,其中包含需要修改(散列/匿名)的列名。
我最初的想法是遍历架构中的每个元素并将列与列表项进行比较,并在匹配后进行修改。但我不知道该怎么做。
列表值:['type','name','work','email']
示例架构:
-- abc: struct (nullable = true)
| |-- xyz: struct (nullable = true)
| | |-- abc123: string (nullable = true)
| | |-- services: struct (nullable = true)
| | | |-- service: array (nullable = true)
| | | | |-- element: struct (contains Null = true)
| | | | | |-- type: string (nullable = true)
| | | | | |-- subtype: string (nullable = true)
| | | |-- name : string(nullable = true)
| | |-- details: struct (nullable =true)
| | | | -- work: (nullable = true)
注意:如果我将架构展平,它会创建 600 多列。因此,我正在寻找一种动态修改列的解决方案(无硬编码)
编辑: 如果它有帮助,我将在我尝试修改值的地方共享我的代码,但它已损坏
def change_nested_field_value(schema, new_df,fields_to_change, parent=""):
new_schema = []
if isinstance(schema, StringType):
return schema
for field in schema:
full_field_name = field.name
short_name = full_field_name.split('.')
if parent:
full_field_name = parent + "." + full_field_name
#print(full_field_name)
if short_name[-1] not in fields_to_change:
if isinstance(field.dataType, StructType):
inner_schema = change_nested_field_value(field.dataType,new_df, fields_to_change, full_field_name)
new_schema.append(StructField(field.name, inner_schema))
elif isinstance(field.dataType, ArrayType):
inner_schema = change_nested_field_value(field.dataType.elementType, new_df,fields_to_change, full_field_name)
new_schema.append(StructField(field.name, ArrayType(inner_schema)))
else:
new_schema.append(StructField(field.name, field.dataType))
# else:
############ this is where I have access to the nested element. I need to modify the value here
# print(StructField(field.name, field.dataType))
return StructType(new_schema)
【问题讨论】:
-
这能回答你的问题吗? stackoverflow.com/a/70286904/7989581
-
是的,我试过了。我面临 TypeError:JSON 对象必须是 str、bytes 或 bytearray,而不是 Row
-
需要修改schema还是数组的内容?
-
@ARCrow 只是内容。我需要散列名称、类型和工作列
-
但是在您的 change_nested_field_value 函数中,您正在更改架构