Cell value data type
- * @param Promoted data type
- * @param PB message that is used to transport initializer specific bytes
- * @param PB message that is used to transport Cell () instance
- * @param PB message that is used to transport Promoted () instance
- */
-@InterfaceAudience.Public
-@InterfaceStability.Evolving
-public class AggregateImplementation
-extends AggregateService implements CoprocessorService, Coprocessor {
- protected static final Log log = LogFactory.getLog(AggregateImplementation.class);
- private RegionCoprocessorEnvironment env;
-
- /**
- * Gives the maximum for a given combination of column qualifier and column
- * family, in the given row range as defined in the Scan object. In its
- * current implementation, it takes one column family and one column qualifier
- * (if provided). In case of null column qualifier, maximum value for the
- * entire column family will be returned.
- */
- @Override
- public void getMax(RpcController controller, AggregateArgument request,
- RpcCallback done) {
- InternalScanner scanner = null;
- AggregateResponse response = null;
- T max = null;
- try {
- ColumnInterpreter ci = constructColumnInterpreterFromRequest(request);
- T temp;
- Scan scan = ProtobufUtil.toScan(request.getScan());
- scanner = env.getRegion().getScanner(scan);
- List results = new ArrayList();
- byte[] colFamily = scan.getFamilies()[0];
- NavigableSet qualifiers = scan.getFamilyMap().get(colFamily);
- byte[] qualifier = null;
- if (qualifiers != null && !qualifiers.isEmpty()) {
- qualifier = qualifiers.pollFirst();
- }
- // qualifier can be null.
- boolean hasMoreRows = false;
- do {
- hasMoreRows = scanner.next(results);
- for (KeyValue kv : results) {
- temp = ci.getValue(colFamily, qualifier, kv);
- max = (max == null || (temp != null && ci.compare(temp, max) > 0)) ? temp : max;
- }
- results.clear();
- } while (hasMoreRows);
- if (max != null) {
- AggregateResponse.Builder builder = AggregateResponse.newBuilder();
- builder.addFirstPart(ci.getProtoForCellType(max).toByteString());
- response = builder.build();
- }
- } catch (IOException e) {
- ResponseConverter.setControllerException(controller, e);
- } finally {
- if (scanner != null) {
- try {
- scanner.close();
- } catch (IOException ignored) {}
- }
- }
- log.info("Maximum from this region is "
- + env.getRegion().getRegionNameAsString() + ": " + max);
- done.run(response);
- }
-
- /**
- * Gives the minimum for a given combination of column qualifier and column
- * family, in the given row range as defined in the Scan object. In its
- * current implementation, it takes one column family and one column qualifier
- * (if provided). In case of null column qualifier, minimum value for the
- * entire column family will be returned.
- */
- @Override
- public void getMin(RpcController controller, AggregateArgument request,
- RpcCallback done) {
- AggregateResponse response = null;
- InternalScanner scanner = null;
- T min = null;
- try {
- ColumnInterpreter ci = constructColumnInterpreterFromRequest(request);
- T temp;
- Scan scan = ProtobufUtil.toScan(request.getScan());
- scanner = env.getRegion().getScanner(scan);
- List results = new ArrayList();
- byte[] colFamily = scan.getFamilies()[0];
- NavigableSet qualifiers = scan.getFamilyMap().get(colFamily);
- byte[] qualifier = null;
- if (qualifiers != null && !qualifiers.isEmpty()) {
- qualifier = qualifiers.pollFirst();
- }
- boolean hasMoreRows = false;
- do {
- hasMoreRows = scanner.next(results);
- for (KeyValue kv : results) {
- temp = ci.getValue(colFamily, qualifier, kv);
- min = (min == null || (temp != null && ci.compare(temp, min) < 0)) ? temp : min;
- }
- results.clear();
- } while (hasMoreRows);
- if (min != null) {
- response = AggregateResponse.newBuilder().addFirstPart(
- ci.getProtoForCellType(min).toByteString()).build();
- }
- } catch (IOException e) {
- ResponseConverter.setControllerException(controller, e);
- } finally {
- if (scanner != null) {
- try {
- scanner.close();
- } catch (IOException ignored) {}
- }
- }
- log.info("Minimum from this region is "
- + env.getRegion().getRegionNameAsString() + ": " + min);
- done.run(response);
- }
-
- /**
- * Gives the sum for a given combination of column qualifier and column
- * family, in the given row range as defined in the Scan object. In its
- * current implementation, it takes one column family and one column qualifier
- * (if provided). In case of null column qualifier, sum for the entire column
- * family will be returned.
- */
- @Override
- public void getSum(RpcController controller, AggregateArgument request,
- RpcCallback done) {
- AggregateResponse response = null;
- InternalScanner scanner = null;
- long sum = 0l;
- try {
- ColumnInterpreter ci = constructColumnInterpreterFromRequest(request);
- S sumVal = null;
- T temp;
- Scan scan = ProtobufUtil.toScan(request.getScan());
- scanner = env.getRegion().getScanner(scan);
- byte[] colFamily = scan.getFamilies()[0];
- NavigableSet qualifiers = scan.getFamilyMap().get(colFamily);
- byte[] qualifier = null;
- if (qualifiers != null && !qualifiers.isEmpty()) {
- qualifier = qualifiers.pollFirst();
- }
- List results = new ArrayList();
- boolean hasMoreRows = false;
- do {
- hasMoreRows = scanner.next(results);
- for (KeyValue kv : results) {
- temp = ci.getValue(colFamily, qualifier, kv);
- if (temp != null)
- sumVal = ci.add(sumVal, ci.castToReturnType(temp));
- }
- results.clear();
- } while (hasMoreRows);
- if (sumVal != null) {
- response = AggregateResponse.newBuilder().addFirstPart(
- ci.getProtoForPromotedType(sumVal).toByteString()).build();
- }
- } catch (IOException e) {
- ResponseConverter.setControllerException(controller, e);
- } finally {
- if (scanner != null) {
- try {
- scanner.close();
- } catch (IOException ignored) {}
- }
- }
- log.debug("Sum from this region is "
- + env.getRegion().getRegionNameAsString() + ": " + sum);
- done.run(response);
- }
-
- /**
- * Gives the row count for the given column family and column qualifier, in
- * the given row range as defined in the Scan object.
- * @throws IOException
- */
- @Override
- public void getRowNum(RpcController controller, AggregateArgument request,
- RpcCallback done) {
- AggregateResponse response = null;
- long counter = 0l;
- List results = new ArrayList();
- InternalScanner scanner = null;
- try {
- Scan scan = ProtobufUtil.toScan(request.getScan());
- byte[] colFamily = scan.getFamilies()[0];
- NavigableSet qualifiers = scan.getFamilyMap().get(colFamily);
- byte[] qualifier = null;
- if (qualifiers != null && !qualifiers.isEmpty()) {
- qualifier = qualifiers.pollFirst();
- }
- if (scan.getFilter() == null && qualifier == null)
- scan.setFilter(new FirstKeyOnlyFilter());
- scanner = env.getRegion().getScanner(scan);
- boolean hasMoreRows = false;
- do {
- hasMoreRows = scanner.next(results);
- if (results.size() > 0) {
- counter++;
- }
- results.clear();
- } while (hasMoreRows);
- ByteBuffer bb = ByteBuffer.allocate(8).putLong(counter);
- bb.rewind();
- response = AggregateResponse.newBuilder().addFirstPart(
- ByteString.copyFrom(bb)).build();
- } catch (IOException e) {
- ResponseConverter.setControllerException(controller, e);
- } finally {
- if (scanner != null) {
- try {
- scanner.close();
- } catch (IOException ignored) {}
- }
- }
- log.info("Row counter from this region is "
- + env.getRegion().getRegionNameAsString() + ": " + counter);
- done.run(response);
- }
-
- /**
- * Gives a Pair with first object as Sum and second object as row count,
- * computed for a given combination of column qualifier and column family in
- * the given row range as defined in the Scan object. In its current
- * implementation, it takes one column family and one column qualifier (if
- * provided). In case of null column qualifier, an aggregate sum over all the
- * entire column family will be returned.
- *
- * The average is computed in
- * {@link AggregationClient#avg(byte[], ColumnInterpreter, Scan)} by
- * processing results from all regions, so its "ok" to pass sum and a Long
- * type.
- */
- @Override
- public void getAvg(RpcController controller, AggregateArgument request,
- RpcCallback done) {
- AggregateResponse response = null;
- InternalScanner scanner = null;
- try {
- ColumnInterpreter ci = constructColumnInterpreterFromRequest(request);
- S sumVal = null;
- Long rowCountVal = 0l;
- Scan scan = ProtobufUtil.toScan(request.getScan());
- scanner = env.getRegion().getScanner(scan);
- byte[] colFamily = scan.getFamilies()[0];
- NavigableSet qualifiers = scan.getFamilyMap().get(colFamily);
- byte[] qualifier = null;
- if (qualifiers != null && !qualifiers.isEmpty()) {
- qualifier = qualifiers.pollFirst();
- }
- List results = new ArrayList();
- boolean hasMoreRows = false;
-
- do {
- results.clear();
- hasMoreRows = scanner.next(results);
- for (KeyValue kv : results) {
- sumVal = ci.add(sumVal, ci.castToReturnType(ci.getValue(colFamily,
- qualifier, kv)));
- }
- rowCountVal++;
- } while (hasMoreRows);
- if (sumVal != null) {
- ByteString first = ci.getProtoForPromotedType(sumVal).toByteString();
- AggregateResponse.Builder pair = AggregateResponse.newBuilder();
- pair.addFirstPart(first);
- ByteBuffer bb = ByteBuffer.allocate(8).putLong(rowCountVal);
- bb.rewind();
- pair.setSecondPart(ByteString.copyFrom(bb));
- response = pair.build();
- }
- } catch (IOException e) {
- ResponseConverter.setControllerException(controller, e);
- } finally {
- if (scanner != null) {
- try {
- scanner.close();
- } catch (IOException ignored) {}
- }
- }
- done.run(response);
- }
-
- /**
- * Gives a Pair with first object a List containing Sum and sum of squares,
- * and the second object as row count. It is computed for a given combination of
- * column qualifier and column family in the given row range as defined in the
- * Scan object. In its current implementation, it takes one column family and
- * one column qualifier (if provided). The idea is get the value of variance first:
- * the average of the squares less the square of the average a standard
- * deviation is square root of variance.
- */
- @Override
- public void getStd(RpcController controller, AggregateArgument request,
- RpcCallback done) {
- InternalScanner scanner = null;
- AggregateResponse response = null;
- try {
- ColumnInterpreter ci = constructColumnInterpreterFromRequest(request);
- S sumVal = null, sumSqVal = null, tempVal = null;
- long rowCountVal = 0l;
- Scan scan = ProtobufUtil.toScan(request.getScan());
- scanner = env.getRegion().getScanner(scan);
- byte[] colFamily = scan.getFamilies()[0];
- NavigableSet qualifiers = scan.getFamilyMap().get(colFamily);
- byte[] qualifier = null;
- if (qualifiers != null && !qualifiers.isEmpty()) {
- qualifier = qualifiers.pollFirst();
- }
- List results = new ArrayList();
-
- boolean hasMoreRows = false;
-
- do {
- tempVal = null;
- hasMoreRows = scanner.next(results);
- for (KeyValue kv : results) {
- tempVal = ci.add(tempVal, ci.castToReturnType(ci.getValue(colFamily,
- qualifier, kv)));
- }
- results.clear();
- sumVal = ci.add(sumVal, tempVal);
- sumSqVal = ci.add(sumSqVal, ci.multiply(tempVal, tempVal));
- rowCountVal++;
- } while (hasMoreRows);
- if (sumVal != null) {
- ByteString first_sumVal = ci.getProtoForPromotedType(sumVal).toByteString();
- ByteString first_sumSqVal = ci.getProtoForPromotedType(sumSqVal).toByteString();
- AggregateResponse.Builder pair = AggregateResponse.newBuilder();
- pair.addFirstPart(first_sumVal);
- pair.addFirstPart(first_sumSqVal);
- ByteBuffer bb = ByteBuffer.allocate(8).putLong(rowCountVal);
- bb.rewind();
- pair.setSecondPart(ByteString.copyFrom(bb));
- response = pair.build();
- }
- } catch (IOException e) {
- ResponseConverter.setControllerException(controller, e);
- } finally {
- if (scanner != null) {
- try {
- scanner.close();
- } catch (IOException ignored) {}
- }
- }
- done.run(response);
- }
-
- /**
- * Gives a List containing sum of values and sum of weights.
- * It is computed for the combination of column
- * family and column qualifier(s) in the given row range as defined in the
- * Scan object. In its current implementation, it takes one column family and
- * two column qualifiers. The first qualifier is for values column and
- * the second qualifier (optional) is for weight column.
- */
- @Override
- public void getMedian(RpcController controller, AggregateArgument request,
- RpcCallback done) {
- AggregateResponse response = null;
- InternalScanner scanner = null;
- try {
- ColumnInterpreter ci = constructColumnInterpreterFromRequest(request);
- S sumVal = null, sumWeights = null, tempVal = null, tempWeight = null;
- Scan scan = ProtobufUtil.toScan(request.getScan());
- scanner = env.getRegion().getScanner(scan);
- byte[] colFamily = scan.getFamilies()[0];
- NavigableSet qualifiers = scan.getFamilyMap().get(colFamily);
- byte[] valQualifier = null, weightQualifier = null;
- if (qualifiers != null && !qualifiers.isEmpty()) {
- valQualifier = qualifiers.pollFirst();
- // if weighted median is requested, get qualifier for the weight column
- weightQualifier = qualifiers.pollLast();
- }
- List results = new ArrayList();
-
- boolean hasMoreRows = false;
-
- do {
- tempVal = null;
- tempWeight = null;
- hasMoreRows = scanner.next(results);
- for (KeyValue kv : results) {
- tempVal = ci.add(tempVal, ci.castToReturnType(ci.getValue(colFamily,
- valQualifier, kv)));
- if (weightQualifier != null) {
- tempWeight = ci.add(tempWeight,
- ci.castToReturnType(ci.getValue(colFamily, weightQualifier, kv)));
- }
- }
- results.clear();
- sumVal = ci.add(sumVal, tempVal);
- sumWeights = ci.add(sumWeights, tempWeight);
- } while (hasMoreRows);
- ByteString first_sumVal = ci.getProtoForPromotedType(sumVal).toByteString();
- S s = sumWeights == null ? ci.castToReturnType(ci.getMinValue()) : sumWeights;
- ByteString first_sumWeights = ci.getProtoForPromotedType(s).toByteString();
- AggregateResponse.Builder pair = AggregateResponse.newBuilder();
- pair.addFirstPart(first_sumVal);
- pair.addFirstPart(first_sumWeights);
- response = pair.build();
- } catch (IOException e) {
- ResponseConverter.setControllerException(controller, e);
- } finally {
- if (scanner != null) {
- try {
- scanner.close();
- } catch (IOException ignored) {}
- }
- }
- done.run(response);
- }
-
- @SuppressWarnings("unchecked")
- ColumnInterpreter constructColumnInterpreterFromRequest(
- AggregateArgument request) throws IOException {
- String className = request.getInterpreterClassName();
- Class> cls;
- try {
- cls = Class.forName(className);
- ColumnInterpreter ci = (ColumnInterpreter) cls.newInstance();
- if (request.hasInterpreterSpecificBytes()) {
- ByteString b = request.getInterpreterSpecificBytes();
- P initMsg = ProtobufUtil.getParsedGenericInstance(ci.getClass(), 2, b);
- ci.initialize(initMsg);
- }
- return ci;
- } catch (ClassNotFoundException e) {
- throw new IOException(e);
- } catch (InstantiationException e) {
- throw new IOException(e);
- } catch (IllegalAccessException e) {
- throw new IOException(e);
- }
- }
-
- @Override
- public Service getService() {
- return this;
- }
-
- /**
- * Stores a reference to the coprocessor environment provided by the
- * {@link org.apache.hadoop.hbase.regionserver.RegionCoprocessorHost} from the region where this
- * coprocessor is loaded. Since this is a coprocessor endpoint, it always expects to be loaded
- * on a table region, so always expects this to be an instance of
- * {@link RegionCoprocessorEnvironment}.
- * @param env the environment provided by the coprocessor host
- * @throws IOException if the provided environment is not an instance of
- * {@code RegionCoprocessorEnvironment}
- */
- @Override
- public void start(CoprocessorEnvironment env) throws IOException {
- if (env instanceof RegionCoprocessorEnvironment) {
- this.env = (RegionCoprocessorEnvironment)env;
- } else {
- throw new CoprocessorException("Must be loaded on a table region!");
- }
- }
-
- @Override
- public void stop(CoprocessorEnvironment env) throws IOException {
- // nothing to do
- }
-
-}
diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/coprocessor/TestAggregateProtocol.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/coprocessor/TestAggregateProtocol.java
deleted file mode 100644
index 64760cd..0000000
--- a/hbase-server/src/test/java/org/apache/hadoop/hbase/coprocessor/TestAggregateProtocol.java
+++ /dev/null
@@ -1,852 +0,0 @@
-/*
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.hadoop.hbase.coprocessor;
-
-import static org.junit.Assert.assertEquals;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.hadoop.conf.Configuration;
-import org.apache.hadoop.hbase.*;
-import org.apache.hadoop.hbase.client.HTable;
-import org.apache.hadoop.hbase.client.Put;
-import org.apache.hadoop.hbase.client.Scan;
-import org.apache.hadoop.hbase.client.Durability;
-import org.apache.hadoop.hbase.client.coprocessor.AggregationClient;
-import org.apache.hadoop.hbase.client.coprocessor.LongColumnInterpreter;
-import org.apache.hadoop.hbase.filter.Filter;
-import org.apache.hadoop.hbase.filter.PrefixFilter;
-import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.EmptyMsg;
-import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.LongMsg;
-import org.apache.hadoop.hbase.util.Bytes;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.junit.experimental.categories.Category;
-
-/**
- * A test class to cover aggregate functions, that can be implemented using
- * Coprocessors.
- */
-@Category(MediumTests.class)
-public class TestAggregateProtocol {
- protected static Log myLog = LogFactory.getLog(TestAggregateProtocol.class);
-
- /**
- * Creating the test infrastructure.
- */
- private static final byte[] TEST_TABLE = Bytes.toBytes("TestTable");
- private static final byte[] TEST_FAMILY = Bytes.toBytes("TestFamily");
- private static final byte[] TEST_QUALIFIER = Bytes.toBytes("TestQualifier");
- private static final byte[] TEST_MULTI_CQ = Bytes.toBytes("TestMultiCQ");
-
- private static byte[] ROW = Bytes.toBytes("testRow");
- private static final int ROWSIZE = 20;
- private static final int rowSeperator1 = 5;
- private static final int rowSeperator2 = 12;
- private static byte[][] ROWS = makeN(ROW, ROWSIZE);
-
- private static HBaseTestingUtility util = new HBaseTestingUtility();
- private static Configuration conf = util.getConfiguration();
-
- /**
- * A set up method to start the test cluster. AggregateProtocolImpl is
- * registered and will be loaded during region startup.
- * @throws Exception
- */
- @BeforeClass
- public static void setupBeforeClass() throws Exception {
-
- conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY,
- "org.apache.hadoop.hbase.coprocessor.AggregateImplementation");
-
- util.startMiniCluster(2);
- HTable table = util.createTable(TEST_TABLE, TEST_FAMILY);
- util.createMultiRegions(util.getConfiguration(), table, TEST_FAMILY,
- new byte[][] { HConstants.EMPTY_BYTE_ARRAY, ROWS[rowSeperator1],
- ROWS[rowSeperator2] });
- /**
- * The testtable has one CQ which is always populated and one variable CQ
- * for each row rowkey1: CF:CQ CF:CQ1 rowKey2: CF:CQ CF:CQ2
- */
- for (int i = 0; i < ROWSIZE; i++) {
- Put put = new Put(ROWS[i]);
- put.setDurability(Durability.SKIP_WAL);
- Long l = new Long(i);
- put.add(TEST_FAMILY, TEST_QUALIFIER, Bytes.toBytes(l));
- table.put(put);
- Put p2 = new Put(ROWS[i]);
- put.setDurability(Durability.SKIP_WAL);
- p2.add(TEST_FAMILY, Bytes.add(TEST_MULTI_CQ, Bytes.toBytes(l)), Bytes
- .toBytes(l * 10));
- table.put(p2);
- }
- table.close();
- }
-
- /**
- * Shutting down the cluster
- * @throws Exception
- */
- @AfterClass
- public static void tearDownAfterClass() throws Exception {
- util.shutdownMiniCluster();
- }
-
- /**
- * an infrastructure method to prepare rows for the testtable.
- * @param base
- * @param n
- * @return
- */
- private static byte[][] makeN(byte[] base, int n) {
- byte[][] ret = new byte[n][];
- for (int i = 0; i < n; i++) {
- ret[i] = Bytes.add(base, Bytes.toBytes(i));
- }
- return ret;
- }
-
- /**
- * ****************** Test cases for Median **********************
- */
- /**
- * @throws Throwable
- */
- @Test
- public void testMedianWithValidRange() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY,TEST_QUALIFIER);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- long median = aClient.median(TEST_TABLE, ci,
- scan);
- assertEquals(8L, median);
- }
-
- /**
- * **************************** ROW COUNT Test cases *******************
- */
-
- /**
- * This will test rowcount with a valid range, i.e., a subset of rows. It will
- * be the most common use case.
- * @throws Throwable
- */
- @Test
- public void testRowCountWithValidRange() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
- scan.setStartRow(ROWS[2]);
- scan.setStopRow(ROWS[14]);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- long rowCount = aClient.rowCount(TEST_TABLE, ci, scan);
- assertEquals(12, rowCount);
- }
-
- /**
- * This will test the row count on the entire table. Startrow and endrow will
- * be null.
- * @throws Throwable
- */
- @Test
- public void testRowCountAllTable() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- long rowCount = aClient.rowCount(TEST_TABLE, ci,
- scan);
- assertEquals(ROWSIZE, rowCount);
- }
-
- /**
- * This will test the row count with startrow > endrow. The result should be
- * -1.
- * @throws Throwable
- */
- @Test
- public void testRowCountWithInvalidRange1() {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
- scan.setStartRow(ROWS[5]);
- scan.setStopRow(ROWS[2]);
-
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- long rowCount = -1;
- try {
- rowCount = aClient.rowCount(TEST_TABLE, ci, scan);
- } catch (Throwable e) {
- myLog.error("Exception thrown in the invalidRange method"
- + e.getStackTrace());
- }
- assertEquals(-1, rowCount);
- }
-
- /**
- * This will test the row count with startrow = endrow and they will be
- * non-null. The result should be 0, as it assumes a non-get query.
- * @throws Throwable
- */
- @Test
- public void testRowCountWithInvalidRange2() {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
- scan.setStartRow(ROWS[5]);
- scan.setStopRow(ROWS[5]);
-
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- long rowCount = -1;
- try {
- rowCount = aClient.rowCount(TEST_TABLE, ci, scan);
- } catch (Throwable e) {
- rowCount = 0;
- }
- assertEquals(0, rowCount);
- }
-
- /**
- * This should return a 0
- */
- @Test
- public void testRowCountWithNullCF() {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.setStartRow(ROWS[5]);
- scan.setStopRow(ROWS[15]);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- long rowCount = -1;
- try {
- rowCount = aClient.rowCount(TEST_TABLE, ci, scan);
- } catch (Throwable e) {
- rowCount = 0;
- }
- assertEquals(0, rowCount);
- }
-
- @Test
- public void testRowCountWithNullCQ() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- long rowCount = aClient.rowCount(TEST_TABLE, ci,
- scan);
- assertEquals(20, rowCount);
- }
-
- @Test
- public void testRowCountWithPrefixFilter() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- Filter f = new PrefixFilter(Bytes.toBytes("foo:bar"));
- scan.setFilter(f);
- long rowCount = aClient.rowCount(TEST_TABLE, ci,
- scan);
- assertEquals(0, rowCount);
- }
-
- /**
- * ***************Test cases for Maximum *******************
- */
-
- /**
- * give max for the entire table.
- * @throws Throwable
- */
- @Test
- public void testMaxWithValidRange() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- long maximum = aClient.max(TEST_TABLE, ci, scan);
- assertEquals(19, maximum);
- }
-
- /**
- * @throws Throwable
- */
- @Test
- public void testMaxWithValidRange2() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
- scan.setStartRow(ROWS[5]);
- scan.setStopRow(ROWS[15]);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- long max = aClient.max(TEST_TABLE, ci, scan);
- assertEquals(14, max);
- }
-
- @Test
- public void testMaxWithValidRangeWithNoCQ() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- long maximum = aClient.max(TEST_TABLE, ci, scan);
- assertEquals(190, maximum);
- }
-
- @Test
- public void testMaxWithValidRange2WithNoCQ() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- scan.setStartRow(ROWS[6]);
- scan.setStopRow(ROWS[7]);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- long max = aClient.max(TEST_TABLE, ci, scan);
- assertEquals(60, max);
- }
-
- @Test
- public void testMaxWithValidRangeWithNullCF() {
- AggregationClient aClient = new AggregationClient(conf);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- Scan scan = new Scan();
- Long max = null;
- try {
- max = aClient.max(TEST_TABLE, ci, scan);
- } catch (Throwable e) {
- max = null;
- }
- assertEquals(null, max);// CP will throw an IOException about the
- // null column family, and max will be set to 0
- }
-
- @Test
- public void testMaxWithInvalidRange() {
- AggregationClient aClient = new AggregationClient(conf);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- Scan scan = new Scan();
- scan.setStartRow(ROWS[4]);
- scan.setStopRow(ROWS[2]);
- scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
- long max = Long.MIN_VALUE;
- try {
- max = aClient.max(TEST_TABLE, ci, scan);
- } catch (Throwable e) {
- max = 0;
- }
- assertEquals(0, max);// control should go to the catch block
- }
-
- @Test
- public void testMaxWithInvalidRange2() throws Throwable {
- long max = Long.MIN_VALUE;
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
- scan.setStartRow(ROWS[4]);
- scan.setStopRow(ROWS[4]);
- try {
- AggregationClient aClient = new AggregationClient(conf);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- max = aClient.max(TEST_TABLE, ci, scan);
- } catch (Exception e) {
- max = 0;
- }
- assertEquals(0, max);// control should go to the catch block
- }
-
- @Test
- public void testMaxWithFilter() throws Throwable {
- Long max = 0l;
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
- Filter f = new PrefixFilter(Bytes.toBytes("foo:bar"));
- scan.setFilter(f);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- max = aClient.max(TEST_TABLE, ci, scan);
- assertEquals(null, max);
- }
-
- /**
- * **************************Test cases for Minimum ***********************
- */
-
- /**
- * @throws Throwable
- */
- @Test
- public void testMinWithValidRange() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
- scan.setStartRow(HConstants.EMPTY_START_ROW);
- scan.setStopRow(HConstants.EMPTY_END_ROW);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- Long min = aClient.min(TEST_TABLE, ci,
- scan);
- assertEquals(0l, min.longValue());
- }
-
- /**
- * @throws Throwable
- */
- @Test
- public void testMinWithValidRange2() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
- scan.setStartRow(ROWS[5]);
- scan.setStopRow(ROWS[15]);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- long min = aClient.min(TEST_TABLE, ci, scan);
- assertEquals(5, min);
- }
-
- @Test
- public void testMinWithValidRangeWithNoCQ() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- scan.setStartRow(HConstants.EMPTY_START_ROW);
- scan.setStopRow(HConstants.EMPTY_END_ROW);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- long min = aClient.min(TEST_TABLE, ci,
- scan);
- assertEquals(0, min);
- }
-
- @Test
- public void testMinWithValidRange2WithNoCQ() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- scan.setStartRow(ROWS[6]);
- scan.setStopRow(ROWS[7]);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- long min = aClient.min(TEST_TABLE, ci, scan);
- assertEquals(6, min);
- }
-
- @Test
- public void testMinWithValidRangeWithNullCF() {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.setStartRow(ROWS[5]);
- scan.setStopRow(ROWS[15]);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- Long min = null;
- try {
- min = aClient.min(TEST_TABLE, ci, scan);
- } catch (Throwable e) {
- }
- assertEquals(null, min);// CP will throw an IOException about the
- // null column family, and max will be set to 0
- }
-
- @Test
- public void testMinWithInvalidRange() {
- AggregationClient aClient = new AggregationClient(conf);
- Long min = null;
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- scan.setStartRow(ROWS[4]);
- scan.setStopRow(ROWS[2]);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- try {
- min = aClient.min(TEST_TABLE, ci, scan);
- } catch (Throwable e) {
- }
- assertEquals(null, min);// control should go to the catch block
- }
-
- @Test
- public void testMinWithInvalidRange2() {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- scan.setStartRow(ROWS[6]);
- scan.setStopRow(ROWS[6]);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- Long min = null;
- try {
- min = aClient.min(TEST_TABLE, ci, scan);
- } catch (Throwable e) {
- }
- assertEquals(null, min);// control should go to the catch block
- }
-
- @Test
- public void testMinWithFilter() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
- Filter f = new PrefixFilter(Bytes.toBytes("foo:bar"));
- scan.setFilter(f);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- Long min = null;
- min = aClient.min(TEST_TABLE, ci, scan);
- assertEquals(null, min);
- }
-
- /**
- * *************** Test cases for Sum *********************
- */
- /**
- * @throws Throwable
- */
- @Test
- public void testSumWithValidRange() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY,TEST_QUALIFIER);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- long sum = aClient.sum(TEST_TABLE, ci,
- scan);
- assertEquals(190, sum);
- }
-
- /**
- * @throws Throwable
- */
- @Test
- public void testSumWithValidRange2() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY,TEST_QUALIFIER);
- scan.setStartRow(ROWS[5]);
- scan.setStopRow(ROWS[15]);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- long sum = aClient.sum(TEST_TABLE, ci, scan);
- assertEquals(95, sum);
- }
-
- @Test
- public void testSumWithValidRangeWithNoCQ() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- long sum = aClient.sum(TEST_TABLE, ci,
- scan);
- assertEquals(190 + 1900, sum);
- }
-
- @Test
- public void testSumWithValidRange2WithNoCQ() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- scan.setStartRow(ROWS[6]);
- scan.setStopRow(ROWS[7]);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- long sum = aClient.sum(TEST_TABLE, ci, scan);
- assertEquals(6 + 60, sum);
- }
-
- @Test
- public void testSumWithValidRangeWithNullCF() {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.setStartRow(ROWS[6]);
- scan.setStopRow(ROWS[7]);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- Long sum = null;
- try {
- sum = aClient.sum(TEST_TABLE, ci, scan);
- } catch (Throwable e) {
- }
- assertEquals(null, sum);// CP will throw an IOException about the
- // null column family, and max will be set to 0
- }
-
- @Test
- public void testSumWithInvalidRange() {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- scan.setStartRow(ROWS[6]);
- scan.setStopRow(ROWS[2]);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- Long sum = null;
- try {
- sum = aClient.sum(TEST_TABLE, ci, scan);
- } catch (Throwable e) {
- }
- assertEquals(null, sum);// control should go to the catch block
- }
-
- @Test
- public void testSumWithFilter() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Filter f = new PrefixFilter(Bytes.toBytes("foo:bar"));
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- scan.setFilter(f);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- Long sum = null;
- sum = aClient.sum(TEST_TABLE, ci, scan);
- assertEquals(null, sum);
- }
-
- /**
- * ****************************** Test Cases for Avg **************
- */
- /**
- * @throws Throwable
- */
- @Test
- public void testAvgWithValidRange() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY,TEST_QUALIFIER);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- double avg = aClient.avg(TEST_TABLE, ci,
- scan);
- assertEquals(9.5, avg, 0);
- }
-
- /**
- * @throws Throwable
- */
- @Test
- public void testAvgWithValidRange2() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY,TEST_QUALIFIER);
- scan.setStartRow(ROWS[5]);
- scan.setStopRow(ROWS[15]);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- double avg = aClient.avg(TEST_TABLE, ci, scan);
- assertEquals(9.5, avg, 0);
- }
-
- @Test
- public void testAvgWithValidRangeWithNoCQ() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- double avg = aClient.avg(TEST_TABLE, ci,
- scan);
- assertEquals(104.5, avg, 0);
- }
-
- @Test
- public void testAvgWithValidRange2WithNoCQ() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- scan.setStartRow(ROWS[6]);
- scan.setStopRow(ROWS[7]);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- double avg = aClient.avg(TEST_TABLE, ci, scan);
- assertEquals(6 + 60, avg, 0);
- }
-
- @Test
- public void testAvgWithValidRangeWithNullCF() {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- Double avg = null;
- try {
- avg = aClient.avg(TEST_TABLE, ci, scan);
- } catch (Throwable e) {
- }
- assertEquals(null, avg);// CP will throw an IOException about the
- // null column family, and max will be set to 0
- }
-
- @Test
- public void testAvgWithInvalidRange() {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY,TEST_QUALIFIER);
- scan.setStartRow(ROWS[5]);
- scan.setStopRow(ROWS[1]);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- Double avg = null;
- try {
- avg = aClient.avg(TEST_TABLE, ci, scan);
- } catch (Throwable e) {
- }
- assertEquals(null, avg);// control should go to the catch block
- }
-
- @Test
- public void testAvgWithFilter() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY,TEST_QUALIFIER);
- Filter f = new PrefixFilter(Bytes.toBytes("foo:bar"));
- scan.setFilter(f);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- Double avg = null;
- avg = aClient.avg(TEST_TABLE, ci, scan);
- assertEquals(Double.NaN, avg, 0);
- }
-
- /**
- * ****************** Test cases for STD **********************
- */
- /**
- * @throws Throwable
- */
- @Test
- public void testStdWithValidRange() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY,TEST_QUALIFIER);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- double std = aClient.std(TEST_TABLE, ci,
- scan);
- assertEquals(5.766, std, 0.05d);
- }
-
- /**
- * @throws Throwable
- */
- @Test
- public void testStdWithValidRange2() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY,TEST_QUALIFIER);
- scan.setStartRow(ROWS[5]);
- scan.setStopRow(ROWS[15]);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- double std = aClient.std(TEST_TABLE, ci, scan);
- assertEquals(2.87, std, 0.05d);
- }
-
- @Test
- public void testStdWithValidRangeWithNoCQ() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- double std = aClient.std(TEST_TABLE, ci,
- scan);
- assertEquals(63.42, std, 0.05d);
- }
-
- @Test
- public void testStdWithValidRange2WithNoCQ() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- scan.setStartRow(ROWS[6]);
- scan.setStopRow(ROWS[7]);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- double std = aClient.std(TEST_TABLE, ci, scan);
- assertEquals(0, std, 0);
- }
-
- @Test
- public void testStdWithValidRangeWithNullCF() {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.setStartRow(ROWS[6]);
- scan.setStopRow(ROWS[17]);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- Double std = null;
- try {
- std = aClient.std(TEST_TABLE, ci, scan);
- } catch (Throwable e) {
- }
- assertEquals(null, std);// CP will throw an IOException about the
- // null column family, and max will be set to 0
- }
-
- @Test
- public void testStdWithInvalidRange() {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- scan.setStartRow(ROWS[6]);
- scan.setStopRow(ROWS[1]);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- Double std = null;
- try {
- std = aClient.std(TEST_TABLE, ci, scan);
- } catch (Throwable e) {
- }
- assertEquals(null, std);// control should go to the catch block
- }
-
- @Test
- public void testStdWithFilter() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Filter f = new PrefixFilter(Bytes.toBytes("foo:bar"));
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- scan.setFilter(f);
- final ColumnInterpreter ci =
- new LongColumnInterpreter();
- Double std = null;
- std = aClient.std(TEST_TABLE, ci, scan);
- assertEquals(Double.NaN, std, 0);
- }
-
-}
-
diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/coprocessor/TestBigDecimalColumnInterpreter.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/coprocessor/TestBigDecimalColumnInterpreter.java
deleted file mode 100644
index f7056c5..0000000
--- a/hbase-server/src/test/java/org/apache/hadoop/hbase/coprocessor/TestBigDecimalColumnInterpreter.java
+++ /dev/null
@@ -1,712 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.hadoop.hbase.coprocessor;
-
-import static org.junit.Assert.assertEquals;
-import java.math.BigDecimal;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.hadoop.conf.Configuration;
-import org.apache.hadoop.hbase.*;
-import org.apache.hadoop.hbase.client.HTable;
-import org.apache.hadoop.hbase.client.Put;
-import org.apache.hadoop.hbase.client.Scan;
-import org.apache.hadoop.hbase.client.Durability;
-import org.apache.hadoop.hbase.client.coprocessor.AggregationClient;
-import org.apache.hadoop.hbase.client.coprocessor.BigDecimalColumnInterpreter;
-import org.apache.hadoop.hbase.filter.Filter;
-import org.apache.hadoop.hbase.filter.PrefixFilter;
-import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.BigDecimalMsg;
-import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.EmptyMsg;
-import org.apache.hadoop.hbase.util.Bytes;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.junit.experimental.categories.Category;
-
-/**
- * A test class to test BigDecimalColumnInterpreter for AggregationsProtocol
- */
-@Category(MediumTests.class)
-public class TestBigDecimalColumnInterpreter {
- protected static Log myLog = LogFactory.getLog(TestBigDecimalColumnInterpreter.class);
-
- /**
- * Creating the test infrastructure.
- */
- private static final byte[] TEST_TABLE = Bytes.toBytes("TestTable");
- private static final byte[] TEST_FAMILY = Bytes.toBytes("TestFamily");
- private static final byte[] TEST_QUALIFIER = Bytes.toBytes("TestQualifier");
- private static final byte[] TEST_MULTI_CQ = Bytes.toBytes("TestMultiCQ");
-
- private static byte[] ROW = Bytes.toBytes("testRow");
- private static final int ROWSIZE = 20;
- private static final int rowSeperator1 = 5;
- private static final int rowSeperator2 = 12;
- private static byte[][] ROWS = makeN(ROW, ROWSIZE);
-
- private static HBaseTestingUtility util = new HBaseTestingUtility();
- private static Configuration conf = util.getConfiguration();
-
- /**
- * A set up method to start the test cluster. AggregateProtocolImpl is registered and will be
- * loaded during region startup.
- * @throws Exception
- */
- @BeforeClass
- public static void setupBeforeClass() throws Exception {
-
- conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY,
- "org.apache.hadoop.hbase.coprocessor.AggregateImplementation");
-
- util.startMiniCluster(2);
- HTable table = util.createTable(TEST_TABLE, TEST_FAMILY);
- util.createMultiRegions(util.getConfiguration(), table, TEST_FAMILY, new byte[][] {
- HConstants.EMPTY_BYTE_ARRAY, ROWS[rowSeperator1], ROWS[rowSeperator2] });
- /**
- * The testtable has one CQ which is always populated and one variable CQ for each row rowkey1:
- * CF:CQ CF:CQ1 rowKey2: CF:CQ CF:CQ2
- */
- for (int i = 0; i < ROWSIZE; i++) {
- Put put = new Put(ROWS[i]);
- put.setDurability(Durability.SKIP_WAL);
- BigDecimal bd = new BigDecimal(i);
- put.add(TEST_FAMILY, TEST_QUALIFIER, Bytes.toBytes(bd));
- table.put(put);
- Put p2 = new Put(ROWS[i]);
- put.setDurability(Durability.SKIP_WAL);
- p2.add(TEST_FAMILY, Bytes.add(TEST_MULTI_CQ, Bytes.toBytes(bd)),
- Bytes.toBytes(bd.multiply(new BigDecimal("0.10"))));
- table.put(p2);
- }
- table.close();
- }
-
- /**
- * Shutting down the cluster
- * @throws Exception
- */
- @AfterClass
- public static void tearDownAfterClass() throws Exception {
- util.shutdownMiniCluster();
- }
-
- /**
- * an infrastructure method to prepare rows for the testtable.
- * @param base
- * @param n
- * @return
- */
- private static byte[][] makeN(byte[] base, int n) {
- byte[][] ret = new byte[n][];
- for (int i = 0; i < n; i++) {
- ret[i] = Bytes.add(base, Bytes.toBytes(i));
- }
- return ret;
- }
-
- /**
- * ****************** Test cases for Median **********************
- */
- /**
- * @throws Throwable
- */
- @Test
- public void testMedianWithValidRange() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- BigDecimal median = aClient.median(TEST_TABLE, ci, scan);
- assertEquals(new BigDecimal("8.00"), median);
- }
-
- /**
- * ***************Test cases for Maximum *******************
- */
-
- /**
- * give max for the entire table.
- * @throws Throwable
- */
- @Test
- public void testMaxWithValidRange() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- BigDecimal maximum = aClient.max(TEST_TABLE, ci, scan);
- assertEquals(new BigDecimal("19.00"), maximum);
- }
-
- /**
- * @throws Throwable
- */
- @Test
- public void testMaxWithValidRange2() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
- scan.setStartRow(ROWS[5]);
- scan.setStopRow(ROWS[15]);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- BigDecimal max = aClient.max(TEST_TABLE, ci, scan);
- assertEquals(new BigDecimal("14.00"), max);
- }
-
- @Test
- public void testMaxWithValidRangeWithNoCQ() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- BigDecimal maximum = aClient.max(TEST_TABLE, ci, scan);
- assertEquals(new BigDecimal("19.00"), maximum);
- }
-
- @Test
- public void testMaxWithValidRange2WithNoCQ() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- scan.setStartRow(ROWS[6]);
- scan.setStopRow(ROWS[7]);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- BigDecimal max = aClient.max(TEST_TABLE, ci, scan);
- assertEquals(new BigDecimal("6.00"), max);
- }
-
- @Test
- public void testMaxWithValidRangeWithNullCF() {
- AggregationClient aClient = new AggregationClient(conf);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- Scan scan = new Scan();
- BigDecimal max = null;
- try {
- max = aClient.max(TEST_TABLE, ci, scan);
- } catch (Throwable e) {
- max = null;
- }
- assertEquals(null, max);// CP will throw an IOException about the
- // null column family, and max will be set to 0
- }
-
- @Test
- public void testMaxWithInvalidRange() {
- AggregationClient aClient = new AggregationClient(conf);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- Scan scan = new Scan();
- scan.setStartRow(ROWS[4]);
- scan.setStopRow(ROWS[2]);
- scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
- BigDecimal max = new BigDecimal(Long.MIN_VALUE);
- ;
- try {
- max = aClient.max(TEST_TABLE, ci, scan);
- } catch (Throwable e) {
- max = BigDecimal.ZERO;
- }
- assertEquals(BigDecimal.ZERO, max);// control should go to the catch block
- }
-
- @Test
- public void testMaxWithInvalidRange2() throws Throwable {
- BigDecimal max = new BigDecimal(Long.MIN_VALUE);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
- scan.setStartRow(ROWS[4]);
- scan.setStopRow(ROWS[4]);
- try {
- AggregationClient aClient = new AggregationClient(conf);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- max = aClient.max(TEST_TABLE, ci, scan);
- } catch (Exception e) {
- max = BigDecimal.ZERO;
- }
- assertEquals(BigDecimal.ZERO, max);// control should go to the catch block
- }
-
- @Test
- public void testMaxWithFilter() throws Throwable {
- BigDecimal max = BigDecimal.ZERO;
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
- Filter f = new PrefixFilter(Bytes.toBytes("foo:bar"));
- scan.setFilter(f);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- max = aClient.max(TEST_TABLE, ci, scan);
- assertEquals(null, max);
- }
-
- /**
- * **************************Test cases for Minimum ***********************
- */
-
- /**
- * @throws Throwable
- */
- @Test
- public void testMinWithValidRange() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
- scan.setStartRow(HConstants.EMPTY_START_ROW);
- scan.setStopRow(HConstants.EMPTY_END_ROW);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- BigDecimal min = aClient.min(TEST_TABLE, ci, scan);
- assertEquals(new BigDecimal("0.00"), min);
- }
-
- /**
- * @throws Throwable
- */
- @Test
- public void testMinWithValidRange2() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
- scan.setStartRow(ROWS[5]);
- scan.setStopRow(ROWS[15]);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- BigDecimal min = aClient.min(TEST_TABLE, ci, scan);
- assertEquals(new BigDecimal("5.00"), min);
- }
-
- @Test
- public void testMinWithValidRangeWithNoCQ() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- scan.setStartRow(HConstants.EMPTY_START_ROW);
- scan.setStopRow(HConstants.EMPTY_END_ROW);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- BigDecimal min = aClient.min(TEST_TABLE, ci, scan);
- assertEquals(new BigDecimal("0.00"), min);
- }
-
- @Test
- public void testMinWithValidRange2WithNoCQ() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- scan.setStartRow(ROWS[6]);
- scan.setStopRow(ROWS[7]);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- BigDecimal min = aClient.min(TEST_TABLE, ci, scan);
- assertEquals(new BigDecimal("0.60"), min);
- }
-
- @Test
- public void testMinWithValidRangeWithNullCF() {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.setStartRow(ROWS[5]);
- scan.setStopRow(ROWS[15]);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- BigDecimal min = null;
- try {
- min = aClient.min(TEST_TABLE, ci, scan);
- } catch (Throwable e) {
- }
- assertEquals(null, min);// CP will throw an IOException about the
- // null column family, and max will be set to 0
- }
-
- @Test
- public void testMinWithInvalidRange() {
- AggregationClient aClient = new AggregationClient(conf);
- BigDecimal min = null;
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- scan.setStartRow(ROWS[4]);
- scan.setStopRow(ROWS[2]);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- try {
- min = aClient.min(TEST_TABLE, ci, scan);
- } catch (Throwable e) {
- }
- assertEquals(null, min);// control should go to the catch block
- }
-
- @Test
- public void testMinWithInvalidRange2() {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- scan.setStartRow(ROWS[6]);
- scan.setStopRow(ROWS[6]);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- BigDecimal min = null;
- try {
- min = aClient.min(TEST_TABLE, ci, scan);
- } catch (Throwable e) {
- }
- assertEquals(null, min);// control should go to the catch block
- }
-
- @Test
- public void testMinWithFilter() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
- Filter f = new PrefixFilter(Bytes.toBytes("foo:bar"));
- scan.setFilter(f);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- BigDecimal min = null;
- min = aClient.min(TEST_TABLE, ci, scan);
- assertEquals(null, min);
- }
-
- /**
- * *************** Test cases for Sum *********************
- */
- /**
- * @throws Throwable
- */
- @Test
- public void testSumWithValidRange() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- BigDecimal sum = aClient.sum(TEST_TABLE, ci, scan);
- assertEquals(new BigDecimal("190.00"), sum);
- }
-
- /**
- * @throws Throwable
- */
- @Test
- public void testSumWithValidRange2() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
- scan.setStartRow(ROWS[5]);
- scan.setStopRow(ROWS[15]);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- BigDecimal sum = aClient.sum(TEST_TABLE, ci, scan);
- assertEquals(new BigDecimal("95.00"), sum);
- }
-
- @Test
- public void testSumWithValidRangeWithNoCQ() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- BigDecimal sum = aClient.sum(TEST_TABLE, ci, scan);
- assertEquals(new BigDecimal("209.00"), sum); // 190 + 19
- }
-
- @Test
- public void testSumWithValidRange2WithNoCQ() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- scan.setStartRow(ROWS[6]);
- scan.setStopRow(ROWS[7]);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- BigDecimal sum = aClient.sum(TEST_TABLE, ci, scan);
- assertEquals(new BigDecimal("6.60"), sum); // 6 + 60
- }
-
- @Test
- public void testSumWithValidRangeWithNullCF() {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.setStartRow(ROWS[6]);
- scan.setStopRow(ROWS[7]);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- BigDecimal sum = null;
- try {
- sum = aClient.sum(TEST_TABLE, ci, scan);
- } catch (Throwable e) {
- }
- assertEquals(null, sum);// CP will throw an IOException about the
- // null column family, and max will be set to 0
- }
-
- @Test
- public void testSumWithInvalidRange() {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- scan.setStartRow(ROWS[6]);
- scan.setStopRow(ROWS[2]);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- BigDecimal sum = null;
- try {
- sum = aClient.sum(TEST_TABLE, ci, scan);
- } catch (Throwable e) {
- }
- assertEquals(null, sum);// control should go to the catch block
- }
-
- @Test
- public void testSumWithFilter() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Filter f = new PrefixFilter(Bytes.toBytes("foo:bar"));
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- scan.setFilter(f);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- BigDecimal sum = null;
- sum = aClient.sum(TEST_TABLE, ci, scan);
- assertEquals(null, sum);
- }
-
- /**
- * ****************************** Test Cases for Avg **************
- */
- /**
- * @throws Throwable
- */
- @Test
- public void testAvgWithValidRange() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- double avg = aClient.avg(TEST_TABLE, ci, scan);
- assertEquals(9.5, avg, 0);
- }
-
- /**
- * @throws Throwable
- */
- @Test
- public void testAvgWithValidRange2() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
- scan.setStartRow(ROWS[5]);
- scan.setStopRow(ROWS[15]);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- double avg = aClient.avg(TEST_TABLE, ci, scan);
- assertEquals(9.5, avg, 0);
- }
-
- @Test
- public void testAvgWithValidRangeWithNoCQ() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- double avg = aClient.avg(TEST_TABLE, ci, scan);
- assertEquals(10.45, avg, 0.01);
- }
-
- @Test
- public void testAvgWithValidRange2WithNoCQ() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- scan.setStartRow(ROWS[6]);
- scan.setStopRow(ROWS[7]);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- double avg = aClient.avg(TEST_TABLE, ci, scan);
- assertEquals(6 + 0.60, avg, 0);
- }
-
- @Test
- public void testAvgWithValidRangeWithNullCF() {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- Double avg = null;
- try {
- avg = aClient.avg(TEST_TABLE, ci, scan);
- } catch (Throwable e) {
- }
- assertEquals(null, avg);// CP will throw an IOException about the
- // null column family, and max will be set to 0
- }
-
- @Test
- public void testAvgWithInvalidRange() {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
- scan.setStartRow(ROWS[5]);
- scan.setStopRow(ROWS[1]);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- Double avg = null;
- try {
- avg = aClient.avg(TEST_TABLE, ci, scan);
- } catch (Throwable e) {
- }
- assertEquals(null, avg);// control should go to the catch block
- }
-
- @Test
- public void testAvgWithFilter() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
- Filter f = new PrefixFilter(Bytes.toBytes("foo:bar"));
- scan.setFilter(f);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- Double avg = null;
- avg = aClient.avg(TEST_TABLE, ci, scan);
- assertEquals(Double.NaN, avg, 0);
- }
-
- /**
- * ****************** Test cases for STD **********************
- */
- /**
- * @throws Throwable
- */
- @Test
- public void testStdWithValidRange() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- double std = aClient.std(TEST_TABLE, ci, scan);
- assertEquals(5.766, std, 0.05d);
- }
-
- /**
- * need to change this
- * @throws Throwable
- */
- @Test
- public void testStdWithValidRange2() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addColumn(TEST_FAMILY, TEST_QUALIFIER);
- scan.setStartRow(ROWS[5]);
- scan.setStopRow(ROWS[15]);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- double std = aClient.std(TEST_TABLE, ci, scan);
- assertEquals(2.87, std, 0.05d);
- }
-
- /**
- * need to change this
- * @throws Throwable
- */
- @Test
- public void testStdWithValidRangeWithNoCQ() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- double std = aClient.std(TEST_TABLE, ci, scan);
- assertEquals(6.342, std, 0.05d);
- }
-
- @Test
- public void testStdWithValidRange2WithNoCQ() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- scan.setStartRow(ROWS[6]);
- scan.setStopRow(ROWS[7]);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- double std = aClient.std(TEST_TABLE, ci, scan);
- System.out.println("std is:" + std);
- assertEquals(0, std, 0.05d);
- }
-
- @Test
- public void testStdWithValidRangeWithNullCF() {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.setStartRow(ROWS[6]);
- scan.setStopRow(ROWS[17]);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- Double std = null;
- try {
- std = aClient.std(TEST_TABLE, ci, scan);
- } catch (Throwable e) {
- }
- assertEquals(null, std);// CP will throw an IOException about the
- // null column family, and max will be set to 0
- }
-
- @Test
- public void testStdWithInvalidRange() {
- AggregationClient aClient = new AggregationClient(conf);
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- scan.setStartRow(ROWS[6]);
- scan.setStopRow(ROWS[1]);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- Double std = null;
- try {
- std = aClient.std(TEST_TABLE, ci, scan);
- } catch (Throwable e) {
- }
- assertEquals(null, std);// control should go to the catch block
- }
-
- @Test
- public void testStdWithFilter() throws Throwable {
- AggregationClient aClient = new AggregationClient(conf);
- Filter f = new PrefixFilter(Bytes.toBytes("foo:bar"));
- Scan scan = new Scan();
- scan.addFamily(TEST_FAMILY);
- scan.setFilter(f);
- final ColumnInterpreter ci =
- new BigDecimalColumnInterpreter();
- Double std = null;
- std = aClient.std(TEST_TABLE, ci, scan);
- assertEquals(Double.NaN, std, 0);
- }
-
-}
\ No newline at end of file