如何阻止PySpark写入Oracle时给表名与列名添加双引号
I totally get the frustration—having to wrap every table or column name in quotes just to query your data is such a hassle. Let's get this sorted out quickly!
The root cause here is that PySpark's JDBC writer automatically quotes identifiers (table and column names) by default, which forces Oracle to treat them as case-sensitive. To fix this, we just need to tweak one key option and adjust our approach slightly:
1. Add the quoteIdentifier Option
The magic fix is adding .option('quoteIdentifier', 'false') to your write configuration. This tells PySpark to stop wrapping table and column names in double quotes entirely.
Here's your updated code:
oversampled_df.write \ .format('jdbc') \ .option('truncate', 'true') \ .option('quoteIdentifier', 'false') # This disables the automatic double quotes .options( url=EXT_DB_URL, driver='oracle.jdbc.driver.OracleDriver', dbtable=DEST_DB_TBL_NAME ) \ .mode('overwrite') \ .save()
2. Understand Oracle's Identifier Behavior
Oracle treats unquoted identifiers as case-insensitive and automatically converts them to uppercase. So:
- If your DataFrame has column names like
description, PySpark will send it to Oracle without quotes, and Oracle will store it asDESCRIPTION. - You can then query it with
SELECT description FROM schema.table;orSELECT DESCRIPTION FROM schema.table;—both will work, since Oracle ignores case for unquoted identifiers.
Bonus: Ensure Consistency (Optional)
If you want to make sure your column names match exactly what you expect in Oracle, you can convert your DataFrame's column names to uppercase before writing:
oversampled_df = oversampled_df.toDF(*[col.upper() for col in oversampled_df.columns])
This way, you'll know exactly what the column names are in Oracle, and you won't have to rely on Oracle's automatic conversion.
After making these changes, your tables and columns will be created without double quotes, and you can query them normally without wrapping everything in quotes.
内容的提问来源于stack exchange,提问作者Exorcismus

