我有一个Spark作业,不断将Parquet文件上传到S3(带有分区)。
这些文件均具有相同的拼花模式。
最近更改了一种字段类型(从String更改为long),因此某些分区的镶木地板架构混杂在一起。
具有两种类型的混合数据的地方现在无法读取某些内容。
看来我可以执行:sqlContext.read.load(path)
尝试在DataFrame上应用任何提取操作(例如collect
)时,该操作失败,并显示ParquetDecodingException
我打算迁移数据并重新格式化,但是无法将混合的内容读取到DataFrame中。
如何使用Apache Spark将混合分区加载到DataFrames或任何其他Spark结构中?
以下是ParquetDecodingException跟踪:
scala> df.collect
[Stage 1:==============> (1 + 3) / 4]
WARN TaskSetManager: Lost task 1.0 in stage 1.0 (TID 2, 172.1.1.1, executor 0): org.apache.parquet.io.ParquetDecodingException:
Can not read value at 1 in block 0 in file
s3a://data/parquet/partition_by_day=20180620/partition_by_hour=10/part-00000-6e4f07e4-3d89-4fad-acdf-37054107dc39.snappy.parquet
at org.apache.parquet.hadoop.InternalParquetRecordReader.nextKeyValue(InternalParquetRecordReader.java:243)
at org.apache.parquet.hadoop.ParquetRecordReader.nextKeyValue(ParquetRecordReader.java:227)
at org.apache.spark.sql.execution.datasources.RecordReaderIterator.hasNext(RecordReaderIterator.scala:39)
at org.apache.spark.sql.execution.datasources.FileScanRDD$$anon$1.hasNext(FileScanRDD.scala:102)
at org.apache.spark.sql.execution.datasources.FileScanRDD$$anon$1.nextIterator(FileScanRDD.scala:166)
at org.apache.spark.sql.execution.datasources.FileScanRDD$$anon$1.hasNext(FileScanRDD.scala:102)
at org.apache.spark.sql.catalyst.expressions.GeneratedClass$GeneratedIterator.processNext(Unknown Source)
at org.apache.spark.sql.execution.BufferedRowIterator.hasNext(BufferedRowIterator.java:43)
at org.apache.spark.sql.execution.WholeStageCodegenExec$$anonfun$8$$anon$1.hasNext(WholeStageCodegenExec.scala:377)
at org.apache.spark.sql.execution.SparkPlan$$anonfun$2.apply(SparkPlan.scala:231)
at org.apache.spark.sql.execution.SparkPlan$$anonfun$2.apply(SparkPlan.scala:225)
at org.apache.spark.rdd.RDD$$anonfun$mapPartitionsInternal$1$$anonfun$apply$25.apply(RDD.scala:826)
at org.apache.spark.rdd.RDD$$anonfun$mapPartitionsInternal$1$$anonfun$apply$25.apply(RDD.scala:826)
at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:38)
at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:323)
at org.apache.spark.rdd.RDD.iterator(RDD.scala:287)
at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:87)
at org.apache.spark.scheduler.Task.run(Task.scala:99)
at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:282)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassCastException: [B cannot be cast to java.lang.Long
at scala.runtime.BoxesRunTime.unboxToLong(BoxesRunTime.java:105)
答案 0 :(得分:1)
据我所知,您不能将具有相同字段的2个架构与不同类型混合使用。因此,我唯一想到的解决方案是:
将每个文件重新写入新位置,然后transform the data to the right schame
答案 1 :(得分:0)
还有另一个想法:与其更改现有字段(field_string)的类型,不如添加一个长类型(field_long)的新字段,并将读取数据的代码更新为类似的形式(以伪代码)并启用模式合并。我相信默认情况下会启用它,但这是一个明确的好例子:
sqlContext.read.option("mergeSchema", "true").parquet(<parquet_file>)
...
if isNull(field_long)
field_value_long = field_string.value.to_long
else
field_value_long = field_long.value