I'm new to Scala and rusty with Java and I'm tripping up on some semantics. I'm trying to get the class object of SequenceFileOutputFormat .
Other stack overflow posts say to simple do the following:
classOf[SequenceFileOutputFormat]
Which yields the error:
class SequenceFileOutputFormat takes type parameters
Ok fine, so it requires the parameter types, so I do the following:
classOf[SequenceFileOutputFormat[String, String]]
Which yields the error:
[error] found : Class[org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat[String,String]](classOf[org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat])
[error] required: Class[_ <: org.apache.hadoop.mapred.OutputFormat[_, _]]
What gives? How do I get the class object of a class that requires type parameters?
With hadoop-mapreduce-client-core
"3.0.0", this here:
import org.apache.hadoop.mapreduce.lib.output._
object HadoopQuestion_48818781 {
def main(args: Array[String]): Unit = {
val c = classOf[SequenceFileOutputFormat[_, _]]
println("Class: " + c)
}
}
prints:
Class: class org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat
Explanation:
Even though classOf[T]
is processed at compile time, the Class[T]
thing itself is a runtime construct, and it does not know anything about the generic type parameters of T
. Therefore, you have to put underscores _
for the runtime-erased type parameters. These underscores are really just syntactic sugar for existential types in Scala, but in this particular case they fulfill a role similar to that of the wildcards in Java.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.