1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.mapreduce;
20
21 import java.io.IOException;
22 import java.util.ArrayList;
23
24 import org.apache.hadoop.classification.InterfaceAudience;
25 import org.apache.hadoop.classification.InterfaceStability;
26 import org.apache.hadoop.conf.Configurable;
27 import org.apache.hadoop.conf.Configuration;
28 import org.apache.hadoop.hbase.KeyValue;
29 import org.apache.hadoop.hbase.client.Result;
30 import org.apache.hadoop.hbase.client.Scan;
31 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
32 import org.apache.hadoop.hbase.util.Bytes;
33 import org.apache.hadoop.mapreduce.Job;
34
35
36
37
38 @InterfaceAudience.Public
39 @InterfaceStability.Stable
40 public class GroupingTableMapper
41 extends TableMapper<ImmutableBytesWritable,Result> implements Configurable {
42
43
44
45
46
47 public static final String GROUP_COLUMNS =
48 "hbase.mapred.groupingtablemap.columns";
49
50
51 protected byte [][] columns;
52
53 private Configuration conf = null;
54
55
56
57
58
59
60
61
62
63
64
65
66
67 @SuppressWarnings("unchecked")
68 public static void initJob(String table, Scan scan, String groupColumns,
69 Class<? extends TableMapper> mapper, Job job) throws IOException {
70 TableMapReduceUtil.initTableMapperJob(table, scan, mapper,
71 ImmutableBytesWritable.class, Result.class, job);
72 job.getConfiguration().set(GROUP_COLUMNS, groupColumns);
73 }
74
75
76
77
78
79
80
81
82
83
84
85
86 @Override
87 public void map(ImmutableBytesWritable key, Result value, Context context)
88 throws IOException, InterruptedException {
89 byte[][] keyVals = extractKeyValues(value);
90 if(keyVals != null) {
91 ImmutableBytesWritable tKey = createGroupKey(keyVals);
92 context.write(tKey, value);
93 }
94 }
95
96
97
98
99
100
101
102
103
104
105 protected byte[][] extractKeyValues(Result r) {
106 byte[][] keyVals = null;
107 ArrayList<byte[]> foundList = new ArrayList<byte[]>();
108 int numCols = columns.length;
109 if (numCols > 0) {
110 for (KeyValue value: r.list()) {
111 byte [] column = KeyValue.makeColumn(value.getFamily(),
112 value.getQualifier());
113 for (int i = 0; i < numCols; i++) {
114 if (Bytes.equals(column, columns[i])) {
115 foundList.add(value.getValue());
116 break;
117 }
118 }
119 }
120 if(foundList.size() == numCols) {
121 keyVals = foundList.toArray(new byte[numCols][]);
122 }
123 }
124 return keyVals;
125 }
126
127
128
129
130
131
132
133
134
135 protected ImmutableBytesWritable createGroupKey(byte[][] vals) {
136 if(vals == null) {
137 return null;
138 }
139 StringBuilder sb = new StringBuilder();
140 for(int i = 0; i < vals.length; i++) {
141 if(i > 0) {
142 sb.append(" ");
143 }
144 sb.append(Bytes.toString(vals[i]));
145 }
146 return new ImmutableBytesWritable(Bytes.toBytes(sb.toString()));
147 }
148
149
150
151
152
153
154
155 @Override
156 public Configuration getConf() {
157 return conf;
158 }
159
160
161
162
163
164
165
166
167 @Override
168 public void setConf(Configuration configuration) {
169 this.conf = configuration;
170 String[] cols = conf.get(GROUP_COLUMNS, "").split(" ");
171 columns = new byte[cols.length][];
172 for(int i = 0; i < cols.length; i++) {
173 columns[i] = Bytes.toBytes(cols[i]);
174 }
175 }
176
177 }