【问题标题】:HBase Table Retrieval DataHBase 表检索数据
【发布时间】:2016-07-06 07:21:27
【问题描述】:

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

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

我不确定val res = table.get(get) 行是否会返回结果,因为带有此行键的行:extracted.socialUser.socialUserConnectionId.toString 传递给 Get 构造函数可能不存在于 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。有什么办法可以解决这个问题吗?

【问题讨论】:

    标签: scala hbase


    【解决方案1】:

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

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

    编辑:

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

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

    【讨论】:

    • 哦,好的。十分感谢你的帮助!这正是我一直在寻找的。在表上使用 Scan 而不是 Get 来搜索行键会更好吗?因为我听说 Scan 效率更高。
    • 我不知道你的其余代码是什么样的,但如果你有所需的rowKey,那么最好用Get 引用它。当然,除非您需要遍历整个表格。请参阅此处接受的答案以获取更多信息:stackoverflow.com/questions/18737212/…
    • 没有。 Get 将转到特定行,如果您没有开始和结束 rowKey 值,则扫描将遍历整个表。在您的情况下,事先知道rowKey 并在Get 中使用它比扫描整个表更有效。
    • 我不确定。不过我之前接受过,不知道为什么它没有出现。刚刚接受了,对此感到抱歉。
    • 没问题。谢谢! :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-10
    • 1970-01-01
    • 2014-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多