View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.mapred;
20  
21  import java.io.IOException;
22  import java.util.ArrayList;
23  
24  import org.apache.hadoop.hbase.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.classification.InterfaceStability;
26  import org.apache.hadoop.hbase.Cell;
27  import org.apache.hadoop.hbase.CellUtil;
28  import org.apache.hadoop.hbase.KeyValue;
29  import org.apache.hadoop.hbase.client.Result;
30  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
31  import org.apache.hadoop.hbase.util.Bytes;
32  import org.apache.hadoop.mapred.JobConf;
33  import org.apache.hadoop.mapred.MapReduceBase;
34  import org.apache.hadoop.mapred.OutputCollector;
35  import org.apache.hadoop.mapred.Reporter;
36  
37  
38  /**
39   * Extract grouping columns from input record
40   */
41  @InterfaceAudience.Public
42  @InterfaceStability.Stable
43  public class GroupingTableMap
44  extends MapReduceBase
45  implements TableMap<ImmutableBytesWritable,Result> {
46  
47    /**
48     * JobConf parameter to specify the columns used to produce the key passed to
49     * collect from the map phase
50     */
51    public static final String GROUP_COLUMNS =
52      "hbase.mapred.groupingtablemap.columns";
53  
54    protected byte [][] columns;
55  
56    /**
57     * Use this before submitting a TableMap job. It will appropriately set up the
58     * JobConf.
59     *
60     * @param table table to be processed
61     * @param columns space separated list of columns to fetch
62     * @param groupColumns space separated list of columns used to form the key
63     * used in collect
64     * @param mapper map class
65     * @param job job configuration object
66     */
67    @SuppressWarnings("unchecked")
68    public static void initJob(String table, String columns, String groupColumns,
69      Class<? extends TableMap> mapper, JobConf job) {
70  
71      TableMapReduceUtil.initTableMapJob(table, columns, mapper,
72          ImmutableBytesWritable.class, Result.class, job);
73      job.set(GROUP_COLUMNS, groupColumns);
74    }
75  
76    @Override
77    public void configure(JobConf job) {
78      super.configure(job);
79      String[] cols = job.get(GROUP_COLUMNS, "").split(" ");
80      columns = new byte[cols.length][];
81      for(int i = 0; i < cols.length; i++) {
82        columns[i] = Bytes.toBytes(cols[i]);
83      }
84    }
85  
86    /**
87     * Extract the grouping columns from value to construct a new key.
88     *
89     * Pass the new key and value to reduce.
90     * If any of the grouping columns are not found in the value, the record is skipped.
91     * @param key
92     * @param value
93     * @param output
94     * @param reporter
95     * @throws IOException
96     */
97    @Override
98    public void map(ImmutableBytesWritable key, Result value,
99        OutputCollector<ImmutableBytesWritable,Result> output,
100       Reporter reporter) throws IOException {
101 
102     byte[][] keyVals = extractKeyValues(value);
103     if(keyVals != null) {
104       ImmutableBytesWritable tKey = createGroupKey(keyVals);
105       output.collect(tKey, value);
106     }
107   }
108 
109   /**
110    * Extract columns values from the current record. This method returns
111    * null if any of the columns are not found.
112    *
113    * Override this method if you want to deal with nulls differently.
114    *
115    * @param r
116    * @return array of byte values
117    */
118   protected byte[][] extractKeyValues(Result r) {
119     byte[][] keyVals = null;
120     ArrayList<byte[]> foundList = new ArrayList<byte[]>();
121     int numCols = columns.length;
122     if (numCols > 0) {
123       for (Cell value: r.listCells()) {
124         byte [] column = KeyValue.makeColumn(CellUtil.cloneFamily(value),
125             CellUtil.cloneQualifier(value));
126         for (int i = 0; i < numCols; i++) {
127           if (Bytes.equals(column, columns[i])) {
128             foundList.add(CellUtil.cloneValue(value));
129             break;
130           }
131         }
132       }
133       if(foundList.size() == numCols) {
134         keyVals = foundList.toArray(new byte[numCols][]);
135       }
136     }
137     return keyVals;
138   }
139 
140   /**
141    * Create a key by concatenating multiple column values.
142    * Override this function in order to produce different types of keys.
143    *
144    * @param vals
145    * @return key generated by concatenating multiple column values
146    */
147   protected ImmutableBytesWritable createGroupKey(byte[][] vals) {
148     if(vals == null) {
149       return null;
150     }
151     StringBuilder sb =  new StringBuilder();
152     for(int i = 0; i < vals.length; i++) {
153       if(i > 0) {
154         sb.append(" ");
155       }
156       sb.append(Bytes.toString(vals[i]));
157     }
158     return new ImmutableBytesWritable(Bytes.toBytesBinary(sb.toString()));
159   }
160 }