And*_*rea 4 hadoop hbase scala existential-type apache-spark
我正在尝试编写一个应该将其输出放入HBase的Spark作业.据我所知,这样做的正确方法是使用方法saveAsHadoopDataset上org.apache.spark.rdd.PairRDDFunctions-这需要我的RDD是由对.
该方法saveAsHadoopDataset需要一个JobConf,这就是我想要构建的.根据这个链接,我必须设置的一件事JobConf是输出格式(实际上它没有用),比如
jobConfig.setOutputFormat(classOf[TableOutputFormat])
Run Code Online (Sandbox Code Playgroud)
问题是显然这不会编译,因为它TableOutputFormat是通用的,即使它忽略了它的类型参数.所以我尝试了各种组合,比如
jobConfig.setOutputFormat(classOf[TableOutputFormat[Unit]])
jobConfig.setOutputFormat(classOf[TableOutputFormat[_]])
Run Code Online (Sandbox Code Playgroud)
但无论如何我都会收到错误
required: Class[_ <: org.apache.hadoop.mapred.OutputFormat[_, _]]
Run Code Online (Sandbox Code Playgroud)
现在,据我所知,Class[_ <: org.apache.hadoop.mapred.OutputFormat[_, _]]转换为Class[T] forSome { type T <: org.apache.hadoop.mapred.OutputFormat[_, _] }.这是我认为我有问题的地方,因为:
Class 是不变的TableOutputFormat[T] <: OutputFormat[T, Mutation]但是T <: OutputFormat[_, _]有没有办法获得的一个亚型OutputFormat[_, _]的TableOutputFormat?似乎问题源于Java和Scala中的泛型之间的差异 - 我能为此做些什么?
编辑:
事实证明这甚至更微妙.我试图在REPL中定义自己的方法
def foo(x: Class[_ <: OutputFormat[_, _]]) = x
Run Code Online (Sandbox Code Playgroud)
我实际上可以调用它
foo(classOf[TableOutputFormat[Unit]])
Run Code Online (Sandbox Code Playgroud)
甚至
foo(classOf[TableOutputFormat[_]])
Run Code Online (Sandbox Code Playgroud)
对那个问题.但我不能打电话
jobConf.setOutputFormat(classOf[TableOutputFormat[_]])
Run Code Online (Sandbox Code Playgroud)
setOutputFormatJava中的原始签名是void setOutputFormat(Class<? extends OutputFormat> theClass).我如何从Scala中调用它?
这很奇怪,你是100%确定你的导入是正确的(编辑:是的,这是问题,请参阅注释),你的构建文件中有正确版本的人工制品吗?如果我从我的工作项目中提供代码片段,它可能会对您有所帮助:
import org.apache.hadoop.hbase.HBaseConfiguration
import org.apache.hadoop.mapred.JobConf
import org.apache.hadoop.hbase.mapred.TableOutputFormat
val conf = HBaseConfiguration.create()
val jobConfig: JobConf = new JobConf(conf, this.getClass)
jobConfig.setOutputFormat(classOf[TableOutputFormat])
jobConfig.set(TableOutputFormat.OUTPUT_TABLE, outputTable)
Run Code Online (Sandbox Code Playgroud)
和我的一些代表:
"org.apache.hadoop" % "hadoop-client" % "2.3.0-mr1-cdh5.0.0",
"org.apache.hbase" % "hbase-client" % "0.96.1.1-cdh5.0.0",
"org.apache.hbase" % "hbase-common" % "0.96.1.1-cdh5.0.0",
"org.apache.hbase" % "hbase-hadoop-compat" % "0.96.1.1-cdh5.0.0",
"org.apache.hbase" % "hbase-it" % "0.96.1.1-cdh5.0.0", /
"org.apache.hbase" % "hbase-hadoop2-compat" % "0.96.1.1-cdh5.0.0",
"org.apache.hbase" % "hbase-prefix-tree" % "0.96.1.1-cdh5.0.0",
"org.apache.hbase" % "hbase-protocol" % "0.96.1.1-cdh5.0.0",
"org.apache.hbase" % "hbase-server" % "0.96.1.1-cdh5.0.0",
"org.apache.hbase" % "hbase-shell" % "0.96.1.1-cdh5.0.0",
"org.apache.hbase" % "hbase-testing-util" % "0.96.1.1-cdh5.0.0",
"org.apache.hbase" % "hbase-thrift" % "0.96.1.1-cdh5.0.0",
Run Code Online (Sandbox Code Playgroud)