001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.mapred;
019
020import static org.junit.Assert.assertArrayEquals;
021import static org.junit.Assert.assertNull;
022import static org.mockito.ArgumentMatchers.any;
023import static org.mockito.Mockito.mock;
024import static org.mockito.Mockito.times;
025import static org.mockito.Mockito.verify;
026import static org.mockito.Mockito.verifyNoInteractions;
027import static org.mockito.Mockito.verifyNoMoreInteractions;
028import static org.mockito.Mockito.when;
029
030import java.io.IOException;
031import java.util.List;
032import java.util.concurrent.atomic.AtomicBoolean;
033import org.apache.hadoop.conf.Configuration;
034import org.apache.hadoop.hbase.Cell;
035import org.apache.hadoop.hbase.HBaseClassTestRule;
036import org.apache.hadoop.hbase.KeyValue;
037import org.apache.hadoop.hbase.client.Result;
038import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
039import org.apache.hadoop.hbase.testclassification.MapReduceTests;
040import org.apache.hadoop.hbase.testclassification.SmallTests;
041import org.apache.hadoop.hbase.util.Bytes;
042import org.apache.hadoop.mapred.JobConf;
043import org.apache.hadoop.mapred.OutputCollector;
044import org.apache.hadoop.mapred.Reporter;
045import org.junit.Assert;
046import org.junit.ClassRule;
047import org.junit.Test;
048import org.junit.experimental.categories.Category;
049
050import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableList;
051
052@Category({ MapReduceTests.class, SmallTests.class })
053public class TestGroupingTableMap {
054
055  @ClassRule
056  public static final HBaseClassTestRule CLASS_RULE =
057    HBaseClassTestRule.forClass(TestGroupingTableMap.class);
058
059  @Test
060  @SuppressWarnings("unchecked")
061  public void shouldNotCallCollectonSinceFindUniqueKeyValueMoreThanOnes() throws Exception {
062    GroupingTableMap gTableMap = null;
063    try {
064      Result result = mock(Result.class);
065      Reporter reporter = mock(Reporter.class);
066      gTableMap = new GroupingTableMap();
067      Configuration cfg = new Configuration();
068      cfg.set(GroupingTableMap.GROUP_COLUMNS, "familyA:qualifierA familyB:qualifierB");
069      JobConf jobConf = new JobConf(cfg);
070      gTableMap.configure(jobConf);
071
072      byte[] row = {};
073      List<Cell> keyValues = ImmutableList.<Cell> of(
074        new KeyValue(row, "familyA".getBytes(), "qualifierA".getBytes(), Bytes.toBytes("1111")),
075        new KeyValue(row, "familyA".getBytes(), "qualifierA".getBytes(), Bytes.toBytes("2222")),
076        new KeyValue(row, "familyB".getBytes(), "qualifierB".getBytes(), Bytes.toBytes("3333")));
077      when(result.listCells()).thenReturn(keyValues);
078      OutputCollector<ImmutableBytesWritable, Result> outputCollectorMock =
079        mock(OutputCollector.class);
080      gTableMap.map(null, result, outputCollectorMock, reporter);
081      verify(result).listCells();
082      verifyNoInteractions(outputCollectorMock);
083    } finally {
084      if (gTableMap != null) gTableMap.close();
085    }
086  }
087
088  @Test
089  @SuppressWarnings("unchecked")
090  public void shouldCreateNewKeyAlthoughExtraKey() throws Exception {
091    GroupingTableMap gTableMap = null;
092    try {
093      Result result = mock(Result.class);
094      Reporter reporter = mock(Reporter.class);
095      gTableMap = new GroupingTableMap();
096      Configuration cfg = new Configuration();
097      cfg.set(GroupingTableMap.GROUP_COLUMNS, "familyA:qualifierA familyB:qualifierB");
098      JobConf jobConf = new JobConf(cfg);
099      gTableMap.configure(jobConf);
100
101      byte[] row = {};
102      List<Cell> keyValues = ImmutableList.<Cell> of(
103        new KeyValue(row, "familyA".getBytes(), "qualifierA".getBytes(), Bytes.toBytes("1111")),
104        new KeyValue(row, "familyB".getBytes(), "qualifierB".getBytes(), Bytes.toBytes("2222")),
105        new KeyValue(row, "familyC".getBytes(), "qualifierC".getBytes(), Bytes.toBytes("3333")));
106      when(result.listCells()).thenReturn(keyValues);
107      OutputCollector<ImmutableBytesWritable, Result> outputCollectorMock =
108        mock(OutputCollector.class);
109      gTableMap.map(null, result, outputCollectorMock, reporter);
110      verify(result).listCells();
111      verify(outputCollectorMock, times(1)).collect(any(), any());
112      verifyNoMoreInteractions(outputCollectorMock);
113    } finally {
114      if (gTableMap != null) gTableMap.close();
115    }
116  }
117
118  @Test
119  public void shouldCreateNewKey() throws Exception {
120    GroupingTableMap gTableMap = null;
121    try {
122      Result result = mock(Result.class);
123      Reporter reporter = mock(Reporter.class);
124      final byte[] bSeparator = Bytes.toBytes(" ");
125      gTableMap = new GroupingTableMap();
126      Configuration cfg = new Configuration();
127      cfg.set(GroupingTableMap.GROUP_COLUMNS, "familyA:qualifierA familyB:qualifierB");
128      JobConf jobConf = new JobConf(cfg);
129      gTableMap.configure(jobConf);
130
131      final byte[] firstPartKeyValue = Bytes.toBytes("34879512738945");
132      final byte[] secondPartKeyValue = Bytes.toBytes("35245142671437");
133      byte[] row = {};
134      List<Cell> cells = ImmutableList.<Cell> of(
135        new KeyValue(row, "familyA".getBytes(), "qualifierA".getBytes(), firstPartKeyValue),
136        new KeyValue(row, "familyB".getBytes(), "qualifierB".getBytes(), secondPartKeyValue));
137      when(result.listCells()).thenReturn(cells);
138
139      final AtomicBoolean outputCollected = new AtomicBoolean();
140      OutputCollector<ImmutableBytesWritable, Result> outputCollector =
141        new OutputCollector<ImmutableBytesWritable, Result>() {
142          @Override
143          public void collect(ImmutableBytesWritable arg, Result result) throws IOException {
144            assertArrayEquals(org.apache.hbase.thirdparty.com.google.common.primitives.Bytes
145              .concat(firstPartKeyValue, bSeparator, secondPartKeyValue), arg.copyBytes());
146            outputCollected.set(true);
147          }
148        };
149
150      gTableMap.map(null, result, outputCollector, reporter);
151      verify(result).listCells();
152      Assert.assertTrue("Output not received", outputCollected.get());
153
154      final byte[] firstPartValue = Bytes.toBytes("238947928");
155      final byte[] secondPartValue = Bytes.toBytes("4678456942345");
156      byte[][] data = { firstPartValue, secondPartValue };
157      ImmutableBytesWritable byteWritable = gTableMap.createGroupKey(data);
158      assertArrayEquals(org.apache.hbase.thirdparty.com.google.common.primitives.Bytes
159        .concat(firstPartValue, bSeparator, secondPartValue), byteWritable.get());
160    } finally {
161      if (gTableMap != null) gTableMap.close();
162    }
163  }
164
165  @Test
166  public void shouldReturnNullFromCreateGroupKey() throws Exception {
167    GroupingTableMap gTableMap = null;
168    try {
169      gTableMap = new GroupingTableMap();
170      assertNull(gTableMap.createGroupKey(null));
171    } finally {
172      if (gTableMap != null) gTableMap.close();
173    }
174  }
175}