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  
23  import org.apache.hadoop.classification.InterfaceAudience;
24  import org.apache.hadoop.classification.InterfaceStability;
25  import org.apache.hadoop.conf.Configured;
26  import org.apache.hadoop.fs.Path;
27  import org.apache.hadoop.hbase.HBaseConfiguration;
28  import org.apache.hadoop.hbase.client.Result;
29  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
30  import org.apache.hadoop.mapred.FileOutputFormat;
31  import org.apache.hadoop.mapred.JobClient;
32  import org.apache.hadoop.mapred.JobConf;
33  import org.apache.hadoop.mapred.OutputCollector;
34  import org.apache.hadoop.mapred.Reporter;
35  import org.apache.hadoop.mapred.lib.IdentityReducer;
36  import org.apache.hadoop.util.Tool;
37  import org.apache.hadoop.util.ToolRunner;
38  
39  /**
40   * A job with a map to count rows.
41   * Map outputs table rows IF the input row has columns that have content.
42   * Uses an {@link IdentityReducer}
43   */
44  @Deprecated
45  @InterfaceAudience.Public
46  @InterfaceStability.Stable
47  public class RowCounter extends Configured implements Tool {
48    // Name of this 'program'
49    static final String NAME = "rowcounter";
50  
51    /**
52     * Mapper that runs the count.
53     */
54    static class RowCounterMapper
55    implements TableMap<ImmutableBytesWritable, Result> {
56      private static enum Counters {ROWS}
57  
58      public void map(ImmutableBytesWritable row, Result values,
59          OutputCollector<ImmutableBytesWritable, Result> output,
60          Reporter reporter)
61      throws IOException {
62          // Count every row containing data, whether it's in qualifiers or values
63          reporter.incrCounter(Counters.ROWS, 1);
64      }
65  
66      public void configure(JobConf jc) {
67        // Nothing to do.
68      }
69  
70      public void close() throws IOException {
71        // Nothing to do.
72      }
73    }
74  
75    /**
76     * @param args
77     * @return the JobConf
78     * @throws IOException
79     */
80    public JobConf createSubmittableJob(String[] args) throws IOException {
81      JobConf c = new JobConf(getConf(), getClass());
82      c.setJobName(NAME);
83      // Columns are space delimited
84      StringBuilder sb = new StringBuilder();
85      final int columnoffset = 2;
86      for (int i = columnoffset; i < args.length; i++) {
87        if (i > columnoffset) {
88          sb.append(" ");
89        }
90        sb.append(args[i]);
91      }
92      // Second argument is the table name.
93      TableMapReduceUtil.initTableMapJob(args[1], sb.toString(),
94        RowCounterMapper.class, ImmutableBytesWritable.class, Result.class, c);
95      c.setNumReduceTasks(0);
96      // First arg is the output directory.
97      FileOutputFormat.setOutputPath(c, new Path(args[0]));
98      return c;
99    }
100 
101   static int printUsage() {
102     System.out.println(NAME +
103       " <outputdir> <tablename> <column1> [<column2>...]");
104     return -1;
105   }
106 
107   public int run(final String[] args) throws Exception {
108     // Make sure there are at least 3 parameters
109     if (args.length < 3) {
110       System.err.println("ERROR: Wrong number of parameters: " + args.length);
111       return printUsage();
112     }
113     JobClient.runJob(createSubmittableJob(args));
114     return 0;
115   }
116 
117   /**
118    * @param args
119    * @throws Exception
120    */
121   public static void main(String[] args) throws Exception {
122     int errCode = ToolRunner.run(HBaseConfiguration.create(), new RowCounter(), args);
123     System.exit(errCode);
124   }
125 }