HBase表检索数据

时间:2016-07-06 07:21:27

标签: scala hbase

如果我尝试使用以下代码从HBase表中检索数据:

val get = new Get(Bytes.toBytes(extracted.user.rowKey.toString))
val res = table.get(get)

我不确定val res = table.get(get)行是否会返回结果,因为带有此行键的行:传递给Get构造函数的extracted.socialUser.socialUserConnectionId.toString可能不存在于HBase表中。

我正在尝试这样的事情:

val get = new Get(Bytes.toBytes(extracted.socialUser.socialUserConnectionId.toString))
val res = table.get(get)
if (!res) {
    /* Create the row in the HBase table */
}

但它在if语句中说Expression of this type result doesn't convert to type Boolean给出了错误。有没有办法可以解决这个问题?

1 个答案:

答案 0 :(得分:1)

乍一看,似乎val res = table.get(get)会返回Optional类型。

鉴于此,您应该拨打res.isEmpty而不是!res

编辑:

更好的是,您可以使用getOrElse代替get

val res = table.getOrElse{
  // Create the row in the HBase table
}
相关问题