【问题标题】:using spark to read specific columns data from hbase使用 spark 从 hbase 读取特定列数据
【发布时间】:2014-11-25 09:00:54
【问题描述】:

我在 HBase 中有一个名为“orders”的表,它的列族为“o”,列为 {id,fname,lname,email} 将行键作为 id。我正在尝试使用 spark 仅从 hbase 获取 fname 和 email 的值。目前“我正在做的事情”如下所示

   override def put(params: scala.collection.Map[String, Any]): Boolean = {
    var sparkConfig = new SparkConf().setAppName("Connector")
    var sc: SparkContext = new SparkContext(sparkConfig)
    var hbaseConfig = HBaseConfiguration.create()
    hbaseConfig.set("hbase.zookeeper.quorum", ZookeeperQourum)
    hbaseConfig.set("hbase.zookeeper.property.clientPort", zookeeperPort)
    hbaseConfig.set(TableInputFormat.INPUT_TABLE, schemdto.tableName);
    hbaseConfig.set(TableInputFormat.SCAN_COLUMNS, "o:fname,o:email");
    var hBaseRDD = sc.newAPIHadoopRDD(hbaseConfig, classOf[TableInputFormat],
      classOf[org.apache.hadoop.hbase.io.ImmutableBytesWritable],
      classOf[org.apache.hadoop.hbase.client.Result])
    try {
      hBaseRDD.map(tuple => tuple._2).map(result => result.raw())
        .map(f => KeyValueToString(f)).saveAsTextFile(sink)

      true
    } catch {
      case _: Exception => false
    }
}


def KeyValueToString(keyValues: Array[KeyValue]): String = {
    var it = keyValues.iterator
    var res = new StringBuilder
    while (it.hasNext) {
      res.append( Bytes.toString(it.next.getValue()) + ",")
    }
    res.substring(0, res.length-1);
}

但没有返回任何内容,如果我尝试仅获取一列,例如

hbaseConfig.set(TableInputFormat.SCAN_COLUMNS, "o:fname");

然后它返回列 fname 的所有值

所以我的问题是如何使用 spark 从 hbase 获取多个列

任何帮助将不胜感激。

【问题讨论】:

    标签: scala hbase apache-spark


    【解决方案1】:

    根据documentation,要扫描的列列表需要用空格分隔。

    hbaseConfig.set(TableInputFormat.SCAN_COLUMNS, "o:fname o:email");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-23
      • 1970-01-01
      • 2013-05-13
      • 1970-01-01
      相关资源
      最近更新 更多