Hi , I want to write a custom transformer to get ...
# ingestion
f
Hi , I want to write a custom transformer to get and analyze the latest schema of datasets. I wrote a transformer which will process dataset
entities
and transform the
schemaMetadata
aspect. But when I try to run it in a data ingestion procudure (Mysql datasource), It can recognize all datasets but can't get the
schemaMetadata
aspect correctly. the parameter
aspect
is always a None value. how can I change this transformer to meet my requirement?
plus1 2
Copy code
class CustomGetSchema(BaseTransformer, SingleAspectTransformer):
    """Transformer that adds owners to datasets according to a callback function."""

    # context param to generate run metadata such as a run ID
    ctx: PipelineContext

    def __init__(self, config, ctx: PipelineContext):
        super().__init__()
        print(f" __init__ config_dict:{config}")


    @classmethod
    def create(cls, config_dict: dict, ctx: PipelineContext) -> "CustomGetSchema":
        return cls(config_dict, ctx)

    def entity_types(self) -> List[str]:
        return ["dataset"]

    def aspect_name(self) -> str:
        return "schemaMetadata"

    def transform_one(self, mce: MetadataChangeEventClass) -> MetadataChangeEventClass:
        print("transform_one")
        print(mce)

        return mce

    def transform_aspect(  # type: ignore
            self, entity_urn: str, aspect_name: str, aspect: Optional[SchemaMetadataClass]) -> Optional[SchemaMetadataClass]:
        print("=" * 40)
        # assert aspect is None or isinstance(aspect,
        #                                     SchemaMetadataClass)

        platform_part, dataset_fqdn, env = entity_urn\
            .replace("urn:li:dataset:(", "")\
            .replace(")", "")\
            .split(",")

        platform = platform_part.split(":")[-1]
        database, table = dataset_fqdn.split(".")

        print(f"entity_urn: {entity_urn}")
        print(f"platform: {platform}, db: {database}, table: {table}")
        print(f"aspect_name: {aspect_name}")
        print(f"aspect: {aspect}")
When I run it in data ingestion, I got output as follows
Copy code
========================================
entity_urn: urn:li:dataset:(urn:li:dataPlatform:mysql,demo.test,PROD)
platform: mysql, db: demo, table: test
aspect_name: schemaMetadata
aspect: None
========================================
entity_urn: urn:li:dataset:(urn:li:dataPlatform:mysql,demo.test1,PROD)
platform: mysql, db: demo, table: test1
aspect_name: schemaMetadata
aspect: None