我在 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/