加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 在Spark工作中写入HBase:存在类型的难题

发布时间:2020-12-16 18:20:37 所属栏目:安全 来源:网络整理
导读:我正在尝试编写一个应该将其输出放入HBase的Spark作业.据我所知,正确的方法是在org.apache.spark.rdd.PairRDDFunctions上使用saveAsHadoopDataset方法 – 这要求我的RDD由对组成. saveAsHadoopDataset方法需要一个JobConf,这就是我想要构建的.根据this link,
我正在尝试编写一个应该将其输出放入HBase的Spark作业.据我所知,正确的方法是在org.apache.spark.rdd.PairRDDFunctions上使用saveAsHadoopDataset方法 – 这要求我的RDD由对组成.

saveAsHadoopDataset方法需要一个JobConf,这就是我想要构建的.根据this link,我必须在我的JobConf上设置的一件事是输出格式(实际上它没有工作),就像

jobConfig.setOutputFormat(classOf[TableOutputFormat])

问题是显然这不会编译,因为TableOutputFormat是通用的,即使它忽略了它的类型参数.所以我尝试了各种组合,比如

jobConfig.setOutputFormat(classOf[TableOutputFormat[Unit]])
jobConfig.setOutputFormat(classOf[TableOutputFormat[_]])

但无论如何我都会收到错误

required: Class[_ <: org.apache.hadoop.mapred.OutputFormat[_,_]]

现在,据我所知,Class [_&lt ;: org.apache.hadoop.mapred.OutputFormat [_,_]]转换为Class [T] forSome {type T&lt ;: org.apache.hadoop.mapred.输出格式[_,_] }.这是我认为我有问题的地方,因为:

>类是不变的
> TableOutputFormat [T]<:OutputFormat [T,Mutation],但是
>我不确定存在类型如何与需求T<:OutputFormat [_,_]中的子类型进行交互
有没有办法从TableOutputFormat获取OutputFormat [_,_]的子类型?似乎问题源于Java和Scala中的泛型之间的差异 – 我能为此做些什么?

编辑:

事实证明这甚至更微妙.我试图在REPL中定义自己的方法

def foo(x: Class[_ <: OutputFormat[_,_]]) = x

我实际上可以调用它

foo(classOf[TableOutputFormat[Unit]])

甚至

foo(classOf[TableOutputFormat[_]])

对那个问题.但我不能打电话

jobConf.setOutputFormat(classOf[TableOutputFormat[_]])

Java中setOutputFormat的原始签名是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)

和我的一些代表:

"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",

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读