View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.mapred;
20  
21  import static org.junit.Assert.assertArrayEquals;
22  import static org.junit.Assert.assertNull;
23  import static org.mockito.Matchers.any;
24  import static org.mockito.Mockito.mock;
25  import static org.mockito.Mockito.times;
26  import static org.mockito.Mockito.verify;
27  import static org.mockito.Mockito.verifyZeroInteractions;
28  import static org.mockito.Mockito.verifyNoMoreInteractions;
29  import static org.mockito.Mockito.when;
30  
31  import java.io.IOException;
32  import java.util.List;
33  import java.util.concurrent.atomic.AtomicBoolean;
34  
35  import org.apache.hadoop.conf.Configuration;
36  import org.apache.hadoop.hbase.Cell;
37  import org.apache.hadoop.hbase.KeyValue;
38  import org.apache.hadoop.hbase.client.Result;
39  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
40  import org.apache.hadoop.hbase.testclassification.SmallTests;
41  import org.apache.hadoop.hbase.util.Bytes;
42  import org.apache.hadoop.mapred.JobConf;
43  import org.apache.hadoop.mapred.OutputCollector;
44  import org.apache.hadoop.mapred.Reporter;
45  import org.junit.Assert;
46  import org.junit.Test;
47  import org.junit.experimental.categories.Category;
48  
49  import com.google.common.collect.ImmutableList;
50  
51  @Category(SmallTests.class)
52  public class TestGroupingTableMap {
53  
54    @Test
55    @SuppressWarnings({ "deprecation", "unchecked" })
56    public void shouldNotCallCollectonSinceFindUniqueKeyValueMoreThanOnes()
57        throws Exception {
58      GroupingTableMap gTableMap = null;
59      try {
60        Result result = mock(Result.class);
61        Reporter reporter = mock(Reporter.class);
62        gTableMap = new GroupingTableMap();
63        Configuration cfg = new Configuration();
64        cfg.set(GroupingTableMap.GROUP_COLUMNS, "familyA:qualifierA familyB:qualifierB");
65        JobConf jobConf = new JobConf(cfg);
66        gTableMap.configure(jobConf);
67    
68        byte[] row = {};
69        List<Cell> keyValues = ImmutableList.<Cell>of(
70            new KeyValue(row, "familyA".getBytes(), "qualifierA".getBytes(), Bytes.toBytes("1111")),
71            new KeyValue(row, "familyA".getBytes(), "qualifierA".getBytes(), Bytes.toBytes("2222")),
72            new KeyValue(row, "familyB".getBytes(), "qualifierB".getBytes(), Bytes.toBytes("3333")));
73        when(result.listCells()).thenReturn(keyValues);
74        OutputCollector<ImmutableBytesWritable, Result> outputCollectorMock =
75            mock(OutputCollector.class);
76        gTableMap.map(null, result, outputCollectorMock, reporter);
77        verify(result).listCells();
78        verifyZeroInteractions(outputCollectorMock);
79      } finally {
80        if (gTableMap != null)
81          gTableMap.close();    
82      }
83    }
84  
85    @Test
86    @SuppressWarnings({ "deprecation", "unchecked" })
87    public void shouldCreateNewKeyAlthoughExtraKey() throws Exception {
88      GroupingTableMap gTableMap = null;
89      try {
90        Result result = mock(Result.class);
91        Reporter reporter = mock(Reporter.class);
92        gTableMap = new GroupingTableMap();
93        Configuration cfg = new Configuration();
94        cfg.set(GroupingTableMap.GROUP_COLUMNS, "familyA:qualifierA familyB:qualifierB");
95        JobConf jobConf = new JobConf(cfg);
96        gTableMap.configure(jobConf);
97    
98        byte[] row = {};
99        List<Cell> keyValues = ImmutableList.<Cell>of(
100           new KeyValue(row, "familyA".getBytes(), "qualifierA".getBytes(), Bytes.toBytes("1111")),
101           new KeyValue(row, "familyB".getBytes(), "qualifierB".getBytes(), Bytes.toBytes("2222")),
102           new KeyValue(row, "familyC".getBytes(), "qualifierC".getBytes(), Bytes.toBytes("3333")));
103       when(result.listCells()).thenReturn(keyValues);
104       OutputCollector<ImmutableBytesWritable, Result> outputCollectorMock =
105           mock(OutputCollector.class);
106       gTableMap.map(null, result, outputCollectorMock, reporter);
107       verify(result).listCells();
108       verify(outputCollectorMock, times(1))
109         .collect(any(ImmutableBytesWritable.class), any(Result.class));
110       verifyNoMoreInteractions(outputCollectorMock);
111     } finally {
112       if (gTableMap != null)
113         gTableMap.close();
114     }
115   }
116 
117   @Test
118   @SuppressWarnings({ "deprecation" })
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(com.google.common.primitives.Bytes.concat(firstPartKeyValue, bSeparator,
145               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(com.google.common.primitives.Bytes.concat(firstPartValue,
159           bSeparator, secondPartValue), byteWritable.get());
160     } finally {
161       if (gTableMap != null)
162         gTableMap.close();
163     }
164   }
165 
166   @Test
167   @SuppressWarnings({ "deprecation" })
168   public void shouldReturnNullFromCreateGroupKey() throws Exception {
169     GroupingTableMap gTableMap = null;
170     try {
171       gTableMap = new GroupingTableMap();
172       assertNull(gTableMap.createGroupKey(null));
173     } finally {
174       if(gTableMap != null)
175         gTableMap.close();
176     }
177   }
178 }