Failed with exception java.io.IOException:org.apache.parquet.io.ParquetDecodingException: Can not read value at 0 in block -1 in file hdfs://hadoop102:8020/warehouse/gmall/ads_user_action4/dt=2020-06-14/part-00000-52558b00-e39f-453c-8d9a-a924776a0aa7.c000.snappy.parquet
记录一次spark on hive,写数据到hive表后,使用hive查询报错的问题。
网上查了好多都没有查到具体原因,最后在spark官网找到了问题所在,有一个参数:spark.sql.parquet.writeLegacyFormat,默认为false。
先看官网解释:
If true, data will be written in a way of Spark 1.4 and earlier. For example, decimal values will be written in Apache Parquet’s fixed-length byte array format, which other systems such as Apache Hive and Apache Impala use. If false, the newer format in Parquet will be used. For example, decimals will be written in int-based format. If Parquet output is intended for use with systems that do not support this newer format, set to true.
大概意思就是:
参数如果为true,数据将以Spark 1.4及更早版本的方式写入。例如,十进制值将以Apache Parquet的固定长度字节数组格式写入,Apache Hive和Apache Impala等其他系统也使用这种格式。如果为false,将使用Parquet中的较新格式。例如,小数将基于int的格式写入。如果Parquet输出用于不支持这种新格式的系统,请设置为true。
设置参数如下:
val spark: SparkSession = SparkSession
.builder()
.appName("SparkSqlFromHdfs")
.master("local[*]")
.config("spark.sql.parquet.writeLegacyFormat", value = true)
.enableHiveSupport()
.getOrCreate()
设置参数后,问题得到解决。
注:如果写入的数据没有小数,不添加这项配置也不会有问题。