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