Python SDK for Tags
In this guide, we will use the Python SDK to create Tags.
A prerequisite for this section is to have previously gone through the following docs.
In the following sections we will:
- Create a Database Service, a Database, a Schema and one Table,
- Create a Classification,
- Create a Tag for the Classification and add it to the Table.
Creating the Entities
To prepare the necessary ingredients, execute the following steps.
1. Preparing the Client
from metadata.sdk import configure
configure(
host="http://localhost:8585/api",
jwt_token="<token>",
)
2. Creating the Database Service
We are mocking a MySQL instance. Note how we need to pass the right configuration class MysqlConnection, as a
parameter for the generic DatabaseConnection type.
from metadata.sdk import DatabaseServices
from metadata.generated.schema.api.services.createDatabaseService import (
CreateDatabaseServiceRequest,
)
from metadata.generated.schema.entity.services.connections.database.common.basicAuth import (
BasicAuth,
)
from metadata.generated.schema.entity.services.connections.database.mysqlConnection import (
MysqlConnection,
)
from metadata.generated.schema.entity.services.databaseService import (
DatabaseConnection,
DatabaseServiceType,
)
db_service = CreateDatabaseServiceRequest(
name="test-service-db-lineage",
serviceType=DatabaseServiceType.Mysql,
connection=DatabaseConnection(
config=MysqlConnection(
username="username",
authType=BasicAuth(password="password"),
hostPort="http://localhost:1234",
)
),
)
db_service_entity = DatabaseServices.create(db_service)
3. Creating the Database
Any Entity that is created and linked to another Entity, has to hold the fullyQualifiedName to the Entity it
relates to. In this case, a Database is bound to a specific service.
from metadata.sdk import Databases
from metadata.generated.schema.api.data.createDatabase import CreateDatabaseRequest
create_db = CreateDatabaseRequest(
name="test-db",
service=db_service_entity.fullyQualifiedName,
)
create_db_entity = Databases.create(create_db)
4. Creating the Schema
The same happens with the Schemas. They are related to a Database.
from metadata.sdk import DatabaseSchemas
from metadata.generated.schema.api.data.createDatabaseSchema import (
CreateDatabaseSchemaRequest,
)
create_schema = CreateDatabaseSchemaRequest(
name="test-schema", database=create_db_entity.fullyQualifiedName
)
create_schema_entity = DatabaseSchemas.create(create_schema)
5. Creating the Table
We are doing a simple example with a single column.
from metadata.sdk import Tables
from metadata.generated.schema.api.data.createTable import CreateTableRequest
from metadata.generated.schema.entity.data.table import Column, DataType
table_request = CreateTableRequest(
name="my_table",
databaseSchema=create_schema_entity.fullyQualifiedName,
columns=[Column(name="id", dataType=DataType.BIGINT)],
)
table = Tables.create(table_request)
6. Creating the Classification
Classifications are the entities we use to group tag definitions. Let’s create a sample one:
from metadata.sdk import Classifications
from metadata.generated.schema.api.classification.createClassification import (
CreateClassificationRequest,
)
from metadata.generated.schema.api.classification.createTag import CreateTagRequest
classification_request = CreateClassificationRequest(
name="TestClassification",
description="Sample classification.",
)
Classifications.create(classification_request)
7. Creating the Tag
Once there is an existing classification, we create tags within that given classification:
from metadata.sdk import Tags
tag_request = CreateTagRequest(
classification=classification_request.name,
name="TestTag",
description="Sample Tag.",
)
Tags.create(tag_request)
8. Tagging a Table
Now that we have the Tag, we can proceed and tag a specific asset. In this example, we’ll
tag the Table we created above. Use Tables.add_tag() from the Fluent API:
Tables.add_tag(
table_id=table.id,
tag_fqn="TestClassification.TestTag",
)
If we now check the tags for the Table, the new tag will be appearing in there.
Note that we used Tables.add_tag() because the Table already existed. If you control
the Tag creation process when the Table metadata is generated, you can assign
the tags in the tags property of CreateTableRequest.
9. Tagging a Column
Column-level tagging is not yet available in the Fluent API. This operation will be added to Tables in a future SDK release.