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() 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 verifyZeroInteractions(outputCollectorMock); 083 } finally { 084 if (gTableMap != null) gTableMap.close(); 085 } 086 } 087 088 @Test 089 @SuppressWarnings({ "deprecation", "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 @SuppressWarnings({ "deprecation" }) 120 public void shouldCreateNewKey() throws Exception { 121 GroupingTableMap gTableMap = null; 122 try { 123 Result result = mock(Result.class); 124 Reporter reporter = mock(Reporter.class); 125 final byte[] bSeparator = Bytes.toBytes(" "); 126 gTableMap = new GroupingTableMap(); 127 Configuration cfg = new Configuration(); 128 cfg.set(GroupingTableMap.GROUP_COLUMNS, "familyA:qualifierA familyB:qualifierB"); 129 JobConf jobConf = new JobConf(cfg); 130 gTableMap.configure(jobConf); 131 132 final byte[] firstPartKeyValue = Bytes.toBytes("34879512738945"); 133 final byte[] secondPartKeyValue = Bytes.toBytes("35245142671437"); 134 byte[] row = {}; 135 List<Cell> cells = ImmutableList.<Cell> of( 136 new KeyValue(row, "familyA".getBytes(), "qualifierA".getBytes(), firstPartKeyValue), 137 new KeyValue(row, "familyB".getBytes(), "qualifierB".getBytes(), secondPartKeyValue)); 138 when(result.listCells()).thenReturn(cells); 139 140 final AtomicBoolean outputCollected = new AtomicBoolean(); 141 OutputCollector<ImmutableBytesWritable, Result> outputCollector = 142 new OutputCollector<ImmutableBytesWritable, Result>() { 143 @Override 144 public void collect(ImmutableBytesWritable arg, Result result) throws IOException { 145 assertArrayEquals(org.apache.hbase.thirdparty.com.google.common.primitives.Bytes 146 .concat(firstPartKeyValue, bSeparator, secondPartKeyValue), arg.copyBytes()); 147 outputCollected.set(true); 148 } 149 }; 150 151 gTableMap.map(null, result, outputCollector, reporter); 152 verify(result).listCells(); 153 Assert.assertTrue("Output not received", outputCollected.get()); 154 155 final byte[] firstPartValue = Bytes.toBytes("238947928"); 156 final byte[] secondPartValue = Bytes.toBytes("4678456942345"); 157 byte[][] data = { firstPartValue, secondPartValue }; 158 ImmutableBytesWritable byteWritable = gTableMap.createGroupKey(data); 159 assertArrayEquals(org.apache.hbase.thirdparty.com.google.common.primitives.Bytes 160 .concat(firstPartValue, bSeparator, secondPartValue), byteWritable.get()); 161 } finally { 162 if (gTableMap != null) 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) gTableMap.close(); 175 } 176 } 177}