簡體   English   中英

如何在 Spark Streaming Scala 中對 HBase 進行單元測試

[英]How to unit test HBase in Spark streaming scala

我試圖對doSomethingRdd進行單元測試,它需要在 rdd 轉換中從 HBase 讀取一些參考數據。

def doSomethingRdd(in: DStream[String]): DStream[String] = {
    in.map(i => {
        val cell = HbaseUtil.getCell("myTable", "myRowKey", "myFamily", "myColumn") 
        i + cell.getOrElse("")
    })
}

Object HBaseUtil {
    def getCell(tableName: String, rowKey: String, columnFamily: String, column: String): Option[String] = {
    val HBaseConn = ConnectionPool.getConnection()
    //the rest of the code will use HBaseConn 
    //to get a HBase cell and convert to a string
    }
}

我閱讀了這篇Cloudera 文章,但我對他們推薦的方法有一些問題。

我嘗試的第一件事是使用 ScalaMock 來模擬HBaseUtil.getUtil方法,這樣我就可以繞過 HBase 連接。 為了模擬本文建議的對象單例,我也做了一些變通方法。 我更新了我的代碼,如下所示。 但是, doSomethingRdd失敗了,因為模擬的 hbaseUtil 不是序列化,Paul Butcher 在他的回復中也解釋了這一點

def doSomethingRdd(in: DStream[String], hbaseUtil: HBaseUtilBody:HBaseUtil): DStream[String] = {
    in.map(i => {
        val cell = HbaseUtil.getCell("myTable", "myRowKey", "myFamily", "myColumn") 
        i + cell.getOrElse("")
    })
}

trait HBaseUtilBody {
    def getCell(tableName: String, rowKey: String, columnFamily: String, column: String): Option[String] = {
    val HBaseConn = ConnectionPool.getConnection()
    //the rest of the code will use HBaseConn 
    //to get a HBase cell and convert to a string
    }
}

object HBaseUtil extends HBaseUtilBody

我認為在 RDD 轉換中從 HBase 獲取數據將是一種非常常見的模式。 但是我不確定如何在不連接到真實 HBase 實例的情況下對其進行單元測試。

在 2020 年的 HBase 2.x 中,我們使用hbase-testing-util 只需將其添加到您的 SBT 構建文件中

// https://mvnrepository.com/artifact/org.apache.hbase/hbase-testing-util

libraryDependencies += "org.apache.hbase" % "hbase-testing-util" % "2.2.3" % Test

然后像這樣建立連接

import org.apache.hadoop.hbase.HBaseTestingUtility

val utility = new HBaseTestingUtiliy
utility.startMiniCluster() // defaults to 1 master, 1 region server and 1 data node
val connection = utility.getConnection()

啟動 MiniCluster 實際啟動

  • 小型DFSC集群
  • MiniZKCluster,和
  • 小型HBase集群

如果您需要添加一些特定的配置(例如安全設置),您可以將 hbase-site.xml 添加到您的資源中。

有關更多信息,請參閱HBase 參考指南中的 HBase Mini-Cluster 集成測試部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM