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 java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.Map;
24
25 import org.apache.hadoop.hbase.KeyValue;
26 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
27 import org.apache.hadoop.hbase.client.Result;
28 import org.apache.hadoop.hbase.util.Bytes;
29 import org.apache.hadoop.mapred.JobConf;
30 import org.apache.hadoop.mapred.MapReduceBase;
31 import org.apache.hadoop.mapred.OutputCollector;
32 import org.apache.hadoop.mapred.Reporter;
33
34
35
36
37
38 @Deprecated
39 public class GroupingTableMap
40 extends MapReduceBase
41 implements TableMap<ImmutableBytesWritable,Result> {
42
43
44
45
46
47 public static final String GROUP_COLUMNS =
48 "hbase.mapred.groupingtablemap.columns";
49
50 protected byte [][] columns;
51
52
53
54
55
56
57
58
59
60
61
62
63 @SuppressWarnings("unchecked")
64 public static void initJob(String table, String columns, String groupColumns,
65 Class<? extends TableMap> mapper, JobConf job) {
66
67 TableMapReduceUtil.initTableMapJob(table, columns, mapper,
68 ImmutableBytesWritable.class, Result.class, job);
69 job.set(GROUP_COLUMNS, groupColumns);
70 }
71
72 @Override
73 public void configure(JobConf job) {
74 super.configure(job);
75 String[] cols = job.get(GROUP_COLUMNS, "").split(" ");
76 columns = new byte[cols.length][];
77 for(int i = 0; i < cols.length; i++) {
78 columns[i] = Bytes.toBytes(cols[i]);
79 }
80 }
81
82
83
84
85
86
87
88
89
90
91
92
93 public void map(ImmutableBytesWritable key, Result value,
94 OutputCollector<ImmutableBytesWritable,Result> output,
95 Reporter reporter) throws IOException {
96
97 byte[][] keyVals = extractKeyValues(value);
98 if(keyVals != null) {
99 ImmutableBytesWritable tKey = createGroupKey(keyVals);
100 output.collect(tKey, value);
101 }
102 }
103
104
105
106
107
108
109
110
111
112
113 protected byte[][] extractKeyValues(Result r) {
114 byte[][] keyVals = null;
115 ArrayList<byte[]> foundList = new ArrayList<byte[]>();
116 int numCols = columns.length;
117 if (numCols > 0) {
118 for (KeyValue value: r.list()) {
119 byte [] column = KeyValue.makeColumn(value.getFamily(),
120 value.getQualifier());
121 for (int i = 0; i < numCols; i++) {
122 if (Bytes.equals(column, columns[i])) {
123 foundList.add(value.getValue());
124 break;
125 }
126 }
127 }
128 if(foundList.size() == numCols) {
129 keyVals = foundList.toArray(new byte[numCols][]);
130 }
131 }
132 return keyVals;
133 }
134
135
136
137
138
139
140
141
142 protected ImmutableBytesWritable createGroupKey(byte[][] vals) {
143 if(vals == null) {
144 return null;
145 }
146 StringBuilder sb = new StringBuilder();
147 for(int i = 0; i < vals.length; i++) {
148 if(i > 0) {
149 sb.append(" ");
150 }
151 sb.append(Bytes.toString(vals[i]));
152 }
153 return new ImmutableBytesWritable(Bytes.toBytes(sb.toString()));
154 }
155 }