0
我已经定义了一个自定义输入和输出格式通过在XMLIO.scala
:斯卡拉/ Hadoop的:编译器错误时使用classOf [CustomInputFormat除非CustomInputFormat的实例存在
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]
}
}
这是怎么回事?有没有解决这个问题,看起来并不像一个黑客?