1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.mapred;
20
21 import java.io.IOException;
22
23 import org.apache.hadoop.hbase.classification.InterfaceAudience;
24 import org.apache.hadoop.hbase.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.util.Tool;
36 import org.apache.hadoop.util.ToolRunner;
37
38
39
40
41
42
43 @InterfaceAudience.Public
44 @InterfaceStability.Stable
45 public class RowCounter extends Configured implements Tool {
46
47 static final String NAME = "rowcounter";
48
49
50
51
52 static class RowCounterMapper
53 implements TableMap<ImmutableBytesWritable, Result> {
54 private static enum Counters {ROWS}
55
56 public void map(ImmutableBytesWritable row, Result values,
57 OutputCollector<ImmutableBytesWritable, Result> output,
58 Reporter reporter)
59 throws IOException {
60
61 reporter.incrCounter(Counters.ROWS, 1);
62 }
63
64 public void configure(JobConf jc) {
65
66 }
67
68 public void close() throws IOException {
69
70 }
71 }
72
73
74
75
76
77
78 public JobConf createSubmittableJob(String[] args) throws IOException {
79 JobConf c = new JobConf(getConf(), getClass());
80 c.setJobName(NAME);
81
82 StringBuilder sb = new StringBuilder();
83 final int columnoffset = 2;
84 for (int i = columnoffset; i < args.length; i++) {
85 if (i > columnoffset) {
86 sb.append(" ");
87 }
88 sb.append(args[i]);
89 }
90
91 TableMapReduceUtil.initTableMapJob(args[1], sb.toString(),
92 RowCounterMapper.class, ImmutableBytesWritable.class, Result.class, c);
93 c.setNumReduceTasks(0);
94
95 FileOutputFormat.setOutputPath(c, new Path(args[0]));
96 return c;
97 }
98
99 static int printUsage() {
100 System.out.println(NAME +
101 " <outputdir> <tablename> <column1> [<column2>...]");
102 return -1;
103 }
104
105 public int run(final String[] args) throws Exception {
106
107 if (args.length < 3) {
108 System.err.println("ERROR: Wrong number of parameters: " + args.length);
109 return printUsage();
110 }
111 JobClient.runJob(createSubmittableJob(args));
112 return 0;
113 }
114
115
116
117
118
119 public static void main(String[] args) throws Exception {
120 int errCode = ToolRunner.run(HBaseConfiguration.create(), new RowCounter(), args);
121 System.exit(errCode);
122 }
123 }