HBase Table 检索数据

HBase Table Retrieval Data

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

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

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

我正在尝试这样的事情:

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。有什么办法可以解决这个问题吗?

乍一看,val res = table.get(get) 会 return 类型 Optional

鉴于此,您应该调用 res.isEmpty 而不是 !res

编辑:

更好的是,您可以使用 getOrElse 而不是 get :

val res = table.getOrElse{
  // Create the row in the HBase table
}