1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.coprocessor;
19
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.hbase.Cell;
27 import org.apache.hadoop.hbase.CellUtil;
28 import org.apache.hadoop.hbase.Coprocessor;
29 import org.apache.hadoop.hbase.CoprocessorEnvironment;
30 import org.apache.hadoop.hbase.HConstants;
31 import org.apache.hadoop.hbase.client.Scan;
32 import org.apache.hadoop.hbase.coprocessor.protobuf.generated.ColumnAggregationWithNullResponseProtos.ColumnAggregationServiceNullResponse;
33 import org.apache.hadoop.hbase.coprocessor.protobuf.generated.ColumnAggregationWithNullResponseProtos.ColumnAggregationNullResponseSumRequest;
34 import org.apache.hadoop.hbase.coprocessor.protobuf.generated.ColumnAggregationWithNullResponseProtos.ColumnAggregationNullResponseSumResponse;
35 import org.apache.hadoop.hbase.protobuf.ResponseConverter;
36 import org.apache.hadoop.hbase.regionserver.HRegion;
37 import org.apache.hadoop.hbase.regionserver.InternalScanner;
38 import org.apache.hadoop.hbase.util.Bytes;
39
40 import com.google.protobuf.RpcCallback;
41 import com.google.protobuf.RpcController;
42 import com.google.protobuf.Service;
43
44
45
46
47
48
49 public class ColumnAggregationEndpointNullResponse
50 extends
51 ColumnAggregationServiceNullResponse
52 implements Coprocessor, CoprocessorService {
53 static final Log LOG = LogFactory.getLog(ColumnAggregationEndpointNullResponse.class);
54 private RegionCoprocessorEnvironment env = null;
55 @Override
56 public Service getService() {
57 return this;
58 }
59
60 @Override
61 public void start(CoprocessorEnvironment env) throws IOException {
62 if (env instanceof RegionCoprocessorEnvironment) {
63 this.env = (RegionCoprocessorEnvironment)env;
64 return;
65 }
66 throw new CoprocessorException("Must be loaded on a table region!");
67 }
68
69 @Override
70 public void stop(CoprocessorEnvironment env) throws IOException {
71
72 }
73
74 @Override
75 public void sum(RpcController controller, ColumnAggregationNullResponseSumRequest request,
76 RpcCallback<ColumnAggregationNullResponseSumResponse> done) {
77
78 Scan scan = new Scan();
79
80 byte[] family = request.getFamily().toByteArray();
81 byte[] qualifier = request.hasQualifier() ? request.getQualifier().toByteArray() : null;
82 if (request.hasQualifier()) {
83 scan.addColumn(family, qualifier);
84 } else {
85 scan.addFamily(family);
86 }
87 int sumResult = 0;
88 InternalScanner scanner = null;
89 try {
90 HRegion region = this.env.getRegion();
91
92 if (Bytes.equals(region.getEndKey(), HConstants.EMPTY_END_ROW)) {
93 done.run(null);
94 return;
95 }
96 scanner = region.getScanner(scan);
97 List<Cell> curVals = new ArrayList<Cell>();
98 boolean hasMore = false;
99 do {
100 curVals.clear();
101 hasMore = scanner.next(curVals);
102 for (Cell kv : curVals) {
103 if (CellUtil.matchingQualifier(kv, qualifier)) {
104 sumResult += Bytes.toInt(kv.getValueArray(), kv.getValueOffset());
105 }
106 }
107 } while (hasMore);
108 } catch (IOException e) {
109 ResponseConverter.setControllerException(controller, e);
110
111 sumResult = -1;
112 LOG.info("Setting sum result to -1 to indicate error", e);
113 } finally {
114 if (scanner != null) {
115 try {
116 scanner.close();
117 } catch (IOException e) {
118 ResponseConverter.setControllerException(controller, e);
119 sumResult = -1;
120 LOG.info("Setting sum result to -1 to indicate error", e);
121 }
122 }
123 }
124 done.run(ColumnAggregationNullResponseSumResponse.newBuilder().setSum(sumResult)
125 .build());
126 LOG.info("Returning sum " + sumResult + " for region " +
127 Bytes.toStringBinary(env.getRegion().getRegionName()));
128 }
129 }