View Javadoc

1   /**
2    * Copyright 2008 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.mapreduce;
21  
22  import java.io.IOException;
23  
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.HBaseConfiguration;
26  import org.apache.hadoop.hbase.KeyValue;
27  import org.apache.hadoop.hbase.client.Result;
28  import org.apache.hadoop.hbase.client.Scan;
29  import org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter;
30  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
31  import org.apache.hadoop.hbase.util.Bytes;
32  import org.apache.hadoop.mapreduce.Job;
33  import org.apache.hadoop.mapreduce.lib.output.NullOutputFormat;
34  import org.apache.hadoop.util.GenericOptionsParser;
35  
36  /**
37   * A job with a just a map phase to count rows. Map outputs table rows IF the
38   * input row has columns that have content.
39   */
40  public class RowCounter {
41  
42    /** Name of this 'program'. */
43    static final String NAME = "rowcounter";
44  
45    /**
46     * Mapper that runs the count.
47     */
48    static class RowCounterMapper
49    extends TableMapper<ImmutableBytesWritable, Result> {
50  
51      /** Counter enumeration to count the actual rows. */
52      public static enum Counters {ROWS}
53  
54      /**
55       * Maps the data.
56       *
57       * @param row  The current table row key.
58       * @param values  The columns.
59       * @param context  The current context.
60       * @throws IOException When something is broken with the data.
61       * @see org.apache.hadoop.mapreduce.Mapper#map(KEYIN, VALUEIN,
62       *   org.apache.hadoop.mapreduce.Mapper.Context)
63       */
64      @Override
65      public void map(ImmutableBytesWritable row, Result values,
66        Context context)
67      throws IOException {
68        for (KeyValue value: values.list()) {
69          if (value.getValue().length > 0) {
70            context.getCounter(Counters.ROWS).increment(1);
71            break;
72          }
73        }
74      }
75    }
76  
77    /**
78     * Sets up the actual job.
79     *
80     * @param conf  The current configuration.
81     * @param args  The command line parameters.
82     * @return The newly created job.
83     * @throws IOException When setting up the job fails.
84     */
85    public static Job createSubmittableJob(Configuration conf, String[] args)
86    throws IOException {
87      String tableName = args[0];
88      Job job = new Job(conf, NAME + "_" + tableName);
89      job.setJarByClass(RowCounter.class);
90      // Columns are space delimited
91      StringBuilder sb = new StringBuilder();
92      final int columnoffset = 1;
93      for (int i = columnoffset; i < args.length; i++) {
94        if (i > columnoffset) {
95          sb.append(" ");
96        }
97        sb.append(args[i]);
98      }
99      Scan scan = new Scan();
100     scan.setFilter(new FirstKeyOnlyFilter());
101     if (sb.length() > 0) {
102       for (String columnName :sb.toString().split(" ")) {
103         String [] fields = columnName.split(":");
104         if(fields.length == 1) {
105           scan.addFamily(Bytes.toBytes(fields[0]));
106         } else {
107           scan.addColumn(Bytes.toBytes(fields[0]), Bytes.toBytes(fields[1]));
108         }
109       }
110     }
111     // Second argument is the table name.
112     job.setOutputFormatClass(NullOutputFormat.class);
113     TableMapReduceUtil.initTableMapperJob(tableName, scan,
114       RowCounterMapper.class, ImmutableBytesWritable.class, Result.class, job);
115     job.setNumReduceTasks(0);
116     return job;
117   }
118 
119   /**
120    * Main entry point.
121    *
122    * @param args  The command line parameters.
123    * @throws Exception When running the job fails.
124    */
125   public static void main(String[] args) throws Exception {
126     Configuration conf = HBaseConfiguration.create();
127     String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
128     if (otherArgs.length < 1) {
129       System.err.println("ERROR: Wrong number of parameters: " + args.length);
130       System.err.println("Usage: RowCounter <tablename> [<column1> <column2>...]");
131       System.exit(-1);
132     }
133     Job job = createSubmittableJob(conf, otherArgs);
134     System.exit(job.waitForCompletion(true) ? 0 : 1);
135   }
136 }