Код: Выделить всё
@udf(allPageContentSchemaOutput)
def modifyContent(input_data: List[Row]) -> List[Row]:
modified_data = []
for row in input_data:
item_dict = row.asDict() # Convert Row to dictionary
components_list = item_dict.get('components', [])
for component in components_list:
component = component.asDict()
if isinstance(component, dict) and 'locale' in component:
component['locale'] = {
'es-us': 'Test', # Replace with your logic for es-us
'en-us': 'Test2' # Replace with your logic for en-us
}
modified_data.append(Row(**item_dict))
return modified_data
Код: Выделить всё
[{"uid":"blt0bbd250f8c97ce90","components":[{"locale":"es-us"}]},{"uid":"blt0bbd250f8c97ce91","components":[{"locale":"es-us"}]}]
Код: Выделить всё
output_schema = StructType([
StructField("uid", StringType(), True),
StructField("components", ArrayType(StructType([
StructField("locale", MapType(StringType(), StringType()), True)
])), True)
])
# Define the schema for the output data
allPageContentSchemaOutput = ArrayType(output_schema)
Подробнее здесь: https://stackoverflow.com/questions/786 ... et-updated