- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2022 08:45 AM
I'm reading data into a dataframe with
df = spark.read.json("s3://somepath/")
I've tried first creating a delta table using the DeltaTable API with:
DeltaTable.createIfNotExists(spark)\
.location(target_path)\
.addColumns(df.schema)\
.execute()
This is giving me an AnalysisException and says I have invalid characters. I should set the table property 'delta.columnMapping.mode' to 'name'. I've tried:
spark.conf.set("spark.databricks.delta.defaults.columnMapping.mode", "name")
This hasn't worked. I've tried to use the DataFrame API instead:
df.write.format("delta").option("delta.columnMapping.mode", "name").save("s3://anotherpath")
But the same error message is coming up. The erroneous column names are deeply nested in structs.
Is there some other way for me to accomplish this delta table write (without flattening and fixing col names)?
EDIT: I ended up traversing through the schema and changing the problematic column names.
- Labels:
-
Column names
-
Data
-
Delta table
-
Source Paths
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
@jb1z @maxutil Have you tried it like this?
import dlt
@dlt.table(table_properties={'quality': 'bronze', 'delta.columnMapping.mode': 'name'})
def netsuite_items_inventory_price():
return (
spark.readStream.format('cloudFiles')
.option('cloudFiles.format', 'json')
.load('s3://data-lake-raw-injestion/Netsuite/Item/testLoad')
)
Looking at https://docs.databricks.com/en/delta-live-tables/properties.html#delta-live-tables-table-properties, it says "In addition to the table properties supported by Delta Lake, you can set the following table properties.", so ideally we should be able to provide a key-value pair, columnMapping is listed in https://docs.databricks.com/en/delta/table-properties.html#delta-table-properties:
delta.columnMapping.mode
Whether column mapping is enabled for Delta table columns and the corresponding Parquet columns that use different names.
See Rename and drop columns with Delta Lake column mapping.
Note: Enabling delta.columnMapping.mode automatically enables delta.randomizeFilePrefixes.
Data type: DeltaColumnMappingMode
Default: none
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi Databricks, it would be great to get a better answer to this 2022 issue, rather than changing column names. Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
You can use column mapping to bypass this issue:
When column mapping is enabled for a Delta table, you can include spaces and any of these characters in the table’s column names: ,;{}()\n\t=
.
https://docs.databricks.com/en/delta/column-mapping.html#supported-characters-in-column-names
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
@Walter_C thanks for the fast response. I tried using 'delta.columnMapping.mode', 'name' as suggested by @maxutil but this failed with the same error. The documentation you provided only had SQL examples and the DLT pipeline i am running only allows python, so i am trying to set the option correctly. This is the code I have, and the example schema below that, that i also tried which failed with an error about columns not matching, even though the columns exactly match my json data.
import dlt
@dlt.table(table_properties={'quality': 'bronze'})
def netsuite_items_inventory_price():
return (
spark.readStream.format('cloudFiles')
.option('cloudFiles.format', 'json')
.option('delta.columnMapping.mode', 'name')
.load('s3://data-lake-raw-injestion/Netsuite/Item/testLoad')
)
# param for @dlt.tables(
schema="""
ID STRING,
RecordType STRING,
Name STRING,
DisplayName STRING,
BasePrice STRING,
IsItemUpdated STRING,
OnHand STRING,
TotalQuantityOnHand STRING,
TotalValue STRING,
AverageCost STRING,
LastPurchasePrice STRING,
Location STRING,
Available STRING,
UPCCode STRING,
VendorName STRING,
VendorCode STRING,
EachHeight STRING,
EachLength STRING,
EachWidth STRING,
Weight STRING
"""
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
@jb1z @maxutil Have you tried it like this?
import dlt
@dlt.table(table_properties={'quality': 'bronze', 'delta.columnMapping.mode': 'name'})
def netsuite_items_inventory_price():
return (
spark.readStream.format('cloudFiles')
.option('cloudFiles.format', 'json')
.load('s3://data-lake-raw-injestion/Netsuite/Item/testLoad')
)
Looking at https://docs.databricks.com/en/delta-live-tables/properties.html#delta-live-tables-table-properties, it says "In addition to the table properties supported by Delta Lake, you can set the following table properties.", so ideally we should be able to provide a key-value pair, columnMapping is listed in https://docs.databricks.com/en/delta/table-properties.html#delta-table-properties:
delta.columnMapping.mode
Whether column mapping is enabled for Delta table columns and the corresponding Parquet columns that use different names.
See Rename and drop columns with Delta Lake column mapping.
Note: Enabling delta.columnMapping.mode automatically enables delta.randomizeFilePrefixes.
Data type: DeltaColumnMappingMode
Default: none
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a week ago
This code worked for me with all the additional table properties for versions. Thanks for your help @VZLA
import dlt
@dlt.table(table_properties={'quality': 'bronze', 'delta.columnMapping.mode': 'name', 'delta.minReaderVersion': '2', 'delta.minWriterVersion': '5'})
def netsuite_items_inventory_price():
return (
spark.readStream.format('cloudFiles')
.option('cloudFiles.format', 'json')
.option('delta.columnMapping.mode', 'name')
.load('s3://data-lake-raw-injestion/Netsuite/Item/testLoad')
)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a week ago
Glad it helped @jb1z , happy to help.