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