Area: API Issues
Sub-Area: Schema Field URN Construction / OpenAPI Integration
Applies to: OSS + Cloud
Issue
You create a Glue dataset through the API and set each column's fieldPath to the plain column name, for example eff_dt. You then build schema field URNs from those names and attach structured properties to them. Later a Glue ingestion recipe runs for the same dataset. It writes field paths such as [version=2.0].[type=int].eff_dt, and your column-level structured properties no longer show on the columns.
You Might Be Asking
- Why does DataHub key a column by its
fieldPathand not by its name? - Why do Glue columns get a
[version=2.0].[type=...]prefix when Snowflake and Teradata columns do not? - How do I build a Glue
fieldPathin Python or Java that matches what ingestion stores? - What is the schema field URN for a Glue column?
Cause
A schema field URN is urn:li:schemaField:(<dataset urn>,<fieldPath>). The UI builds this URN from the fieldPath stored in the dataset's schemaMetadata aspect. Metadata you attached to a URN with a different fieldPath does not match any column in the stored schema.
DataHub uses the full fieldPath because a column name alone is not unique in nested schemas (structs, arrays, maps). The v2 field path format encodes the type and position of each field. See SchemaFieldPath Specification (Version 2).
Connectors build field paths in different ways:
-
Glue, hive-metastore and presto-on-hive pass every column, partition keys included, through
get_schema_fields_for_hive_column. This converts the Hive type string to Avro and gives a v2 path for every column, primitives included. Anintcolumneff_dtbecomes[version=2.0].[type=int].eff_dt. -
hive (the SQLAlchemy-based source) keeps the plain column name for primitive columns. It uses v2 paths only for
struct,map,arrayanduniontypecolumns. - Teradata uses the plain column name as the SQLAlchemy dialect returns it. No prefix.
-
Snowflake uses the plain column name, lowercased by default. With
preserve_column_case: trueit keeps Snowflake's casing.
The Glue source writes the whole schemaMetadata aspect on each run. Each Glue run replaces the field paths you wrote through the API with its own v2 paths.
Solution
Build your field paths the way the connector does. Then the API and the recipe write the same paths, and your schema field URNs stay valid.
Type mapping for Glue and Hive Metastore columns
A primitive column's path is [version=2.0].[type=<avro type>].<column name>. The Hive type maps to the Avro type like this:
-
int,integer,tinyint,smallint→int -
date,timestamp,datetime→int -
bigint,long→long -
float→float -
double,double precision→double -
string,varchar,char,varchar(n),char(n)→string -
binary→string -
boolean,bool→boolean -
decimal,decimal(p,s),numeric,numeric(p,s)→bytes - any type not in the converter's list →
null
Complex columns start with [version=2.0].[type=struct]. Examples:
-
s struct<a:string,b:int>gives three fields:[version=2.0].[type=struct].[type=struct].s,[version=2.0].[type=struct].[type=struct].s.[type=string].aand[version=2.0].[type=struct].[type=struct].s.[type=int].b -
tags array<string>gives[version=2.0].[type=struct].[type=array].[type=string].tags -
m map<string,int>gives[version=2.0].[type=struct].[type=map].[type=int].m(only the value type appears in the path)
Python: use the connector's own function
get_schema_fields_for_hive_column in the acryl-datahub package is the function the Glue source calls. Its field paths match what Glue ingestion stores.
The example below writes the whole schemaMetadata aspect, the same way the Glue source does. Use it for datasets that you create. It then attaches a structured property to one column with a patch. The patch adds or updates only that property and keeps the column's other structured properties. The structured property must allow the schemaField entity type.
from datahub.emitter.mce_builder import make_dataset_urn, make_schema_field_urn
from datahub.emitter.mcp import MetadataChangeProposalWrapper
from datahub.emitter.mcp_patch_builder import MetadataPatchProposal
from datahub.emitter.rest_emitter import DatahubRestEmitter
from datahub.metadata.schema_classes import OtherSchemaClass, SchemaMetadataClass
from datahub.specific.aspect_helpers.structured_properties import (
HasStructuredPropertiesPatch,
)
from datahub.utilities.hive_schema_to_avro import get_schema_fields_for_hive_column
class FieldStructuredPropertiesPatch(HasStructuredPropertiesPatch, MetadataPatchProposal):
pass
emitter = DatahubRestEmitter("<your-datahub-gms-url>", token="<your-token>")
# Use the same platform, platform_instance and env as your Glue recipe.
dataset_urn = make_dataset_urn("glue", "mydb.mytable", "PROD")
columns = [
("eff_dt", "int"),
("account_id", "string"),
("amount", "decimal(10,2)"),
]
fields = []
for name, hive_type in columns:
fields.extend(
get_schema_fields_for_hive_column(name, hive_type, default_nullable=True)
)
emitter.emit_mcp(
MetadataChangeProposalWrapper(
entityUrn=dataset_urn,
aspect=SchemaMetadataClass(
schemaName="mytable",
platform="urn:li:dataPlatform:glue",
version=0,
hash="",
platformSchema=OtherSchemaClass(rawSchema=""),
fields=fields,
),
)
)
# The first entry is the column itself. Struct, array and map columns add more entries after it.
eff_dt_path = get_schema_fields_for_hive_column("eff_dt", "int")[0].fieldPath
# "[version=2.0].[type=int].eff_dt"
field_urn = make_schema_field_urn(dataset_urn, eff_dt_path)
patch = FieldStructuredPropertiesPatch(field_urn)
patch.set_structured_property("urn:li:structuredProperty:<your-property-id>", 365.0)
for mcp in patch.build():
emitter.emit_mcp(mcp)
Pass a float for a number property and a string for a string property. Pass the Hive type exactly as Glue stores it. The function does not change its case.
For Snowflake and Teradata, use the column name as the field path. For Snowflake, lowercase it unless your recipe sets preserve_column_case: true:
dataset_urn = make_dataset_urn("snowflake", "mydb.myschema.mytable", "PROD")
field_urn = make_schema_field_urn(dataset_urn, "eff_dt")
Java
The Java SDK has no equivalent of get_schema_fields_for_hive_column. Use one of these options.
Option A: read the stored field paths. After the dataset is ingested once, read its schemaMetadata aspect with the OpenAPI v3 entity endpoint and take each value.fields[i].fieldPath. Nothing needs to be built, so nothing can mismatch. This works for every column type.
curl -H 'Authorization: Bearer <your-token>' \
'<your-datahub-gms-url>/openapi/v3/entity/dataset/<url-encoded-dataset-urn>/schemaMetadata'
See the OpenAPI guide for how to reach the endpoints.
Option B: build paths for primitive columns before the first ingestion. This helper applies the same type rules as the Python converter, for primitive types only. For struct, array, map or uniontype columns, use Option A or the Python function.
import java.util.Map;
import java.util.regex.Pattern;
public final class HivePrimitiveFieldPath {
private static final Map<String, String> AVRO_TYPES = Map.ofEntries(
Map.entry("string", "string"),
Map.entry("varchar", "string"),
Map.entry("char", "string"),
Map.entry("binary", "string"),
Map.entry("int", "int"),
Map.entry("integer", "int"),
Map.entry("tinyint", "int"),
Map.entry("smallint", "int"),
Map.entry("byteint", "int"),
Map.entry("date", "int"),
Map.entry("timestamp", "int"),
Map.entry("datetime", "int"),
Map.entry("bigint", "long"),
Map.entry("long", "long"),
Map.entry("int64", "long"),
Map.entry("bignumeric", "long"),
Map.entry("float", "float"),
Map.entry("double", "double"),
Map.entry("double precision", "double"),
Map.entry("float64", "double"),
Map.entry("bigdecimal", "double"),
Map.entry("boolean", "boolean"),
Map.entry("bool", "boolean"),
Map.entry("bytes", "bytes"));
private static final Pattern FIXED_STRING = Pattern.compile("(var)?char\\(\\s*\\d+\\s*\\)");
private static final Pattern DECIMAL = Pattern.compile("decimal|numeric");
public static String fieldPath(String columnName, String hiveType) {
String type = hiveType.trim();
String avroType = AVRO_TYPES.get(type);
if (avroType == null) {
if (FIXED_STRING.matcher(type).lookingAt()) {
avroType = "string";
} else if (DECIMAL.matcher(type).lookingAt()) {
avroType = "bytes";
} else {
avroType = "null";
}
}
return "[version=2.0].[type=" + avroType + "]." + columnName;
}
}
For example, fieldPath("eff_dt", "int") returns [version=2.0].[type=int].eff_dt, and fieldPath("amount", "decimal(10,2)") returns [version=2.0].[type=bytes].amount. Build the schema field URN as urn:li:schemaField:(<dataset urn>,<fieldPath>).