1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.testclassification.MapReduceTests;
39 import org.apache.hadoop.hbase.testclassification.SmallTests;
40 import org.apache.hadoop.hbase.client.Result;
41 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
42 import org.apache.hadoop.hbase.util.Bytes;
43 import org.apache.hadoop.mapred.JobConf;
44 import org.apache.hadoop.mapred.OutputCollector;
45 import org.apache.hadoop.mapred.Reporter;
46 import org.junit.Assert;
47 import org.junit.Test;
48 import org.junit.experimental.categories.Category;
49
50 import com.google.common.collect.ImmutableList;
51
52 @Category({MapReduceTests.class, SmallTests.class})
53 public class TestGroupingTableMap {
54
55 @Test
56 @SuppressWarnings({ "deprecation", "unchecked" })
57 public void shouldNotCallCollectonSinceFindUniqueKeyValueMoreThanOnes()
58 throws Exception {
59 GroupingTableMap gTableMap = null;
60 try {
61 Result result = mock(Result.class);
62 Reporter reporter = mock(Reporter.class);
63 gTableMap = new GroupingTableMap();
64 Configuration cfg = new Configuration();
65 cfg.set(GroupingTableMap.GROUP_COLUMNS, "familyA:qualifierA familyB:qualifierB");
66 JobConf jobConf = new JobConf(cfg);
67 gTableMap.configure(jobConf);
68
69 byte[] row = {};
70 List<Cell> keyValues = ImmutableList.<Cell>of(
71 new KeyValue(row, "familyA".getBytes(), "qualifierA".getBytes(), Bytes.toBytes("1111")),
72 new KeyValue(row, "familyA".getBytes(), "qualifierA".getBytes(), Bytes.toBytes("2222")),
73 new KeyValue(row, "familyB".getBytes(), "qualifierB".getBytes(), Bytes.toBytes("3333")));
74 when(result.listCells()).thenReturn(keyValues);
75 OutputCollector<ImmutableBytesWritable, Result> outputCollectorMock =
76 mock(OutputCollector.class);
77 gTableMap.map(null, result, outputCollectorMock, reporter);
78 verify(result).listCells();
79 verifyZeroInteractions(outputCollectorMock);
80 } finally {
81 if (gTableMap != null)
82 gTableMap.close();
83 }
84 }
85
86 @Test
87 @SuppressWarnings({ "deprecation", "unchecked" })
88 public void shouldCreateNewKeyAlthoughExtraKey() throws Exception {
89 GroupingTableMap gTableMap = null;
90 try {
91 Result result = mock(Result.class);
92 Reporter reporter = mock(Reporter.class);
93 gTableMap = new GroupingTableMap();
94 Configuration cfg = new Configuration();
95 cfg.set(GroupingTableMap.GROUP_COLUMNS, "familyA:qualifierA familyB:qualifierB");
96 JobConf jobConf = new JobConf(cfg);
97 gTableMap.configure(jobConf);
98
99 byte[] row = {};
100 List<Cell> keyValues = ImmutableList.<Cell>of(
101 new KeyValue(row, "familyA".getBytes(), "qualifierA".getBytes(), Bytes.toBytes("1111")),
102 new KeyValue(row, "familyB".getBytes(), "qualifierB".getBytes(), Bytes.toBytes("2222")),
103 new KeyValue(row, "familyC".getBytes(), "qualifierC".getBytes(), Bytes.toBytes("3333")));
104 when(result.listCells()).thenReturn(keyValues);
105 OutputCollector<ImmutableBytesWritable, Result> outputCollectorMock =
106 mock(OutputCollector.class);
107 gTableMap.map(null, result, outputCollectorMock, reporter);
108 verify(result).listCells();
109 verify(outputCollectorMock, times(1))
110 .collect(any(ImmutableBytesWritable.class), any(Result.class));
111 verifyNoMoreInteractions(outputCollectorMock);
112 } finally {
113 if (gTableMap != null)
114 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(com.google.common.primitives.Bytes.concat(firstPartKeyValue, bSeparator,
146 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(com.google.common.primitives.Bytes.concat(firstPartValue,
160 bSeparator, secondPartValue), byteWritable.get());
161 } finally {
162 if (gTableMap != null)
163 gTableMap.close();
164 }
165 }
166
167 @Test
168 @SuppressWarnings({ "deprecation" })
169 public void shouldReturnNullFromCreateGroupKey() throws Exception {
170 GroupingTableMap gTableMap = null;
171 try {
172 gTableMap = new GroupingTableMap();
173 assertNull(gTableMap.createGroupKey(null));
174 } finally {
175 if(gTableMap != null)
176 gTableMap.close();
177 }
178 }
179 }