- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2024 10:36 AM
Hi,
I have an external table which reads data from a S3 bucket. The s3 bucket is expected to get new files frequently. With some changes to the underlying schema. I used Refresh Table table command to load new files from the s3 location and it worked fine. But when there are schema changes, either addition or deletion, the refresh table is not working,
Is it possible to refresh the metadata of the external table when there are schema changes? Should I alter the table each time there are changes to the schema? Could someone please help?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2024 04:36 PM
@Dp15 - Please refer to the below illustration.
- Considering a streaming pipeline from table A to table B
- Without column mapping:
- you can only ADD COLUMN on table A
- If spark.databricks.delta.schema.autoMerge.enabled is set
- Any addition of columns to A (say c )would cause c to be added to table B as well (assuming no transformation or filtering)
- This is considered safe because adding column to a table won't cause data miss or duplication, e.g. a select * from table B now will just show nulls for all historical data in B
- If ^ is not set: the stream would fail anyway
- With column mapping:
- For ADD COLUMN, same story. So when you use schemaTrackingLocation and ADD COLUMN, we won't require you to enter the allowDropOrRenameColumn SQL conf.
Please refer to the below doc for additional details - https://docs.databricks.com/en/delta/delta-column-mapping.html#streaming-with-column-mapping-and-sch...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2024 07:18 AM
HI @shan_chandra how about deletions from the external location? And what if I am not using a streaming table?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2024 08:29 AM
@Dp15 - you can drop column manually using the below
ALTER TABLE table_name DROP COLUMN col_name
1. Please note dropping a column from a metadata does not delete the underlying data for column in files.
2. Purging the data column can be done using REORG TABLE to rewrite files.
3. Use VACUUM to physically delete the files that contain the dropped column data.
Reference:
https://docs.databricks.com/en/delta/delta-column-mapping.html#drop-columns
https://docs.databricks.com/en/delta/update-schema.html#explicitly-update-schema-to-drop-columns
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2024 01:27 PM
Hi @shan_chandra This drop works for a delta table which is managed table, however it does work for an external table, I am looking specifically for schema changes in external table, now a refresh might work to load new metadata in the external table, however when there are schema modifications, only addition of columns are possible dropping a column has not worked for me, Correct me if I am wrong here
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2024 09:10 AM
@Dp15 - yes you are correct. Dropping a column from an managed table in Databricks works different from the external table(as the schema is inferred by the underlying source). Below hack can help. AFAIK. Please let me know if this works for you.
1. create or replace new external table B on the new schema (new set of columns you want to keep) and new data source path
2. insert into new table B as select (required columns) from table A(old table).
3. Drop table A
4. Alter table - Rename table B to table A
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2024 07:38 AM
@shan_chandra This worked thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2024 07:48 AM - edited 01-23-2024 07:48 AM
@Dp15 - I am glad it worked. Happy to help!!!