1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.coprocessor.example;
20
21 import org.apache.hadoop.conf.Configuration;
22 import org.apache.hadoop.hbase.HBaseTestingUtility;
23 import org.apache.hadoop.hbase.testclassification.MediumTests;
24 import org.apache.hadoop.hbase.TableName;
25 import org.apache.hadoop.hbase.client.HTable;
26 import org.apache.hadoop.hbase.client.Put;
27 import org.apache.hadoop.hbase.client.Table;
28 import org.apache.hadoop.hbase.client.coprocessor.Batch;
29 import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
30 import org.apache.hadoop.hbase.coprocessor.example.generated.ExampleProtos;
31 import org.apache.hadoop.hbase.ipc.BlockingRpcCallback;
32 import org.apache.hadoop.hbase.ipc.ServerRpcController;
33 import org.apache.hadoop.hbase.util.Bytes;
34 import org.junit.experimental.categories.Category;
35
36 import java.io.IOException;
37 import java.util.Iterator;
38 import java.util.Map;
39
40 import static junit.framework.Assert.*;
41
42
43
44
45
46 @Category(MediumTests.class)
47 public class TestRowCountEndpoint {
48 private static final TableName TEST_TABLE = TableName.valueOf("testrowcounter");
49 private static final byte[] TEST_FAMILY = Bytes.toBytes("f");
50 private static final byte[] TEST_COLUMN = Bytes.toBytes("col");
51
52 private static HBaseTestingUtility TEST_UTIL = null;
53 private static Configuration CONF = null;
54
55
56 public static void setupBeforeClass() throws Exception {
57 TEST_UTIL = new HBaseTestingUtility();
58 CONF = TEST_UTIL.getConfiguration();
59 CONF.setStrings(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY,
60 RowCountEndpoint.class.getName());
61
62 TEST_UTIL.startMiniCluster();
63 TEST_UTIL.createTable(TEST_TABLE, new byte[][]{TEST_FAMILY});
64 }
65
66
67 public static void tearDownAfterClass() throws Exception {
68 TEST_UTIL.shutdownMiniCluster();
69 }
70
71
72 public void testEndpoint() throws Throwable {
73 Table table = new HTable(CONF, TEST_TABLE);
74
75
76 for (int i=0; i<5; i++) {
77 byte[] iBytes = Bytes.toBytes(i);
78 Put p = new Put(iBytes);
79 p.add(TEST_FAMILY, TEST_COLUMN, iBytes);
80 table.put(p);
81 }
82
83 final ExampleProtos.CountRequest request = ExampleProtos.CountRequest.getDefaultInstance();
84 Map<byte[],Long> results = table.coprocessorService(ExampleProtos.RowCountService.class,
85 null, null,
86 new Batch.Call<ExampleProtos.RowCountService,Long>() {
87 public Long call(ExampleProtos.RowCountService counter) throws IOException {
88 ServerRpcController controller = new ServerRpcController();
89 BlockingRpcCallback<ExampleProtos.CountResponse> rpcCallback =
90 new BlockingRpcCallback<ExampleProtos.CountResponse>();
91 counter.getRowCount(controller, request, rpcCallback);
92 ExampleProtos.CountResponse response = rpcCallback.get();
93 if (controller.failedOnException()) {
94 throw controller.getFailedOn();
95 }
96 return (response != null && response.hasCount()) ? response.getCount() : 0;
97 }
98 });
99
100 assertEquals(1, results.size());
101 Iterator<Long> iter = results.values().iterator();
102 Long val = iter.next();
103 assertNotNull(val);
104 assertEquals(5l, val.longValue());
105 }
106
107 }