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