Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ingestion/transformer): create tag if not exist #9076

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
46f40d1
add tag if not exist
siddiquebagwan-gslab Oct 23, 2023
50dbbfd
Merge branch 'master' into master+tag-transformer
siddiquebagwan-gslab Oct 23, 2023
2d27c4e
Merge branch 'master' into master+tag-transformer
siddiquebagwan-gslab Oct 25, 2023
605fb51
Merge branch 'master' into master+tag-transformer
siddiquebagwan-gslab Oct 31, 2023
b00f652
Merge branch 'master+tag-transformer' of github.com:siddiquebagwan-gs…
siddiquebagwan-gslab Oct 31, 2023
dc5a4f9
address review comments
siddiquebagwan-gslab Nov 1, 2023
b242961
Merge branch 'master' into master+tag-transformer
siddiquebagwan-gslab Nov 15, 2023
d128f89
address review comments
siddiquebagwan-gslab Nov 15, 2023
069bfb0
remove unused function
siddiquebagwan-gslab Nov 15, 2023
fa8ab34
return urn
siddiquebagwan-gslab Nov 15, 2023
0015219
Merge branch 'master' into master+tag-transformer
siddiquebagwan-gslab Dec 12, 2023
0aae160
review comments
siddiquebagwan-gslab Dec 12, 2023
94b135f
Merge branch 'master' into master+tag-transformer
siddiquebagwan-gslab Dec 13, 2023
b1c4345
lint fix
siddiquebagwan-gslab Dec 13, 2023
29afacb
Merge branch 'master+tag-transformer' of github.com:siddiquebagwan-gs…
siddiquebagwan-gslab Dec 13, 2023
465da36
Merge branch 'master' into master+tag-transformer
siddiquebagwan-gslab Dec 14, 2023
7f4a70e
review comments
siddiquebagwan-gslab Dec 14, 2023
f09195d
Merge branch 'master' into master+tag-transformer
siddiquebagwan-gslab Dec 14, 2023
31c14f2
python3.7 lint fix
siddiquebagwan-gslab Dec 14, 2023
d672758
Merge branch 'master+tag-transformer' of github.com:siddiquebagwan-gs…
siddiquebagwan-gslab Dec 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions metadata-ingestion/src/datahub/ingestion/graph/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,9 +787,11 @@ def get_aspect_counts(self, aspect: str, urn_like: Optional[str] = None) -> int:

def execute_graphql(self, query: str, variables: Optional[Dict] = None) -> Dict:
url = f"{self.config.server}/api/graphql"

body: Dict = {
"query": query,
}

if variables:
body["variables"] = variables

Expand Down Expand Up @@ -1065,6 +1067,22 @@ def parse_sql_lineage(
default_schema=default_schema,
)

def create_tag(self, tag_name: str) -> Dict[Any, Any]:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should return just the urn, and not a dict

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

graph_query: str = """
mutation($tag_detail: CreateTagInput!) {
createTag(input: $tag_detail)
}
"""

variables = {
"tag_detail": {"name": tag_name},
}

return self.execute_graphql(
hsheth2 marked this conversation as resolved.
Show resolved Hide resolved
query=graph_query,
variables=variables,
)

def close(self) -> None:
self._make_schema_resolver.cache_clear()
super().close()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from typing import Callable, List, Optional, cast

from datahub.configuration.common import (
Expand All @@ -9,6 +10,9 @@
from datahub.ingestion.api.common import PipelineContext
from datahub.ingestion.transformer.dataset_transformer import DatasetTagsTransformer
from datahub.metadata.schema_classes import GlobalTagsClass, TagAssociationClass
from datahub.utilities.urns.tag_urn import TagUrn

logger = logging.getLogger(__name__)


class AddDatasetTagsConfig(TransformerSemanticsConfigModel):
Expand All @@ -33,6 +37,26 @@ def create(cls, config_dict: dict, ctx: PipelineContext) -> "AddDatasetTags":
config = AddDatasetTagsConfig.parse_obj(config_dict)
return cls(config, ctx)

def create_tag_if_not_exist(
siddiquebagwan-gslab marked this conversation as resolved.
Show resolved Hide resolved
self, tag_associations: List[TagAssociationClass]
) -> None:
if self.ctx.graph is None:
logger.debug("graph instance is None. Skip tag creation")
return # graph instance in not available

for tag_association in tag_associations:
if self.ctx.graph.exists(tag_association.tag):
continue

ids: List[str] = TagUrn.create_from_string(
tag_association.tag
).get_entity_id()

assert len(ids) == 1, "Invalid Tag Urn"

response: dict = self.ctx.graph.create_tag(tag_name=ids[0])
logger.debug(f"Tag creation response: {response}")

def transform_aspect(
self, entity_urn: str, aspect_name: str, aspect: Optional[Aspect]
) -> Optional[Aspect]:
Expand All @@ -45,6 +69,9 @@ def transform_aspect(
tags_to_add = self.config.get_tags_to_add(entity_urn)
if tags_to_add is not None:
out_global_tags_aspect.tags.extend(tags_to_add)
self.create_tag_if_not_exist(
tag_associations=out_global_tags_aspect.tags,
)

return self.get_result_semantics(
self.config, self.ctx.graph, entity_urn, out_global_tags_aspect
Expand Down
Loading