我有一个由ChoiceType
组成的动态数据框架file = glueContext.create_dynamic_frame.from_options(...)
由于结构是相当动态的,我想找到发生ChoiceType的键我该怎么做呢
你可以这样做:
from typing import List
from awsglue.gluetypes import ChoiceType, Field, StructType, ArrayType, NullType
def check_for_unresolved_fields(schema: List[Field]):
def _recursion(field_map: List[Field], unresolved_column_names: List[Field]):
for field in field_map.values():
if isinstance(field.dataType, StructType):
_recursion(field.dataType.field_map, unresolved_column_names)
if isinstance(field.dataType, ArrayType):
if not isinstance(field.dataType.elementType, NullType):
if isinstance(field.dataType.elementType, StructType):
_recursion(field.dataType.elementType.field_map, unresolved_column_names)
if isinstance(field.dataType.elementType, ChoiceType):
unresolved_column_names.append((field.name, field.dataType.elementType.choices))
if isinstance(field.dataType, ChoiceType):
unresolved_column_names.append((field.name, field.dataType.choices))
unresolved_columns = []
_recursion(schema.field_map, unresolved_columns)
if unresolved_columns:
raise ValueError(f'The following columns {unresolved_columns} are still not resolved')
check_for_unresolved_fields(dynamic_frame.schema())