Scala/Hadoop : Compiler error when using classOf[CustomInputFormat] unless instance of CustomInputFormat exists

标签 scala hadoop

我在 XMLIO.scala 中定义了自定义输入和输出格式:

import scala.xml.Node
import org.apache.hadoop.lib.input.FileInputFormat
import org.apache.hadoop.lib.output.FileOutputFormat
import org.apache.hadoop.mapreduce.{ RecordReader, RecordWriter }
// ...
object XMLIO {
    class XMLInputFormat extends FileInputFormat[LongWritable, Node] { /*...*/ }
    class XMLRecordReader extends RecordReader[LongWritable, Node] { /*...*/ }
    class XMLOutputFormat extends FileOutputFormat[LongWritable, Node] { /*...*/ }
    class XMLRecordWriter extends RecordWriter[LongWritable, Node] { /*...*/ }
}

我正在尝试将其用于我在 Example.scala 中定义的工作:
import XMLIO._
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.mapreduce.Job
object Example {
    @throws(classOf[Exception])
    def main( args : Array[String] ) {
        val job = new Job(new Configuration(), "")
        job setInputFormatClass classOf[XMLInputFormat]
    }
}

但是,这给了我一个编译器错误:
[ERROR] /path/to/Example.scala:8: error: type mismatch;
[INFO]  found   : java.lang.Class[XMLInputFormat](classOf[XMLInputFormat])
[INFO]  required: java.lang.Class[_ <: org.apache.hadoop.mapreduce.InputFormat]
[INFO]     job setInputFormatClass classOf[XMLInputFormat]
[INFO]                                    ^

鉴于 XMLInputFormat,这对我来说似乎很奇怪是 FileInputFormat 的子类,它是 InputFormat 的子类.

在 REPL 中玩了一下,我发现了一个奇怪的解决方法。如果我创建了一个 XMLInputFormat 的实例在我设置输入格式类之前,没有编译器错误。也就是说,以下编译正常:
import XMLIO._
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.mapreduce.Job
object Example {
    @throws(classOf[Exception])
    def main( args : Array[String] ) {
        val x = new XMLInputFormat()
        val job = new Job(new Configuration(), "")
        job setInputFormatClass classOf[XMLInputFormat]
    }
}

这里发生了什么?有没有解决这个问题的方法,看起来不像是一个黑客?

最佳答案

看起来这是 scala 2.9.0(这是我使用的)的错误。当我升级到 Scala 2.9.1 时,问题就消失了。

关于Scala/Hadoop : Compiler error when using classOf[CustomInputFormat] unless instance of CustomInputFormat exists,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10260226/

相关文章:

java - 从WordCount hadoop示例中调用HelloWorld JNI函数,给出错误java.lang.UnsatisfiedLinkError

hadoop - 无法在 hadoop 中格式化 Namenode

apache-spark - 无法针对 hadoop 3.2.0 构建 spark2.4.3

scala - 是否有将 Int 限制为范围的内置函数?

scala - 在调整大小之前,传单 map 不会正确显示

java - Maven:将 Scala 代码和 Java 代码一起构建到一个 Fat JAR 中

hadoop - 寻找关键的最大值(value)

scala - 如何在 Play Framework 关闭期间等待 Actor 停止?

java - 无法下载 scala-library-2.11

java - 如何将 COBOL COMP、COMP-1 和 COMP-2 转换为 Java Long 或 Int