001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.mapreduce;
020
021import java.io.IOException;
022
023import org.apache.hadoop.hbase.CompareOperator;
024import org.apache.hadoop.hbase.HConstants;
025import org.apache.yetus.audience.InterfaceAudience;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028import org.apache.hadoop.conf.Configuration;
029import org.apache.hadoop.conf.Configured;
030import org.apache.hadoop.fs.Path;
031import org.apache.hadoop.hbase.Cell;
032import org.apache.hadoop.hbase.CellUtil;
033import org.apache.hadoop.hbase.HBaseConfiguration;
034import org.apache.hadoop.hbase.client.Result;
035import org.apache.hadoop.hbase.client.Scan;
036import org.apache.hadoop.hbase.filter.Filter;
037import org.apache.hadoop.hbase.filter.PrefixFilter;
038import org.apache.hadoop.hbase.filter.RegexStringComparator;
039import org.apache.hadoop.hbase.filter.RowFilter;
040import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
041import org.apache.hadoop.hbase.util.Bytes;
042import org.apache.hadoop.io.IntWritable;
043import org.apache.hadoop.io.Text;
044import org.apache.hadoop.mapreduce.Job;
045import org.apache.hadoop.mapreduce.Reducer;
046import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
047import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
048import org.apache.hadoop.util.Tool;
049import org.apache.hadoop.util.ToolRunner;
050
051import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
052
053
054/**
055 * A job with a a map and reduce phase to count cells in a table.
056 * The counter lists the following stats for a given table:
057 * <pre>
058 * 1. Total number of rows in the table
059 * 2. Total number of CFs across all rows
060 * 3. Total qualifiers across all rows
061 * 4. Total occurrence of each CF
062 * 5. Total occurrence  of each qualifier
063 * 6. Total number of versions of each qualifier.
064 * </pre>
065 *
066 * The cellcounter can take optional parameters to use a user
067 * supplied row/family/qualifier string to use in the report and
068 * second a regex based or prefix based row filter to restrict the
069 * count operation to a limited subset of rows from the table or a
070 * start time and/or end time to limit the count to a time range.
071 */
072@InterfaceAudience.Public
073public class CellCounter extends Configured implements Tool {
074  private static final Logger LOG =
075    LoggerFactory.getLogger(CellCounter.class.getName());
076
077
078  /**
079   * Name of this 'program'.
080   */
081  static final String NAME = "CellCounter";
082
083  private final static String JOB_NAME_CONF_KEY = "mapreduce.job.name";
084
085  /**
086   * Mapper that runs the count.
087   */
088  static class CellCounterMapper
089  extends TableMapper<Text, IntWritable> {
090    /**
091     * Counter enumeration to count the actual rows.
092     */
093    public static enum Counters {
094      ROWS,
095      CELLS
096    }
097
098    private Configuration conf;
099    private String separator;
100
101    // state of current row, family, column needs to persist across map() invocations
102    // in order to properly handle scanner batching, where a single qualifier may have too
103    // many versions for a single map() call
104    private byte[] lastRow;
105    private String currentRowKey;
106    byte[] currentFamily = null;
107    String currentFamilyName = null;
108    byte[] currentQualifier = null;
109    // family + qualifier
110    String currentQualifierName = null;
111    // rowkey + family + qualifier
112    String currentRowQualifierName = null;
113
114    @Override
115    protected void setup(Context context) throws IOException, InterruptedException {
116      conf = context.getConfiguration();
117      separator = conf.get("ReportSeparator",":");
118    }
119
120    /**
121     * Maps the data.
122     *
123     * @param row     The current table row key.
124     * @param values  The columns.
125     * @param context The current context.
126     * @throws IOException When something is broken with the data.
127     */
128
129    @Override
130    @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="NP_NULL_ON_SOME_PATH",
131      justification="Findbugs is blind to the Precondition null check")
132    public void map(ImmutableBytesWritable row, Result values,
133                    Context context)
134        throws IOException {
135      Preconditions.checkState(values != null,
136          "values passed to the map is null");
137
138      try {
139        byte[] currentRow = values.getRow();
140        if (lastRow == null || !Bytes.equals(lastRow, currentRow)) {
141          lastRow = currentRow;
142          currentRowKey = Bytes.toStringBinary(currentRow);
143          currentFamily = null;
144          currentQualifier = null;
145          context.getCounter(Counters.ROWS).increment(1);
146          context.write(new Text("Total ROWS"), new IntWritable(1));
147        }
148        if (!values.isEmpty()) {
149          int cellCount = 0;
150          for (Cell value : values.listCells()) {
151            cellCount++;
152            if (currentFamily == null || !CellUtil.matchingFamily(value, currentFamily)) {
153              currentFamily = CellUtil.cloneFamily(value);
154              currentFamilyName = Bytes.toStringBinary(currentFamily);
155              currentQualifier = null;
156              context.getCounter("CF", currentFamilyName).increment(1);
157              if (1 == context.getCounter("CF", currentFamilyName).getValue()) {
158                context.write(new Text("Total Families Across all Rows"), new IntWritable(1));
159                context.write(new Text(currentFamily), new IntWritable(1));
160              }
161            }
162            if (currentQualifier == null || !CellUtil.matchingQualifier(value, currentQualifier)) {
163              currentQualifier = CellUtil.cloneQualifier(value);
164              currentQualifierName = currentFamilyName + separator +
165                  Bytes.toStringBinary(currentQualifier);
166              currentRowQualifierName = currentRowKey + separator + currentQualifierName;
167
168              context.write(new Text("Total Qualifiers across all Rows"),
169                  new IntWritable(1));
170              context.write(new Text(currentQualifierName), new IntWritable(1));
171            }
172            // Increment versions
173            context.write(new Text(currentRowQualifierName + "_Versions"), new IntWritable(1));
174          }
175          context.getCounter(Counters.CELLS).increment(cellCount);
176        }
177      } catch (InterruptedException e) {
178        LOG.error("Interrupted while writing cellCount", e);
179        Thread.currentThread().interrupt();
180      }
181    }
182  }
183
184  static class IntSumReducer<Key> extends Reducer<Key, IntWritable,
185      Key, IntWritable> {
186
187    private IntWritable result = new IntWritable();
188    public void reduce(Key key, Iterable<IntWritable> values,
189      Context context)
190    throws IOException, InterruptedException {
191      int sum = 0;
192      for (IntWritable val : values) {
193        sum += val.get();
194      }
195      result.set(sum);
196      context.write(key, result);
197    }
198  }
199
200  /**
201   * Sets up the actual job.
202   *
203   * @param conf The current configuration.
204   * @param args The command line parameters.
205   * @return The newly created job.
206   * @throws IOException When setting up the job fails.
207   */
208  public static Job createSubmittableJob(Configuration conf, String[] args)
209      throws IOException {
210    String tableName = args[0];
211    Path outputDir = new Path(args[1]);
212    String reportSeparatorString = (args.length > 2) ? args[2]: ":";
213    conf.set("ReportSeparator", reportSeparatorString);
214    Job job = Job.getInstance(conf, conf.get(JOB_NAME_CONF_KEY, NAME + "_" + tableName));
215    job.setJarByClass(CellCounter.class);
216    Scan scan = getConfiguredScanForJob(conf, args);
217    TableMapReduceUtil.initTableMapperJob(tableName, scan,
218        CellCounterMapper.class, ImmutableBytesWritable.class, Result.class, job);
219    job.setNumReduceTasks(1);
220    job.setMapOutputKeyClass(Text.class);
221    job.setMapOutputValueClass(IntWritable.class);
222    job.setOutputFormatClass(TextOutputFormat.class);
223    job.setOutputKeyClass(Text.class);
224    job.setOutputValueClass(IntWritable.class);
225    FileOutputFormat.setOutputPath(job, outputDir);
226    job.setReducerClass(IntSumReducer.class);
227    return job;
228  }
229
230  private static Scan getConfiguredScanForJob(Configuration conf, String[] args)
231      throws IOException {
232    // create scan with any properties set from TableInputFormat
233    Scan s = TableInputFormat.createScanFromConfiguration(conf);
234    // Set Scan Versions
235    if (conf.get(TableInputFormat.SCAN_MAXVERSIONS) == null) {
236      // default to all versions unless explicitly set
237      s.readVersions(Integer.MAX_VALUE);
238    }
239    s.setCacheBlocks(false);
240    // Set RowFilter or Prefix Filter if applicable.
241    Filter rowFilter = getRowFilter(args);
242    if (rowFilter!= null) {
243      LOG.info("Setting Row Filter for counter.");
244      s.setFilter(rowFilter);
245    }
246    // Set TimeRange if defined
247    long timeRange[] = getTimeRange(args);
248    if (timeRange != null) {
249      LOG.info("Setting TimeRange for counter.");
250      s.setTimeRange(timeRange[0], timeRange[1]);
251    }
252    return s;
253  }
254
255
256  private static Filter getRowFilter(String[] args) {
257    Filter rowFilter = null;
258    String filterCriteria = (args.length > 3) ? args[3]: null;
259    if (filterCriteria == null) return null;
260    if (filterCriteria.startsWith("^")) {
261      String regexPattern = filterCriteria.substring(1, filterCriteria.length());
262      rowFilter = new RowFilter(CompareOperator.EQUAL, new RegexStringComparator(regexPattern));
263    } else {
264      rowFilter = new PrefixFilter(Bytes.toBytesBinary(filterCriteria));
265    }
266    return rowFilter;
267  }
268
269  private static long[] getTimeRange(String[] args) throws IOException {
270    final String startTimeArgKey = "--starttime=";
271    final String endTimeArgKey = "--endtime=";
272    long startTime = 0L;
273    long endTime = 0L;
274
275    for (int i = 1; i < args.length; i++) {
276      System.out.println("i:" + i + "arg[i]" + args[i]);
277      if (args[i].startsWith(startTimeArgKey)) {
278        startTime = Long.parseLong(args[i].substring(startTimeArgKey.length()));
279      }
280      if (args[i].startsWith(endTimeArgKey)) {
281        endTime = Long.parseLong(args[i].substring(endTimeArgKey.length()));
282      }
283    }
284
285    if (startTime == 0 && endTime == 0)
286      return null;
287
288    endTime = endTime == 0 ? HConstants.LATEST_TIMESTAMP : endTime;
289    return new long [] {startTime, endTime};
290  }
291
292  @Override
293  public int run(String[] args) throws Exception {
294    if (args.length < 2) {
295      printUsage(args.length);
296      return -1;
297    }
298    Job job = createSubmittableJob(getConf(), args);
299    return (job.waitForCompletion(true) ? 0 : 1);
300  }
301
302  private void printUsage(int parameterCount) {
303    System.err.println("ERROR: Wrong number of parameters: " + parameterCount);
304    System.err.println("Usage: hbase cellcounter <tablename> <outputDir> [reportSeparator] "
305        + "[^[regex pattern] or [Prefix]] [--starttime=<starttime> --endtime=<endtime>]");
306    System.err.println("  Note: -D properties will be applied to the conf used.");
307    System.err.println("  Additionally, all of the SCAN properties from TableInputFormat can be "
308        + "specified to get fine grained control on what is counted.");
309    System.err.println("   -D" + TableInputFormat.SCAN_ROW_START + "=<rowkey>");
310    System.err.println("   -D" + TableInputFormat.SCAN_ROW_STOP + "=<rowkey>");
311    System.err.println("   -D" + TableInputFormat.SCAN_COLUMNS + "=\"<col1> <col2>...\"");
312    System.err.println("   -D" + TableInputFormat.SCAN_COLUMN_FAMILY
313        + "=<family1>,<family2>, ...");
314    System.err.println("   -D" + TableInputFormat.SCAN_TIMESTAMP + "=<timestamp>");
315    System.err.println("   -D" + TableInputFormat.SCAN_TIMERANGE_START + "=<timestamp>");
316    System.err.println("   -D" + TableInputFormat.SCAN_TIMERANGE_END + "=<timestamp>");
317    System.err.println("   -D" + TableInputFormat.SCAN_MAXVERSIONS + "=<count>");
318    System.err.println("   -D" + TableInputFormat.SCAN_CACHEDROWS + "=<count>");
319    System.err.println("   -D" + TableInputFormat.SCAN_BATCHSIZE + "=<count>");
320    System.err.println(" <reportSeparator> parameter can be used to override the default report "
321        + "separator string : used to separate the rowId/column family name and qualifier name.");
322    System.err.println(" [^[regex pattern] or [Prefix] parameter can be used to limit the cell "
323        + "counter count operation to a limited subset of rows from the table based on regex or "
324        + "prefix pattern.");
325  }
326
327  /**
328   * Main entry point.
329   * @param args The command line parameters.
330   * @throws Exception When running the job fails.
331   */
332  public static void main(String[] args) throws Exception {
333    int errCode = ToolRunner.run(HBaseConfiguration.create(), new CellCounter(), args);
334    System.exit(errCode);
335  }
336
337}