【问题标题】:Scala - How to Return this Kind of RDD typeScala - 如何返回这种 RDD 类型
【发布时间】:2016-08-11 08:19:24
【问题描述】:

我尝试使返回RDD的方法引用this,但由于返回需要参数而失败。根据 API (Java),这是我的代码:

def HBaseToRDD(_HBaseConfiguration:HBaseConfiguration, _sc:SparkContext) : RDD[(K, V)] = 
{
val HBaseRDD = _sc.newAPIHadoopRDD(_HBaseConfiguration, classOf[TableInputFormat],
            classOf[org.apache.hadoop.hbase.io.ImmutableBytesWritable],
            classOf[org.apache.hadoop.hbase.client.Result])
}

有什么办法解决这个问题吗? 提前谢谢...

【问题讨论】:

  • 什么是KV?我在您的方法声明中没有看到它们。

标签: java scala hadoop apache-spark bigdata


【解决方案1】:

由于 Yuval Itzchakov 已经提到有关 KV 的信息丢失,此外我观察到以下内容:

  1. 您的方法中缺少返回值
  2. 即使您返回 val HBaseRDD,代码仍然无法编译,因为预期的返回值是 RDD[(K, V)] 类型,但 val HBaseRDDRDD[(org.apache.hadoop.hbase.io.ImmutableBytesWritable, org.apache.hadoop.hbase.client.Result)] 类型

考虑到这一点和几个假设,工作代码示例可能如下所示:

def HBaseToRDD[K, V](_HBaseConfiguration:HBaseConfiguration, _sc:SparkContext) : RDD[(K, V)] =
{
  def toK(key: org.apache.hadoop.hbase.io.ImmutableBytesWritable): K = {
    // here you convert key to K
  }

  def toV(row: org.apache.hadoop.hbase.client.Result): V = {
    // here you convert row to V
  }

  // no need to assign to variable, the result of map will be returned by scala
  _sc.newAPIHadoopRDD(_HBaseConfiguration, classOf[TableInputFormat],
    classOf[org.apache.hadoop.hbase.io.ImmutableBytesWritable],
    classOf[org.apache.hadoop.hbase.client.Result]).map { case (key, row) =>
    toK(key) -> toV(row) // return tuple of type (K, V)
  }
}

【讨论】:

    猜你喜欢
    • 2019-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 2021-02-11
    相关资源
    最近更新 更多