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.Matchers.any;
023import static org.mockito.Mockito.mock;
024import static org.mockito.Mockito.times;
025import static org.mockito.Mockito.verify;
026import static org.mockito.Mockito.verifyNoMoreInteractions;
027import static org.mockito.Mockito.verifyZeroInteractions;
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({ "deprecation", "unchecked" })
061  public void shouldNotCallCollectonSinceFindUniqueKeyValueMoreThanOnes()
062      throws Exception {
063    GroupingTableMap gTableMap = null;
064    try {
065      Result result = mock(Result.class);
066      Reporter reporter = mock(Reporter.class);
067      gTableMap = new GroupingTableMap();
068      Configuration cfg = new Configuration();
069      cfg.set(GroupingTableMap.GROUP_COLUMNS, "familyA:qualifierA familyB:qualifierB");
070      JobConf jobConf = new JobConf(cfg);
071      gTableMap.configure(jobConf);
072
073      byte[] row = {};
074      List<Cell> keyValues = ImmutableList.<Cell>of(
075          new KeyValue(row, Bytes.toBytes("familyA"), Bytes.toBytes("qualifierA"),
076              Bytes.toBytes("1111")),
077          new KeyValue(row, Bytes.toBytes("familyA"), Bytes.toBytes("qualifierA"),
078              Bytes.toBytes("2222")),
079          new KeyValue(row, Bytes.toBytes("familyB"), Bytes.toBytes("qualifierB"),
080              Bytes.toBytes("3333")));
081      when(result.listCells()).thenReturn(keyValues);
082      OutputCollector<ImmutableBytesWritable, Result> outputCollectorMock =
083          mock(OutputCollector.class);
084      gTableMap.map(null, result, outputCollectorMock, reporter);
085      verify(result).listCells();
086      verifyZeroInteractions(outputCollectorMock);
087    } finally {
088      if (gTableMap != null)
089        gTableMap.close();
090    }
091  }
092
093  @Test
094  @SuppressWarnings({ "deprecation", "unchecked" })
095  public void shouldCreateNewKeyAlthoughExtraKey() throws Exception {
096    GroupingTableMap gTableMap = null;
097    try {
098      Result result = mock(Result.class);
099      Reporter reporter = mock(Reporter.class);
100      gTableMap = new GroupingTableMap();
101      Configuration cfg = new Configuration();
102      cfg.set(GroupingTableMap.GROUP_COLUMNS, "familyA:qualifierA familyB:qualifierB");
103      JobConf jobConf = new JobConf(cfg);
104      gTableMap.configure(jobConf);
105
106      byte[] row = {};
107      List<Cell> keyValues = ImmutableList.<Cell>of(
108          new KeyValue(row, Bytes.toBytes("familyA"), Bytes.toBytes("qualifierA"),
109              Bytes.toBytes("1111")),
110          new KeyValue(row, Bytes.toBytes("familyB"), Bytes.toBytes("qualifierB"),
111              Bytes.toBytes("2222")),
112          new KeyValue(row, Bytes.toBytes("familyC"), Bytes.toBytes("qualifierC"),
113              Bytes.toBytes("3333")));
114      when(result.listCells()).thenReturn(keyValues);
115      OutputCollector<ImmutableBytesWritable, Result> outputCollectorMock =
116          mock(OutputCollector.class);
117      gTableMap.map(null, result, outputCollectorMock, reporter);
118      verify(result).listCells();
119      verify(outputCollectorMock, times(1))
120        .collect(any(), any());
121      verifyNoMoreInteractions(outputCollectorMock);
122    } finally {
123      if (gTableMap != null)
124        gTableMap.close();
125    }
126  }
127
128  @Test
129  @SuppressWarnings({ "deprecation" })
130  public void shouldCreateNewKey() throws Exception {
131    GroupingTableMap gTableMap = null;
132    try {
133      Result result = mock(Result.class);
134      Reporter reporter = mock(Reporter.class);
135      final byte[] bSeparator = Bytes.toBytes(" ");
136      gTableMap = new GroupingTableMap();
137      Configuration cfg = new Configuration();
138      cfg.set(GroupingTableMap.GROUP_COLUMNS, "familyA:qualifierA familyB:qualifierB");
139      JobConf jobConf = new JobConf(cfg);
140      gTableMap.configure(jobConf);
141
142      final byte[] firstPartKeyValue = Bytes.toBytes("34879512738945");
143      final byte[] secondPartKeyValue = Bytes.toBytes("35245142671437");
144      byte[] row = {};
145      List<Cell> cells = ImmutableList.<Cell>of(
146          new KeyValue(row, Bytes.toBytes("familyA"), Bytes.toBytes("qualifierA"),
147              firstPartKeyValue),
148          new KeyValue(row, Bytes.toBytes("familyB"), Bytes.toBytes("qualifierB"),
149              secondPartKeyValue));
150      when(result.listCells()).thenReturn(cells);
151
152      final AtomicBoolean outputCollected = new AtomicBoolean();
153      OutputCollector<ImmutableBytesWritable, Result> outputCollector =
154          new OutputCollector<ImmutableBytesWritable, Result>() {
155        @Override
156        public void collect(ImmutableBytesWritable arg, Result result) throws IOException {
157          assertArrayEquals(org.apache.hbase.thirdparty.com.google.common.primitives.
158            Bytes.concat(firstPartKeyValue, bSeparator,
159              secondPartKeyValue), arg.copyBytes());
160          outputCollected.set(true);
161        }
162      };
163
164      gTableMap.map(null, result, outputCollector, reporter);
165      verify(result).listCells();
166      Assert.assertTrue("Output not received", outputCollected.get());
167
168      final byte[] firstPartValue = Bytes.toBytes("238947928");
169      final byte[] secondPartValue = Bytes.toBytes("4678456942345");
170      byte[][] data = { firstPartValue, secondPartValue };
171      ImmutableBytesWritable byteWritable = gTableMap.createGroupKey(data);
172      assertArrayEquals(org.apache.hbase.thirdparty.com.google.common.primitives.
173        Bytes.concat(firstPartValue,
174          bSeparator, secondPartValue), byteWritable.get());
175    } finally {
176      if (gTableMap != null)
177        gTableMap.close();
178    }
179  }
180
181  @Test
182  @SuppressWarnings({ "deprecation" })
183  public void shouldReturnNullFromCreateGroupKey() throws Exception {
184    GroupingTableMap gTableMap = null;
185    try {
186      gTableMap = new GroupingTableMap();
187      assertNull(gTableMap.createGroupKey(null));
188    } finally {
189      if(gTableMap != null)
190        gTableMap.close();
191    }
192  }
193}