Unable to write data in table by Apache Iceberg using Spark

Viewed 1083

I am new to Apache Iceberg. I want to perform read and write operation using Apache Iceberg. I am using Spark 3.0.0.

code:

    System.setProperty("hadoop.home.dir","C:\\hadoop" )
    val conf = new SparkConf()
    conf.set("spark.sql.extensions","org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions")
    conf.set("spark.sql.catalog.spark_catalog","org.apache.iceberg.spark.SparkSessionCatalog")
    conf.set("spark.sql.catalog.spark_catalog.type","hive")
    conf.set("spark.sql.catalog.local","org.apache.iceberg.spark.SparkCatalog")
    conf.set("spark.sql.catalog.local.type","hadoop")
    conf.set("spark.sql.catalog.local.warehouse","warehouse")

    val spark = SparkSession.builder().master("local").config(conf).getOrCreate()
    
    spark.sql("CREATE TABLE local.db.table (id bigint, data string) USING iceberg")
    spark.sql("INSERT INTO local.db.table VALUES (1, 'a'), (2, 'b'), (3, 'c')")

Dependency List:

    compile group: 'org.scala-lang', name: 'scala-library', version: '2.12.0'

    compile group: 'org.apache.spark', name: 'spark-core_2.12', version: '3.0.0'
    compile group: 'org.apache.spark', name: 'spark-sql_2.12', version: '3.0.0'

    compile group: 'org.apache.iceberg', name: 'iceberg-spark', version: '0.11.1'
    compile group: 'org.apache.iceberg', name: 'iceberg-spark3-runtime', version: '0.11.1'

    compile group:  'org.apache.hive', name: 'hive-metastore', version: '2.0.1'

Exception Message :

java.lang.ClassCastException: org.apache.iceberg.shaded.org.apache.parquet.schema.MessageType cannot be cast to org.apache.parquet.schema.MessageType
    at org.apache.iceberg.parquet.ParquetWriter.<init>(ParquetWriter.java:96)
    at org.apache.iceberg.parquet.Parquet$WriteBuilder.build(Parquet.java:250)
    at org.apache.iceberg.spark.source.SparkAppenderFactory.newAppender(SparkAppenderFactory.java:110)

Should i need any other dependency to enable MessageType cast.

1 Answers

When using Spark and Iceberg you only want to use the iceberg-spark3-runtime jar. This is a shaded jar which protects against dependency conflicts with Spark dependencies. If you also include iceberg-spark you will end up with conflicts since this is the unshaded jar for Spark 2 and will included unshaded dependencies which will conflict with both Spark3 and Iceberg-Spark3-Runtime.

So in summation, you should remove

    compile group: 'org.apache.iceberg', name: 'iceberg-spark', version: '0.11.1'

And this

    compile group: 'org.apache.iceberg', name: 'iceberg-spark3-runtime', version: '0.11.1'

Should be marked as a provided (or runtime) dependency

Related