<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 單元測試 HBase 應用程序 本章討論使用 JUnit,Mockito,MRUnit 和 HBaseTestingUtility 對 HBase 應用程序進行單元測試。大部分信息來自[關于測試 HBase 應用程序的社區博客文章](http://blog.cloudera.com/blog/2013/09/how-to-test-hbase-applications-using-popular-tools/)。有關 HBase 本身的單元測試的信息,請參閱 [hbase.tests](#hbase.tests) 。 ## 175\. JUnit HBase 使用 [JUnit](http://junit.org) 進行單元測試 此示例將單元測試添加到以下示例類: ``` public class MyHBaseDAO { public static void insertRecord(Table.getTable(table), HBaseTestObj obj) throws Exception { Put put = createPut(obj); table.put(put); } private static Put createPut(HBaseTestObj obj) { Put put = new Put(Bytes.toBytes(obj.getRowKey())); put.add(Bytes.toBytes("CF"), Bytes.toBytes("CQ-1"), Bytes.toBytes(obj.getData1())); put.add(Bytes.toBytes("CF"), Bytes.toBytes("CQ-2"), Bytes.toBytes(obj.getData2())); return put; } } ``` 第一步是將 JUnit 依賴項添加到 Maven POM 文件中: ``` <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> ``` 接下來,在代碼中添加一些單元測試。測試用`@Test`注釋。這里,單元測試以粗體顯示。 ``` public class TestMyHbaseDAOData { @Test public void testCreatePut() throws Exception { HBaseTestObj obj = new HBaseTestObj(); obj.setRowKey("ROWKEY-1"); obj.setData1("DATA-1"); obj.setData2("DATA-2"); Put put = MyHBaseDAO.createPut(obj); assertEquals(obj.getRowKey(), Bytes.toString(put.getRow())); assertEquals(obj.getData1(), Bytes.toString(put.get(Bytes.toBytes("CF"), Bytes.toBytes("CQ-1")).get(0).getValue())); assertEquals(obj.getData2(), Bytes.toString(put.get(Bytes.toBytes("CF"), Bytes.toBytes("CQ-2")).get(0).getValue())); } } ``` 這些測試確保您的`createPut`方法創建,填充和返回具有預期值的`Put`對象。當然,JUnit 可以做的遠不止這些。有關 JUnit 的介紹,請參閱 [https://github.com/junit-team/junit/wiki/Getting-started](https://github.com/junit-team/junit/wiki/Getting-started) 。 ## 176\. Mockito Mockito 是一個嘲弄的框架。它比 JUnit 更進一步,允許您測試對象之間的交互,而不必復制整個環境。您可以在其項目網站上閱讀有關 Mockito 的更多信息, [https://code.google.com/p/mockito/](https://code.google.com/p/mockito/) 。 您可以使用 Mockito 在較小的單元上進行單元測試。例如,您可以模擬`org.apache.hadoop.hbase.Server`實例或`org.apache.hadoop.hbase.master.MasterServices`接口參考而不是完整的`org.apache.hadoop.hbase.master.HMaster`。 此示例基于 [unit.tests](#unit.tests) 中的示例代碼,以測試`insertRecord`方法。 首先,將 Mockito 的依賴項添加到 Maven POM 文件中。 ``` <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>2.1.0</version> <scope>test</scope> </dependency> ``` 接下來,將`@RunWith`注釋添加到測試類,以指示它使用 Mockito。 ``` @RunWith(MockitoJUnitRunner.class) public class TestMyHBaseDAO{ @Mock Configuration config = HBaseConfiguration.create(); @Mock Connection connection = ConnectionFactory.createConnection(config); @Mock private Table table; @Captor private ArgumentCaptor putCaptor; @Test public void testInsertRecord() throws Exception { //return mock table when getTable is called when(connection.getTable(TableName.valueOf("tablename")).thenReturn(table); //create test object and make a call to the DAO that needs testing HBaseTestObj obj = new HBaseTestObj(); obj.setRowKey("ROWKEY-1"); obj.setData1("DATA-1"); obj.setData2("DATA-2"); MyHBaseDAO.insertRecord(table, obj); verify(table).put(putCaptor.capture()); Put put = putCaptor.getValue(); assertEquals(Bytes.toString(put.getRow()), obj.getRowKey()); assert(put.has(Bytes.toBytes("CF"), Bytes.toBytes("CQ-1"))); assert(put.has(Bytes.toBytes("CF"), Bytes.toBytes("CQ-2"))); assertEquals(Bytes.toString(put.get(Bytes.toBytes("CF"),Bytes.toBytes("CQ-1")).get(0).getValue()), "DATA-1"); assertEquals(Bytes.toString(put.get(Bytes.toBytes("CF"),Bytes.toBytes("CQ-2")).get(0).getValue()), "DATA-2"); } } ``` 該代碼用`ROWKEY-1'',` DATA-1'',``DATA-2''填充`HBaseTestObj`作為值。然后它將記錄插入到模擬表中。捕獲 DAO 將插入的 Put,并測試值以驗證它們是否符合您的預期。 這里的關鍵是在 DAO 之外管理 Connection 和 Table 實例創建。這允許您干凈地模擬它們并測試 Puts,如上所示。同樣,您現在可以擴展到其他操作,例如“獲取”,“掃描”或“刪除”。 ## 177\. MRUnit [Apache MRUnit](https://mrunit.apache.org/) 是一個允許您對 MapReduce 作業進行單元測試的庫。您可以使用它以與其他 MapReduce 作業相同的方式測試 HBase 作業。 給定一個寫入名為`MyTest`的 HBase 表的 MapReduce 作業,該表有一個名為`CF`的列族,這樣的作業的縮減器可能如下所示: ``` public class MyReducer extends TableReducer<Text, Text, ImmutableBytesWritable> { public static final byte[] CF = "CF".getBytes(); public static final byte[] QUALIFIER = "CQ-1".getBytes(); public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException { //bunch of processing to extract data to be inserted, in our case, let's say we are simply //appending all the records we receive from the mapper for this particular //key and insert one record into HBase StringBuffer data = new StringBuffer(); Put put = new Put(Bytes.toBytes(key.toString())); for (Text val : values) { data = data.append(val); } put.add(CF, QUALIFIER, Bytes.toBytes(data.toString())); //write to HBase context.write(new ImmutableBytesWritable(Bytes.toBytes(key.toString())), put); } } ``` 要測試此代碼,第一步是將 MRUnit 的依賴項添加到 Maven POM 文件中。 ``` <dependency> <groupId>org.apache.mrunit</groupId> <artifactId>mrunit</artifactId> <version>1.0.0 </version> <scope>test</scope> </dependency> ``` 接下來,在 Reducer 作業中使用 MRUnit 提供的 ReducerDriver。 ``` public class MyReducerTest { ReduceDriver<Text, Text, ImmutableBytesWritable, Writable> reduceDriver; byte[] CF = "CF".getBytes(); byte[] QUALIFIER = "CQ-1".getBytes(); @Before public void setUp() { MyReducer reducer = new MyReducer(); reduceDriver = ReduceDriver.newReduceDriver(reducer); } @Test public void testHBaseInsert() throws IOException { String strKey = "RowKey-1", strValue = "DATA", strValue1 = "DATA1", strValue2 = "DATA2"; List<Text> list = new ArrayList<Text>(); list.add(new Text(strValue)); list.add(new Text(strValue1)); list.add(new Text(strValue2)); //since in our case all that the reducer is doing is appending the records that the mapper //sends it, we should get the following back String expectedOutput = strValue + strValue1 + strValue2; //Setup Input, mimic what mapper would have passed //to the reducer and run test reduceDriver.withInput(new Text(strKey), list); //run the reducer and get its output List<Pair<ImmutableBytesWritable, Writable>> result = reduceDriver.run(); //extract key from result and verify assertEquals(Bytes.toString(result.get(0).getFirst().get()), strKey); //extract value for CF/QUALIFIER and verify Put a = (Put)result.get(0).getSecond(); String c = Bytes.toString(a.get(CF, QUALIFIER).get(0).getValue()); assertEquals(expectedOutput,c ); } } ``` 您的 MRUnit 測試驗證輸出是否符合預期,插入 HBase 的 Put 具有正確的值,ColumnFamily 和 ColumnQualifier 具有正確的值。 MRUnit 包含一個 MapperDriver 來測試映射作業,您可以使用 MRUnit 測試其他操作,包括從 HBase 讀取,處理數據或寫入 HDFS, ## 178.使用 HBase Mini-Cluster 進行集成測試 HBase 附帶 HBaseTestingUtility,這使得使用 _ 迷你集群 _ 編寫集成測試變得容易。第一步是向 Maven POM 文件添加一些依賴項。檢查版本以確保它們合適。 ``` <properties> <hbase.version>2.0.0-SNAPSHOT</hbase.version> </properties> <dependencies> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-testing-util</artifactId> <version>${hbase.version}</version> <scope>test</scope> </dependency> </dependencies> ``` 此代碼表示 [unit.tests](#unit.tests) 中顯示的 MyDAO 插入的集成測試。 ``` public class MyHBaseIntegrationTest { private static HBaseTestingUtility utility; byte[] CF = "CF".getBytes(); byte[] CQ1 = "CQ-1".getBytes(); byte[] CQ2 = "CQ-2".getBytes(); @Before public void setup() throws Exception { utility = new HBaseTestingUtility(); utility.startMiniCluster(); } @Test public void testInsert() throws Exception { Table table = utility.createTable(Bytes.toBytes("MyTest"), CF); HBaseTestObj obj = new HBaseTestObj(); obj.setRowKey("ROWKEY-1"); obj.setData1("DATA-1"); obj.setData2("DATA-2"); MyHBaseDAO.insertRecord(table, obj); Get get1 = new Get(Bytes.toBytes(obj.getRowKey())); get1.addColumn(CF, CQ1); Result result1 = table.get(get1); assertEquals(Bytes.toString(result1.getRow()), obj.getRowKey()); assertEquals(Bytes.toString(result1.value()), obj.getData1()); Get get2 = new Get(Bytes.toBytes(obj.getRowKey())); get2.addColumn(CF, CQ2); Result result2 = table.get(get2); assertEquals(Bytes.toString(result2.getRow()), obj.getRowKey()); assertEquals(Bytes.toString(result2.value()), obj.getData2()); } } ``` 此代碼創建一個 HBase 迷你集群并啟動它。接下來,它創建一個名為`MyTest`的表,其中包含一個列族`CF`。插入記錄,從同一個表執行 Get,并驗證插入。 > 啟動迷你集群大約需要 20-30 秒,但這應該適合集成測試。 有關 HBaseTestingUtility 的更多信息,請參閱 [HBase 案例研究中的論文:使用 HBaseTestingUtility 進行本地測試和開發](http://blog.sematext.com/2010/08/30/hbase-case-study-using-hbasetestingutility-for-local-testing-development/)(2010)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看