2016-07-06 70 views
1

如果我想使用此代碼從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 */ 
} 

但它給我的錯誤,如果聲明說:Expression of this type result doesn't convert to type Boolean。有什麼辦法可以解決這個問題嗎?

回答

1

乍一看,它似乎val res = table.get(get)將返回類型Optional

鑑於這種情況,你應該叫res.isEmpty代替!res

編輯:

更妙的是,你可以使用getOrElse代替get

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

那好吧。十分感謝你的幫助!這正是我所期待的。在表格上使用Scan而不是Get來搜索行鍵會更好嗎?因爲我聽說掃描更有效率。 – CapturedTree

+0

我不知道你的代碼的其餘部分是什麼樣的,但如果你有所需的'rowKey',那麼最好用'Get'來引用它。除非你需要瀏覽整個表格。看到接受的答案在這裏更多的信息:http://stackoverflow.com/questions/18737212/hbase-get-vs-scan-and-in-memory-table –

+0

噢好吧。但即使我將rowkey傳遞給'Get',它仍然需要遍歷所有行才能找到它?但我認爲,因爲我知道rowkey'Get'可能更有效。 – CapturedTree