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