View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional information regarding
4    * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0
5    * (the "License"); you may not use this file except in compliance with the License. You may
6    * obtain a copy of the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software distributed under the
11   * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
12   * either express or implied. See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package org.apache.hadoop.hbase.mapreduce;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.apache.hadoop.conf.Configuration;
22  import org.apache.hadoop.hbase.Cell;
23  import org.apache.hadoop.hbase.KeyValue;
24  import org.apache.hadoop.hbase.SmallTests;
25  import org.apache.hadoop.hbase.client.Result;
26  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
27  import org.apache.hadoop.hbase.util.Bytes;
28  import org.apache.hadoop.mapreduce.Mapper;
29  import org.junit.Test;
30  import org.junit.experimental.categories.Category;
31  
32  import static org.mockito.Mockito.*;
33  
34  @Category(SmallTests.class)
35  public class TestGroupingTableMapper {
36  
37    /**
38     * Test GroupingTableMapper class
39     */
40    @Test
41    public void testGroupingTableMapper() throws Exception {
42  
43      GroupingTableMapper mapper = new GroupingTableMapper();
44      Configuration configuration = new Configuration();
45      configuration.set(GroupingTableMapper.GROUP_COLUMNS, "family1:clm family2:clm");
46      mapper.setConf(configuration);
47  
48      Result result = mock(Result.class);
49      @SuppressWarnings("unchecked")
50      Mapper<ImmutableBytesWritable, Result, ImmutableBytesWritable, Result>.Context context =
51          mock(Mapper.Context.class);
52      context.write(any(ImmutableBytesWritable.class), any(Result.class));
53      List<Cell> keyValue = new ArrayList<Cell>();
54      byte[] row = {};
55      keyValue.add(new KeyValue(row, Bytes.toBytes("family2"), Bytes.toBytes("clm"), Bytes
56          .toBytes("value1")));
57      keyValue.add(new KeyValue(row, Bytes.toBytes("family1"), Bytes.toBytes("clm"), Bytes
58          .toBytes("value2")));
59      when(result.listCells()).thenReturn(keyValue);
60      mapper.map(null, result, context);
61      // template data
62      byte[][] data = { Bytes.toBytes("value1"), Bytes.toBytes("value2") };
63      ImmutableBytesWritable ibw = mapper.createGroupKey(data);
64      verify(context).write(ibw, result);
65    }
66  
67  }