How to Fix ‘DataFrame’ object has no attribute ‘writeTo’ When Working with Apache Iceberg in PySpark
If you’re working with Apache Iceberg in PySpark and encounter this error:
Failed to write to Iceberg table: 'DataFrame' object has no attribute 'writeTo'
You’re not alone. This is a common mistake when transitioning from the traditional DataFrame.write syntax to Iceberg’s DataFrameWriterV2 API.
Let’s walk through why this happens, how to fix it quickly, and when to use each writing method.
Why This Error Happens
The method .writeTo() does not exist on a regular PySpark DataFrame. It’s part of the DataFrameWriterV2 API, introduced in Spark 3.0+ and specifically designed for advanced table management when working with Apache Iceberg through a properly configured catalog.
You’ll see this error if:
- You don’t have a compatible catalog configured (like GlueCatalog, HadoopCatalog, or HiveCatalog).
- You’re not using the right Iceberg packages.
- You’re using an older Spark version.
Solution 1 — Use .write.format("iceberg") (Safe for Most Setups)
If you don’t have a catalog configured, the easiest and most compatible way to write to Iceberg is using the traditional Spark syntax:
df.write \
.format("iceberg") \
.mode("overwrite") \
.save("s3://path-to-your-table")
Or, if you’ve already created the table using Spark SQL:
df.write \
.format("iceberg") \
.mode("append") \
.saveAsTable("catalog_name.db.table_name")
Solution 2 — Correctly Use .writeTo() (Requires Catalog)
If you want to use the modern .writeTo() syntax:
df.writeTo("glue_catalog.service.table").overwritePartitions()
Solution 3 — Create the Table First Using Spark SQL
If the table doesn’t exist yet, you can create it using SQL like this:
CREATE TABLE glue_catalog.db.my_table (
id BIGINT,
name STRING,
created_at TIMESTAMP
)
USING ICEBERG
PARTITIONED BY (days(created_at))
LOCATION 's3://your-bucket/path/'
Key Takeaways
The 'DataFrame' object has no attribute 'writeTo' error doesn’t mean your code is broken — it just means you’re using an API that depends on a catalog-enabled Spark environment.
If you don’t have a catalog configured, simply use the classic df.write.format(“iceberg”) method.
If you want to use the modern Iceberg APIs like .writeTo() and .overwritePartitions(), make sure:
- You have Spark 3.1+
- You’ve installed the correct Iceberg packages
- Your catalog (Glue, Hive, or Hadoop) is configured properly