动态类加载器的IllegalAccessError

2019年3月3日 7点热度 0条评论

我创建了一个自定义ParquetOutputFormat(org.apache.parquet.hadoop中的类)来覆盖getRecordWriter方法。在getRecordWriter方法内部,它访问CodecFactory,这导致了IllegalAccessError。为了尝试解决该问题,我尝试创建自己的类加载器,但这没有帮助。我关注了此博客文章http://techblog.applift.com/upgrading-spark#advanced-case-parquet-writer

在创建自定义类加载器之前,我使用了CustomParquetOutputFormat,如下所示:

override def createOutputFormat: OutputFormat[Void, InternalRow] with Ext = new CustomParquetOutputFormat[InternalRow]() with Ext {
 ...
}


CustomParquetOutputFormat被调用时,当
CodecFactory尝试访问第274行上的
getRecordWriter时,就会发生此问题:

  CodecFactory codecFactory = new CodecFactory(conf);

(这是可访问CustomParquetOutputFormat的ParquetOutputFormat的第274行)


CodecFactory是软件包专用的。

自定义类加载器:

class CustomClassLoader(urls: Array[URL], parent: ClassLoader, whiteList: List[String])
  extends ChildFirstURLClassLoader(urls, parent) {
  override def  loadClass(name: String) = {
    if (whiteList.exists(name.startsWith)) {
      super.loadClass(name)
    } else {
      parent.loadClass(name)
    }
  }
}


用法:

val sc: SparkContext = SparkContext.getOrCreate()
val cl: CustomClassLoader = new CustomClassLoader(sc.jars.map(new URL(_)).toArray,
  Thread.currentThread.getContextClassLoader, List(
    "org.apache.parquet.hadoop.CustomParquetOutputFormat",
    "org.apache.parquet.hadoop.CodecFactory",
    "org.apache.parquet.hadoop.ParquetFileWriter",
    "org.apache.parquet.hadoop.ParquetRecordWriter",
    "org.apache.parquet.hadoop.InternalParquetRecordWriter",
    "org.apache.parquet.hadoop.ColumnChunkPageWriteStore",
    "org.apache.parquet.hadoop.MemoryManager"
  ))


cl.loadClass("org.apache.parquet.hadoop.CustomParquetOutputFormat")
  .getConstructor(classOf[String], classOf[TaskAttemptContext])
  .newInstance(fullPathWithoutExt, taskAttemptContext)
  .asInstanceOf[OutputFormat[Void, InternalRow] with ProvidesExtension]


错误:

java.lang.IllegalAccessError: tried to access class org.apache.parquet.hadoop.CodecFactory from class org.apache.parquet.hadoop.customParquetOutputFormat
        at org.apache.parquet.hadoop.CustomParquetOutputFormat.getRecordWriter(CustomParquetOutputFormat.scala:40)
        at org.apache.parquet.hadoop.ParquetOutputFormat.getRecordWriter(ParquetOutputFormat.java:262)
        at org.apache.spark.custom.hadoop.HadoopWriter.<init>(HadoopWriter.scala:35)
        at org.apache.spark.sql.execution.datasources.parquet.ParquetWriter.<init>(ParquetWriter.scala:16)
        at org.apache.spark.sql.execution.datasources.parquet.ParquetWriterFactory.createWriter(ParquetWriterFactory.scala:71)
        at com.abden.custom.index.IndexBuilder$$anonfun$4.apply(IndexBuilder.scala:55)
        at com.abden.custom.index.IndexBuilder$$anonfun$4.apply(IndexBuilder.scala:54)
        at scala.collection.immutable.Stream.map(Stream.scala:418)
        at com.abden.custom.index.IndexBuilder.generateTiles(IndexBuilder.scala:54)
        at com.abden.custom.index.IndexBuilder.generateLayer(IndexBuilder.scala:155)
        at com.abden.custom.index.IndexBuilder.appendLayer(IndexBuilder.scala:184)
        at com.abden.custom.index.IndexBuilder$$anonfun$appendLayers$1$$anonfun$apply$1.apply(IndexBuilder.scala:213)
        at com.abden.custom.index.IndexBuilder$$anonfun$appendLayers$1$$anonfun$apply$1.apply(IndexBuilder.scala:210)
        at scala.collection.Iterator$class.foreach(Iterator.scala:742)
        at com.abden.custom.util.SplittingByKeyIterator.foreach(SplittingByKeyIterator.scala:3)
        at com.abden.custom.index.IndexBuilder$$anonfun$appendLayers$1.apply(IndexBuilder.scala:210)
        at com.abden.custom.index.IndexBuilder$$anonfun$appendLayers$1.apply(IndexBuilder.scala:209)
        at org.apache.spark.rdd.RDD$$anonfun$foreachPartition$1$$anonfun$apply$33.apply(RDD.scala:920)
        at org.apache.spark.rdd.RDD$$anonfun$foreachPartition$1$$anonfun$apply$33.apply(RDD.scala:920)
        at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1858)
        at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1858)
        at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:66)
        at org.apache.spark.scheduler.Task.run(Task.scala:89)
        at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:227)
        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)

错误发生在
getRecordWriter的这一行:

val codecFactory = new CodecFactory(conf)

CodecFactory没有修饰符,因此仅限于其软件包。即使使用动态类加载器从同一个类加载器加载所有类,我仍然会得到
IllegalAccessError

解决方案如下:

因此,您尝试做的是打破Java的工作方式!您想要通过实现自己的允许破坏JVM的保护规则的类加载器来访问其包外部的私有(private)包类(因此,您想破坏Java语言规范!)。

我的答案很简单:不要这样做!

如果它是私有(private)程序包,则无法访问它。期!

我认为最好的办法是考虑所需的功能,并使用当前的API来实现它,而不必试图强行闯入。因此,最好的方法是解释您想要的内容,而不是询问如何进行一些技术攻关做(为什么要实现自己的getRecordWriter方法。

我已经在此SOW问题中给出了有关如何在纯Java中读取/写入 Parquet 文件的答案:Write Parquet format to HDFS using Java API with out using Avro and MR

问候,

洛伊奇