扩展defaultcodec以支持hadoop文件的zip压缩

cgfeq70w  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(564)

我有一些spark代码,它从hdfs读取两个文件(头文件和正文文件),将rdd[string]减少到单个分区,然后使用gzip编解码器将结果写入压缩文件:

spark.sparkContext.textFile("path_to_header.txt,path_to_body.txt")
.coalesce(1)
.saveAsTextFile("output_path", classOf[GzipCodec])

这是100%的预期效果。我们现在被要求为无法解压*.gzip文件的windows用户支持zip压缩。显然,zip格式本机不受支持,所以我尝试推出自己的压缩编解码器。
我在运行代码时遇到了“zipexception:no current zip entry”异常,但是:

Exception occured while exporting org.apache.spark.SparkException: Job aborted due to stage failure: Task 0 in stage 16.0 failed 2 times, most recent failure: Lost task 0.1 in stage 16.0 (TID 675, xxxxxxx.xxxxx.xxx, executor 16): java.util.zip.ZipException: no current ZIP entry
    at java.util.zip.ZipOutputStream.write(Unknown Source)
    at io.ZipCompressorStream.write(ZipCompressorStream.java:23)
    at java.io.DataOutputStream.write(Unknown Source)
    at org.apache.hadoop.mapred.TextOutputFormat$LineRecordWriter.writeObject(TextOutputFormat.java:81)
    at org.apache.hadoop.mapred.TextOutputFormat$LineRecordWriter.write(TextOutputFormat.java:102)
    at org.apache.spark.SparkHadoopWriter.write(SparkHadoopWriter.scala:95)
    at org.apache.spark.rdd.PairRDDFunctions$$anonfun$saveAsHadoopDataset$1$$anonfun$13$$anonfun$apply$7.apply$mcV$sp(PairRDDFunctions.scala:1205)
    at org.apache.spark.rdd.PairRDDFunctions$$anonfun$saveAsHadoopDataset$1$$anonfun$13$$anonfun$apply$7.apply(PairRDDFunctions.scala:1203)
    at org.apache.spark.rdd.PairRDDFunctions$$anonfun$saveAsHadoopDataset$1$$anonfun$13$$anonfun$apply$7.apply(PairRDDFunctions.scala:1203)
    at org.apache.spark.util.Utils$.tryWithSafeFinallyAndFailureCallbacks(Utils.scala:1348)
    at org.apache.spark.rdd.PairRDDFunctions$$anonfun$saveAsHadoopDataset$1$$anonfun$13.apply(PairRDDFunctions.scala:1211)
    at org.apache.spark.rdd.PairRDDFunctions$$anonfun$saveAsHadoopDataset$1$$anonfun$13.apply(PairRDDFunctions.scala:1190)
    at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:70)
    at org.apache.spark.scheduler.Task.run(Task.scala:86)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:274)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

我创建了一个扩展defaultcodec的zipcodec类:

public class ZipCodec extends DefaultCodec {

   @Override
   public CompressionOutputStream createOutputStream(final OutputStream out, final Compressor compressor) throws IOException {
      return new ZipCompressorStream(new ZipOutputStream(out));
   }

以及zipcompressorstream,它扩展了compressorstream:

public class ZipCompressorStream extends CompressorStream {

   public ZipCompressorStream(final ZipOutputStream out) {
      super(out);
   }

   @Override
   public void write(final int b) throws IOException {
      out.write(b);
   }

   @Override
   public void write(final byte[] data, final int offset, final int length) throws IOException {
      out.write(data, offset, length);
   }

我们目前正在使用spark 1.6.0和hadoop 2.6.0-cdh5.8.2
有什么想法吗?
提前谢谢!

xtfmy6hx

xtfmy6hx1#

zip是一种容器格式,而gzip只是一种类似流的格式(用于存储一个文件)。这就是为什么在装箱一个新的zip文件时,您需要首先启动一个条目(给出一个名称),然后在写入之后关闭该条目,然后再关闭一个容器。参见此处示例:https://www.programcreek.com/java-api-examples/?class=java.util.zip.zipoutputstream&method=putnextentry

相关问题