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  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   * Extract grouping columns from input record
37   */
38  @Deprecated
39  public class GroupingTableMap
40  extends MapReduceBase
41  implements TableMap<ImmutableBytesWritable,Result> {
42  
43    /**
44     * JobConf parameter to specify the columns used to produce the key passed to
45     * collect from the map phase
46     */
47    public static final String GROUP_COLUMNS =
48      "hbase.mapred.groupingtablemap.columns";
49  
50    protected byte [][] columns;
51  
52    /**
53     * Use this before submitting a TableMap job. It will appropriately set up the
54     * JobConf.
55     *
56     * @param table table to be processed
57     * @param columns space separated list of columns to fetch
58     * @param groupColumns space separated list of columns used to form the key
59     * used in collect
60     * @param mapper map class
61     * @param job job configuration object
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     * Extract the grouping columns from value to construct a new key.
84     *
85     * Pass the new key and value to reduce.
86     * If any of the grouping columns are not found in the value, the record is skipped.
87     * @param key
88     * @param value
89     * @param output
90     * @param reporter
91     * @throws IOException
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    * Extract columns values from the current record. This method returns
106    * null if any of the columns are not found.
107    *
108    * Override this method if you want to deal with nulls differently.
109    *
110    * @param r
111    * @return array of byte values
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    * Create a key by concatenating multiple column values.
137    * Override this function in order to produce different types of keys.
138    *
139    * @param vals
140    * @return key generated by concatenating multiple column values
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 }