06-20-2024 05:40 AM
06-21-2024 02:52 AM - edited 06-21-2024 02:53 AM
Thank you Kaniz!
A few follow-up questions:
Could you give an example of each?
For 1. you said "define your schema with metadata", how do you this, what is the syntax for defining metadata with tags, like I said before, the following does not work:
10-25-2024 02:52 AM
Did you ever get an answer to this ?
10-25-2024 06:38 AM
@chaosBEE Have you try the below
Use following SQL command to add tags programmatically
spark.sql("""ALTER TABLE your_catalog.your_schema.your_table SET TAGS ('key1' = 'value1');""")
10-25-2024 07:18 AM
Hey @Panda ,
That will work but when you want to do this for each of the columns in your table it becomes very unclean in comparison to using something like the StructField Metadata attribute.
At the moment you would end up doing somethings like:
def _create_tags_script(self, column_name, field_name, value):
return f"ALTER COLUMN {column_name} SET TAGS ('{field_name}' = '{value}')"
def get_sql_script(self, catalog_name, schema_name, table_name):
alter_table_script = f"ALTER TABLE {catalog_name}.{schema_name}.{table_name}"
output_sql_script = []
for schema_field in self.schema_fields:
if schema_field.source_field:
output_sql_script.append(
f"{alter_table_script} {self._create_tags_script(schema_field.column_name, 'source_field', schema_field.source_field)}"
)
if schema_field.legacy_field:
output_sql_script.append(
f"{alter_table_script} {self._create_tags_script(schema_field.column_name, 'legacy_field', schema_field.legacy_field)}"
)
if schema_field.comment:
output_sql_script.append(
f"{alter_table_script} ALTER COLUMN {schema_field.column_name} COMMENT '{schema_field.comment}'"
)
return output_sql_script
which is just a bit more messy than
StructType(
[
StructField(
"columnName1",
StringType(),
True,
metadata = {
'comment': "This is a comment"
}
),
StructField(
"columnName2",
StringType(),
True,
metadata = {
'comment': "This is a comment"
}
),
StructField(
"columnName3",
StringType(),
True,
metadata = {
'comment': "This is a comment"
}
)
]
)
Thursday
Bump,
I've got the same issue. Looks like there was a partial reply from Kaniz but I can't see it in this thread.
Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.
If there isn’t a group near you, start one and help create a community that brings people together.
Request a New Group