Area: API
Sub-Area: Schema Extension
Applies to: OSS + Cloud
Issue
You want to add custom metadata fields, or create new entity types, beyond the standard DataHub schema. You need to know which mechanism to use and how to use it.
You Might Be Asking
- Can I add custom fields to datasets?
- How do I create a new entity type?
- What's the difference between custom properties and structured properties?
Cause
DataHub has three ways to extend metadata:
- Structured properties are typed properties that you define first and then assign to assets. They add validated properties to any entity type without changing the metadata model. When you write a value, the server checks that the definition isn't soft-deleted, the cardinality (single or multiple values), the data type, the allowed values, the string length and the allowed platforms. It also rejects changes to immutable properties.
- Custom properties are a string key-value map on a dataset, in the DatasetProperties aspect. Keys and values are strings.
- Custom entity types change the metadata model itself. They need a fork of DataHub or a custom model repository, so try structured properties first.
Solution
1. Create a structured property
In the UI, go to Govern > Structured Properties. You can also use GraphQL or the CLI. Creating a property needs the privilege to manage structured properties. Without it, the call fails with "Unable to create structured property. Please contact your admin."
GraphQL:
mutation {
createStructuredProperty(
input: {
qualifiedName: "io.company.dataClassification"
displayName: "Data Classification"
description: "Internal data classification level"
valueType: "urn:li:dataType:datahub.string"
cardinality: SINGLE
entityTypes: ["urn:li:entityType:datahub.dataset"]
}
) {
urn
}
}
Rules for this GraphQL input:
-
valueTypeis a data type URN. The built-in types areurn:li:dataType:datahub.string,.number,.urn,.rich_textand.date. There is no boolean type. Dates use the formatYYYY-MM-DD. -
entityTypesis a list of entity type URNs, such asurn:li:entityType:datahub.dataset. - If you set both
idandqualifiedName, they must match. Otherwise the call fails with "Qualified name and the ID of a structured property must match". Put the readable label indisplayName. - The mutation returns an object, so you must select fields, for example
{ urn }.
With the CLI, write a definition YAML file and run datahub properties upsert -f <file>. The YAML uses short names: lowercase type names (string, number, date, urn, rich_text) and entity types such as dataset. The keys are id, qualified_name, display_name, type, entity_types, cardinality and allowed_values. The file needs id (or urn). An entry with only qualified_name fails. If you set both id and qualified_name, they must match. Each allowed_values entry is {value, description}. Use datahub properties list and datahub properties get --urn <urn> to read definitions back.
2. Apply structured property values
Create the definition first. If you assign a value to a property that doesn't exist, the server drops that assignment with a warning by default. The write still succeeds if at least one valid assignment remains. On self-hosted DataHub you can make this strict with STRUCTURED_PROPERTIES_DROP_MISSING_PROPERTY_VALUES_WITH_WARNING. On DataHub Cloud, contact DataHub Support.
Use GraphQL:
mutation {
upsertStructuredProperties(
input: {
assetUrn: "<asset urn>"
structuredPropertyInputParams: [
{
structuredPropertyUrn: "urn:li:structuredProperty:io.company.dataClassification"
values: [{ stringValue: "confidential" }]
}
]
}
) {
properties {
structuredProperty { urn }
}
}
}
A property with SINGLE cardinality rejects more than one value. A property with allowed values rejects any other value. Use removeStructuredProperties(input: {assetUrn, structuredPropertyUrns}) to remove values.
You can also assign values in a dataset YAML file under structured_properties and load it with datahub dataset upsert -f <file>. Do not add a structured_properties key to an ingestion recipe. Recipes don't have that key, and unknown keys fail validation.
3. Add custom properties to a dataset
Use the Python SDK patch helpers in datahub.specific.dataset. DatasetPatchBuilder has add_custom_property(key, value) to add one property and set_custom_properties(dict) to replace the whole property map. The DatasetProperties aspect also holds the dataset name and description, so don't write the whole aspect just to add a key. GraphQL doesn't support custom property operations. See the custom properties tutorial for working examples.
If you emit with DataHubRestEmitter, emit_mcp takes a single MetadataChangeProposalWrapper or MetadataChangeProposal. It has no entity_urn, aspect_name or aspect keywords, and passing them raises a TypeError.
4. Create a custom entity type (self-hosted)
- Declare the new entity in an
entity-registry.ymlfile. Give it a key aspect annotated with@Aspect. - The
@Entityannotation applies only to Snapshot records, the legacy pattern. Don't use it for new entities. -
mlModel(key aspectmlModelKey) is already a built-in entity. Choose a new name for a custom entity. - You can fork DataHub and rebuild it, or ship a custom model repository. A custom model repository only supports aspects defined in PDL, added to existing or new entities.
- A custom model repository is mounted at the path in
ENTITY_REGISTRY_PLUGIN_PATH. The default is/etc/datahub/plugins/models.
On DataHub Cloud, you can't set server configuration yourself. Contact DataHub Support if you need a new entity type.
5. Read the values back
query {
dataset(urn: "<dataset urn>") {
structuredProperties {
properties {
structuredProperty { urn }
values {
... on StringValue { stringValue }
... on NumberValue { numberValue }
}
}
}
}
}
values returns StringValue or NumberValue, so the query selects the field of each type. The entry has no propertyUrn field. Use structuredProperty { urn }. On DataHub Cloud, run GraphQL in GraphiQL at https://your-account.acryl.io/api/graphiql.